博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA SHA1 加密 对应 c# SHA1 加密
阅读量:4634 次
发布时间:2019-06-09

本文共 2006 字,大约阅读时间需要 6 分钟。

java:

1   public static String SHA1(String decript) { 2         try { 3             MessageDigest digest = MessageDigest.getInstance("SHA-1"); 4             digest.update(decript.getBytes("UTF-8")); 5             byte[] messageDigest = digest.digest(); 6             StringBuilder hexString = new StringBuilder(); 7             for (byte message : messageDigest) { 8                 String shaHex = Integer.toHexString(message & 0xFF); 9                 if (shaHex.length() < 2)10                     hexString.append(0);11 12                 hexString.append(shaHex);13             }14             return hexString.toString().toLowerCase();15         } catch (Exception e) {16             e.printStackTrace();17         }18         return "";19     }

c# :

1         ///    2         /// SHA1 加密,返回大写字符串   3         ///    4         /// 需要加密字符串   5         /// 
返回40位UTF8 大写
6 public static string SHA1(string content) 7 { 8 return SHA1(content, Encoding.UTF8).ToLower(); 9 }10 /// 11 /// SHA1 加密,返回大写字符串 12 /// 13 /// 需要加密字符串 14 /// 指定加密编码 15 ///
返回40位大写字符串
16 public static string SHA1(string content, Encoding encode)17 {18 try19 {20 SHA1 sha1 = new SHA1CryptoServiceProvider();21 byte[] bytes_in = encode.GetBytes(content);22 byte[] bytes_out = sha1.ComputeHash(bytes_in);23 sha1.Dispose();24 25 var sb = new StringBuilder();26 foreach (byte b in bytes_out)27 {28 sb.Append(b.ToString("x2"));29 }30 //所有字符转为小写31 return sb.ToString().ToLower();32 }33 catch (Exception ex)34 {35 throw new Exception("SHA1加密出错:" + ex.Message);36 }37 }

 

转载于:https://www.cnblogs.com/wuweimin/p/7839556.html

你可能感兴趣的文章
eShopOnContainers 知多少[8]:Ordering microservice
查看>>
bzoj 1040: [ZJOI2008]骑士 树形dp
查看>>
hdu 1423
查看>>
hdu 4720
查看>>
友元关系
查看>>
IO缓冲区
查看>>
一道SQL统计试题
查看>>
OO第二次博客作业
查看>>
mysql 批量更新数据类型
查看>>
HDU 1273 漫步森林
查看>>
Bootstrap4.x 新增
查看>>
太久没来了,好尴尬呀
查看>>
django 链接地址匹配流程
查看>>
图片和文件上传的两款插件
查看>>
简析平衡树(三)——浅谈Splay
查看>>
The Knuth-Morris-Pratt Algorithm in my own words(转)
查看>>
374. Guess Number Higher or Lower
查看>>
目标反射回波检测算法及其FPGA实现 之一:算法概述
查看>>
php去除字符串首尾空格(包括全角)(转)
查看>>
第十一章
查看>>