📄 uniquekey.java
字号:
// =========================================================
// Copyright (c) 2000-2004 by Agile Software Corp.
// All Rights Reserved.
//
// This is unpublished proprietary source code of Agile Software
// Corp. The copyright notice above does not evidence
// any actual or intended publication of such source code.
// ==========================================================
package test.passwd;
import test.passwd.Base64;
import test.passwd.IntRef;
/**
* Class to generate unique key from user credentials (username and password) and
* to encode key to back to username & password
*/
public class UniqueKey
{
/** encodes username and password to a unique secure key
* @param String clear text username
* @param String cler text password
* @return String generated session key
* */
public static String encode(String username, String password) throws Exception {
debug("Username: " + username + " , Password: " + password);
int strSize = username.length();
int strSizeLength = (new Integer(strSize)).toString().length();
String userKey = "" + strSizeLength + strSize + username;
strSize = password.length();
strSizeLength = (new Integer(strSize)).toString().length();
userKey = userKey + strSizeLength + strSize + password;
debug("Key: " + userKey);
String encKey = new String(Base64.encode(userKey.getBytes()), "UTF-8");
encKey = new String(Base64.encode(encKey.getBytes()), "UTF-8");
debug("Encoded Key: " + encKey);
return encKey;
}
/** encodes password to a unique secure key
* @param String cler text password
* @return String generated session key
* */
public static String encodePassword(String password) throws Exception {
debug("Password: " + password);
int strSize = password.length();
int strSizeLength = (new Integer(strSize)).toString().length();
String userKey = "" + strSizeLength + strSize + password;
String encKey = new String(Base64.encode(userKey.getBytes()), "UTF-8");
encKey = new String(Base64.encode(encKey.getBytes()), "UTF-8");
debug("Encrypt Password: " + encKey);
return encKey;
}
/** decodes the unique key to password
* @param String unique session key
* @return String clear text password
* */
public static String decodePassword(String encKey) throws Exception {
debug("Encrypt Password: " + encKey);
String userKey = new String(Base64.decode(encKey.getBytes("UTF-8"), new IntRef()), "UTF-8");
userKey = new String(Base64.decode(userKey.getBytes("UTF-8"), new IntRef()), "UTF-8");
int strSizeLength = Integer.parseInt(userKey.substring(0,1));
int strSize = Integer.parseInt(userKey.substring(1, strSizeLength + 1));
String password = userKey.substring(strSizeLength + 1,strSize + strSizeLength + 1);
debug("Password: " + password);
return password;
}
/** decodes the unique key to username and password
* @param String unique session key
* @return String clear text username
* */
public static String decodeUname(String encKey) throws Exception {
debug("Encoded Key: " + encKey);
String userKey = new String(Base64.decode(encKey.getBytes("UTF-8"), new IntRef()), "UTF-8");
userKey = new String(Base64.decode(userKey.getBytes("UTF-8"), new IntRef()), "UTF-8");
debug("Key: " + userKey);
int strSizeLength = Integer.parseInt(userKey.substring(0,1));
int strSize = Integer.parseInt(userKey.substring(1, strSizeLength + 1));
String username = userKey.substring(strSizeLength + 1,strSize + strSizeLength + 1);
debug("Username: " + username);
return username;
}
/** decodes the unique key to username and password
* @param String unique session key
* @return String clear text password
* */
public static String decodePwd(String encKey) throws Exception {
debug("Encoded Key: " + encKey);
String userKey = new String(Base64.decode(encKey.getBytes("UTF-8"), new IntRef()), "UTF-8");
userKey = new String(Base64.decode(userKey.getBytes("UTF-8"), new IntRef()), "UTF-8");
debug("Key: " + userKey);
int strSizeLength = Integer.parseInt(userKey.substring(0,1));
int strSize = Integer.parseInt(userKey.substring(1, strSizeLength + 1));
String tempPassword = userKey.substring(strSize + strSizeLength + 1);
strSizeLength = Integer.parseInt(tempPassword.substring(0,1));
strSize = Integer.parseInt(tempPassword.substring(1, strSizeLength + 1));
String password = tempPassword.substring(strSizeLength + 1,strSize + strSizeLength + 1);
debug("Username: " + password);
return password;
}
public static void main(String[] args) throws Exception {
String username = "agile";
String password = "sss";
String userKey = UniqueKey.encode(username, password);
System.out.println("userKey="+userKey);
String u = null, p= null;
u = UniqueKey.decodeUname(userKey);
p = UniqueKey.decodePwd(userKey);
System.out.println("Username: " + u + " , Password: " + p);
String passwd = UniqueKey.encodePassword(password);
System.out.println("passwd="+passwd);
p= null;
p = UniqueKey.decodePassword(passwd);
System.out.println("Password: " + p);
}
static void debug(String s) {
//System.out.println(s);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -