加密 总结
/**
* MD5加密
*
* @param pwd
* 传入的密码字符串
* @return 加密后的密码
* @see [类、类#方法、类#成员]
*/
public static String getMD5pwd(String pwd) {
StringBuffer code = new StringBuffer("");
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(pwd.getBytes());
byte[] bt = md.digest();
int j;
for (int i = 0; i < bt.length; i++) {
j = bt[i];
if (j < 0) {
j += 256;
}
if (j < 16) {
code.append("0");
}
code.append(Integer.toHexString(j));
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
return code.toString();
}
/**
* 获取系统密钥
* @return 系统默认密钥
* @see [类、类#方法、类#成员]
*/
public static String getSystemKey(){
SysConfigManager sysConfigManager = DBGroup.getInstance().getVGroup().getSystemGroup().getSysConfigManager();
SysConfig key_sysConfig = sysConfigManager.findSysConfigByCode("system_key");
return key_sysConfig.getConfigValue();
}
/**
* Des解密
*
* @param pwd
* 密码
* @return 解密后的密码
* @see [类、类#方法、类#成员]
*/
public static String DesDecode(String pwd,String desSceret) {
// 为我们选择的DES算法生成一个KeyGenerator对象
try {
Key key = init("DESede",desSceret);
Cipher cipher = Cipher.getInstance("DESede");
// 解密
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] tt = cipher.doFinal(hex2byte(pwd));
String code = new String(tt);
return code;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 创建密匙
* @param algorithm 加密算法,可用 DES,DESede,Blowfish
* @param desSceret 加密密钥,要求转换为UTF-8的格式,字节长度至少24位
* @return 秘密(对称)密钥
* @see [类、类#方法、类#成员]
*/
private static Key init(String algorithm, String desSceret) {
Key key = null;
try {
DESedeKeySpec dks = new DESedeKeySpec(desSceret
.getBytes("UTF-8"));
SecretKeyFactory keyFactory = SecretKeyFactory
.getInstance(algorithm);
key = keyFactory.generateSecret(dks);
} catch (Exception e) {
e.printStackTrace();
}
return key;
}