首页 新闻 赞助 找找看

请求Java的SHA1加密算法转换为NET怎么写的?

0
悬赏园豆:5 [已关闭问题] 关闭于 2012-10-16 17:14

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怎么写的?

面包快跑的主页 面包快跑 | 初学一级 | 园豆:189
提问于:2012-10-11 10:19
< >
分享
所有回答(1)
0

    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;  
    } 

荣耀属于跪拜猫 | 园豆:832 (小虾三级) | 2012-10-11 12:51

注意这两句用来转换string到byte[]。要注意字符集,这里是ascii字符集。

               System.Text.ASCIIEncoding encoder = new System.Text.ASCIIEncoding();  
               byte[] combined = encoder.GetBytes(str); 

支持(0) 反对(0) 荣耀属于跪拜猫 | 园豆:832 (小虾三级) | 2012-10-11 12:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册