前几天在Domino中用了Java代理写了一个采用MD5对字符串进行加密以及验证的简单功能。
参考文章:
1. JAVA上加密算法的实现用例:http://www-128.ibm.com/developerworks/cn/java/l-security
实现过程如下:
1.对密码字符串进行加密:
首先用生成一个MessageDigest类:MessageDigest alg= MessageDigest.getInstance("MD5");
添加要进行计算摘要的信息:alg.update(password.getBytes()); //其中password为密码字符串
计算出摘要:byte[] digest=alga.digest();
然后将摘要转换成16进制:byte2hex(digest)
/*
* 二进制转换为十六进制字符串
*
* @param b 二进制数组 @return 十六进制字符串
*/
private static String byte2hex(byte[] bytes) {
String hs = "";
String stmp = "";
for (int i = 0; i < bytes.length; i++) {
stmp = (java.lang.Integer.toHexString(bytes[i] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}将生成的16进制摘要保存到数据库中。
2. 验证:
获取输入的验证密码:password=doc.getItemValueString("F_PasswordValidate");
在数据库中获取相关的摘要,并对摘要进行转换:
byte[] digest = toBytes(curDoc.getItemValueString("F_Password"));
/**
* 把字符串转换成byte[]
* @param hex 要转换的字符串
* @return 转换后的byte[]
*/
public static byte[] toBytes(String hex) {
byte[] bytes = new byte[16];
for(int i = 0,j=0; i<hex.length(); i=i+2,j++){
bytes[j] = Integer.valueOf(hex.substring(i,i+2), 16).byteValue();
}
return bytes;
}
最后进行验证,比较摘要是否相同:
result = isValidate(digest, password);
if (result == true)
retmsg = "成功!";
else
retmsg = "失败!";
public boolean isValidate(byte[] digest, String password) {
try {
MessageDigest alga = MessageDigest.getInstance("MD5");
alga.update(password.getBytes());
if (MessageDigest.isEqual(digest, alga.digest()))
return true;
else
return false;
} catch (Exception ex) {
System.out.println("算法出错.");
return false;
}
}
完整Java代码:
/*
* Created on 2005-9-23
*
*/
package security;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* @author stunet
*
* 采用Java对字符串进行MD5的加密以及验证示例
*
*/
public class MD5DigestDemo {
public static void main(String[] args) {
try {
// 进行字符串加密
String password = "NET";
System.out.println("加密后字符串:" + MD5DigestDemo.md5Digest(password));
// 进行字符串验证
String validate = MD5DigestDemo.md5Digest(password);
String passwordValidate = "NET";
boolean isSuccess = MD5DigestDemo.isValidate(toBytes(validate),
passwordValidate);
if (isSuccess)
System.out.println("验证成功!");
else
System.out.println("验证失败!");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
/**
*
* @param password
* 需要加密的字符串
* @return 加密后的字符串
* @throws NoSuchAlgorithmException
*/
public static String md5Digest(String password)
throws NoSuchAlgorithmException {
String temp;
MessageDigest alg = MessageDigest.getInstance("MD5");
alg.update(password.getBytes());
byte[] digest = alg.digest();
temp = byte2hex(digest);
return temp;
}
/**
*
* @param digest
* 加密后的字符串
* @param password
* 验证的字符串
* @return 验证是否成功
* @throws NoSuchAlgorithmException
*/
public static boolean isValidate(byte[] digest, String password)
throws NoSuchAlgorithmException {
boolean flag = false;
MessageDigest alg = MessageDigest.getInstance("MD5");
alg.update(password.getBytes());
if (MessageDigest.isEqual(digest, alg.digest()))
flag = true;
else
flag = false;
return flag;
}
/*
* 二进制转换为十六进制字符串
*
* @param b 二进制数组 @return 十六进制字符串
*/
private static String byte2hex(byte[] bytes) {
String hs = "";
String stmp = "";
for (int i = 0; i < bytes.length; i++) {
stmp = (java.lang.Integer.toHexString(bytes[i] & 0XFF));
if (stmp.length() == 1)
hs = hs + "0" + stmp;
else
hs = hs + stmp;
}
return hs.toUpperCase();
}
/*
* 把字符串转换成byte[]
*
* @param hex 要转换的字符串 @return 转换后的byte[]
*/
private static byte[] toBytes(String hex) {
//String[] hexs = hex.split(":");
byte[] bytes = new byte[16];
for (int i = 0, j = 0; i < hex.length(); i = i + 2, j++) {
bytes[j] = Integer.valueOf(hex.substring(i, i + 2), 16).byteValue();
}
return bytes;
}
}