我以前在.NET里面是这样做的:
public static string Encrypt(string str)
{
UnicodeEncoding code = new UnicodeEncoding();
byte[] bytes = code.GetBytes(str);
byte[] hashValue = ((HashAlgorithm)CryptoConfig.CreateFromName("SHA")).ComputeHash(bytes);
return Convert.ToBase64String(hashValue);
}
现在,这些加密的口令已经存到数据库里面了,我想用Java来实现同样的功能,请问我应该如何做呢?请高手赐教!

解决方案 »

  1.   

    首先用生成一个MessageDigest类,确定计算方法 java.security.MessageDigest alga=java.security.MessageDigest.getInstance("SHA-1"); 添加要进行计算摘要的信息 alga.update(myinfo.getBytes()); 计算出摘要 byte[] digesta=alga.digest(); 发送给其他人你的信息和摘要 其他人用相同的方法初始化,添加信息,最后进行比较摘要是否相同 algb.isEqual(digesta,algb.digest()) 
      

  2.   

    http://www-900.ibm.com/developerWorks/cn/java/l-security/index.shtml
    很详细