import java.security.MessageDigest;
/**
* MD5加密工具类
*/
public class CipherUtil {
/**
* 十六进制下数字到字符的映射数组
*/
private final static String[] hexDigits = {"0", "1", "2", "3", "4",
"5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
/**
* 把inputString加密
* @param inputString 密码明文
* @return 密码密文
*/
public static String generateMD5Password(String inputString, String account){
if(account != null){
return encodeByMD5(account+"_"+encodeByMD5(inputString));
}
return encodeByMD5(inputString);
}
/**
* 验证输入的密码是否正确
* @param password 加密后的密码
* @param inputString 输入的字符串
* @return 验证结果,TRUE:正确 FALSE:错误
*/
public static boolean validateMD5Password(String password, String inputString){
if(password.equals(encodeByMD5(inputString))){
return true;
} else{
return false;
}
}
/**
* 对字符串进行MD5加密
* @param originString 明文
* @return 密文
*/
private static String encodeByMD5(String originString){
if (originString != null){
try{
//创建具有指定算法名称的信息摘要
MessageDigest md = MessageDigest.getInstance("MD5");
//使用指定的字节数组对摘要进行最后更新,然后完成摘要计算
byte[] results = md.digest(originString.getBytes());
//将得到的字节数组变成字符串返回
String resultString = byteArrayToHexString(results);
return resultString/*.toUpperCase()*/;
} catch(Exception ex){
ex.printStackTrace();
}
}
return null;
}
/**
* 转换字节数组为十六进制字符串
* @param 字节数组
* @return 十六进制字符串
*/
private static String byteArrayToHexString(byte[] b){
StringBuffer resultSb = new StringBuffer();
for (int i = 0; i < b.length; i++){
resultSb.append(byteToHexString(b[i]));
}
return resultSb.toString();
}
/**
* 将一个字节转化成十六进制形式的字符串
* @param b 十进制字符串
* @return 16进制字符串
*/
private static String byteToHexString(byte b){
int n = b;
if (n < 0)
n = 256 + n;
int d1 = n / 16;
int d2 = n % 16;
return hexDigits[d1] + hexDigits[d2];
}
public static void main(String[] args) {
String encode = CipherUtil.encodeByMD5("123456");
System.out.println("encode = " + encode);
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class EncryptDecrypt {
/**
* 加密
*
* @param pass
* @return
*/
public static String Encrypt(String pass) {
int len = pass.length();
char enPass[] = new char[len * 2];
int chk[] = { 104, 115, 109, 97, 122, 104, 106, 104 };
if (pass == null || pass.equals(""))
return "";
int i = 0;
for (int j = 0; i < len; j++) {
if (j == 8) {
j = 0;
}
int temp = pass.charAt(i) ^ chk[j];
String C = Integer.toString(temp, 16);
if (C.length() > 1) {
enPass[i * 2] = C.charAt(0);
enPass[i * 2 + 1] = C.charAt(1);
} else {
enPass[i * 2] = '0';
enPass[i * 2 + 1] = C.charAt(0);
}
i++;
}
return new String(enPass);
}
/**
* 解密
*
* @param enPass
* @return
*/
public static String unEncrypt(String enPass) {
int len = enPass.length();
if (len % 2 != 0) {
return "";
}
if (enPass == null || enPass.equals(""))
return "";
char pass[] = new char[len / 2];
char C[] = new char[2];
int chk[] = { 104, 115, 109, 97, 122, 104, 106, 104 };
int i = 0;
for (int j = 0; i < len / 2; j++) {
if (j == 8) {
j = 0;
}
C[0] = enPass.charAt(i * 2);
C[1] = enPass.charAt(i * 2 + 1);
int temp = Integer.parseInt(String.valueOf(C), 16);
pass[i] = (char) (temp ^ chk[j]);
i++;
}
return new String(pass);
}
public static void main(String args[]) {
System.out.println("请输入数据库密码(回车确认):");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try {
String pwd = br.readLine();
System.out.println("================回车离开");
System.out.println("原密码:" + pwd);
System.out.println("加密密码:" + Encrypt(pwd));
System.out.println("加密密码:" + unEncrypt(Encrypt(pwd)));
} catch (IOException e) {
e.printStackTrace();
}
}