MessageDigest md= MessageDigest.getInstance("SHA-1");
byte[] b1 = nonce != null ? Base64.decode(nonce) : new byte[0];
//生成字符字节流
byte[] b2 = created != null ? created.getBytes("UTF-8"): new byte[0];
byte[] b3 = password;
//根据我们传得值的长度生成流的长度
byte[] b4 = new byte[b1.length + b2.length + b3.length];
//利用sha-1加密字符 md.update(b1, 0, b1.length); md.update(b2,0,b2.length); md.update(b3,0,b3.length); //生成sha-1加密后的流
b4 = md.digest();
//生成最终的加密字符串
String result = new String(Base64.encode(b4));以上是Java加密,转换为NET怎么写的?
public static string HashCode(string str)
{
string rethash = "";
try
{
System.Security.Cryptography.SHA1 hash = System.Security.Cryptography.SHA1.Create();
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] combined = encoder.GetBytes(str);
hash.ComputeHash(combined);
rethash = Convert.ToBase64String(hash.Hash);
}
catch (Exception ex)
{
string strerr = "Error in HashCode : " + ex.Message;
}
return rethash;
}
注意这两句用来转换string到byte[]。要注意字符集,这里是ascii字符集。
System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();
byte[] combined = encoder.GetBytes(str);