一,SHA加密相关java代码:
/××
algorithm=“SHA”------pasword=密码
×/
private
String encryptLdapPassword(String algorithm, String _password) {
String sEncrypted = _password;
if
((_password !=
null
) && (_password.length() >
0
)) {
boolean
bMD5 = algorithm.equalsIgnoreCase(
"MD5"
);
boolean
bSHA = algorithm.equalsIgnoreCase(
"SHA"
)
|| algorithm.equalsIgnoreCase(
"SHA1"
)
|| algorithm.equalsIgnoreCase(
"SHA-1"
);
if
(bSHA || bMD5) {
String sAlgorithm =
"MD5"
;
if
(bSHA) {
sAlgorithm =
"SHA"
;
}
try
{
MessageDigest md = MessageDigest.getInstance(sAlgorithm);
md.update(_password.getBytes(
"UTF-8"
));
sEncrypted =
"{"
+ sAlgorithm +
"}"
+ (
new
BASE64Encoder()).encode(md.digest());
}
catch
(Exception e) {
sEncrypted =
null
;
logger.error(e, e);
}
}
}
return
sEncrypted;
}
说明:将密码经过 MD5 或SHA-1运算,得到 32 字节的字符串,然后每2个字节压缩成一个十六进制字符,这样得到16字节的字符串,最后经过Base64编码。
详细代码:http://edwin.baculsoft.com/2011/10/ldap-programming-with-java/