惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

博客园 - 笑萧亦然

KingdeeK3-修改单据邮件发送的自定义字段 微软在GitHub上开放源代码 【转】GitHub入门详细讲解 asp.net 导出excel 中文乱码解决方法 (转) C#自动切换Windows窗口程序,如何才能调出主窗口? 系统颜色对照表 showModalDialog后如何刷新父页面 delegate Demo (一个关于System.Timers.Timer的Demo) 常用SQL关于表的操作 System.Web.HttpException 与 HTTP Error 404.13 - Not Found问题解决说明 SQL 数据库常用的系统存储过程解析 根据经纬度坐标计算两点间几何距离 - 椰子树下 - CSDN博客 获取库中的所有字段的描述/获取某个表中所有字段方法 多个CSS风格共用同一(背景)图片_那一片天_百度空间 学习总结之三(SQL SERVER游标CURSOR的使用) SQL Server 如何锁一个表的某一行 - MS-SQL Server / 基础类 SQL中的系统变量一览 简单的弹出层窗口应用(DIV+JS) - 笑萧亦然 - 博客园 sql中左侧不够自动补0的写法,优!
JAVA md5把我气到疯的代码,天哪,神呀,我的C# 啊。
笑萧亦然 · 2011-04-27 · via 博客园 - 笑萧亦然

以下代码是客户写给我的,说是他们JAVA的加密方法,其中红色的那两句差点把我整垮了。

Java人士说:content是明文  key是密钥,用密钥来加密明文。

C#认识说:什么明文密钥一起丢进去,所以只需要 content+key 丢到computehash(...)一切尽在转化中。。。。

 private static String getDigest(String content, String key, String algorithm) {

                   try {

                            byte[] plainText = content.getBytes("utf-8");

                            MessageDigest messageDigest = MessageDigest.getInstance(algorithm);

                            messageDigest.update(plainText);

                            byte[] digest = messageDigest.digest(key.getBytes("utf-8"));

                            return byte2hex(digest);

                   } catch (NoSuchAlgorithmException ex) {

                            Debug.error("Error digest algorithm: " + algorithm);

                   } catch (UnsupportedEncodingException e) {

                            Debug.error("Error digest algorithm: " + algorithm);

                   }

                   return null;

         }

         private static String byte2hex(byte[] b) {

                   StringBuilder hs = new StringBuilder();

                   String stmp = "";

                   for (int n = 0; n < b.length; n++) {

                            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));

                            if (stmp.length() == 1) {

                                     hs.append("0" + stmp);

                            } else {

                                     hs.append(stmp);

                            }

                   }

                  return hs.toString();

         }

红色部分其实就是:

messageDigest.update(plainText);

messageDigest.update(key.getBytes("utf-8"));

byte[] digest = messageDigest.digest();

可恶的是本人就没有学过JAVA,看了三遍的JDK说明,无解,

最后才看到了

SHA-1 例子:
         现在有i1、i2和i3,分别是3个字节数组,构成一个消息,计算其散列函数值:
MessageDigest sha1 = MessageDigest.getInstance(“sha-1”);
sha1.Update(i1);
sha1.Update(i2);
sha1.Update(i3);
byte[] hash = sha1.digest();
        对digest方法的调用则说明输入消息结束。进行初始化,update提交的数据丢失。Digest方法也可以把输入的最后一部分作为参数:

看到这里我真的想哭。原来就是字符串的一个叠加,搞得我想死。我一直以为这两个函数功能是不一样的。鬼才晓得后面这个

digest(byte[]),其实和Update是一样的。


sha1.Update(i1);
sha1.Update(i2);
byte[] hash = sha1.digest(i3);
         在一些散列函数实现中,可以通过复制(clone)来获得中间散列数值。如:
要分别计算:i1,i1和i2,i1、i2和i3的散列数值。
//计算i1 hash
sha1.update(i1);
byte[] i1Hash = sha1.clone().digest();
//计算i1和i2 hash
sha1.update(i2);
byte[] i12Hash = sha1.clone().digest();
//计算i1、i2和i3 hash
sha1.update(i3);
byte[] i123Hash = sha1.digest();

太感谢上苍了。

 1         //方法1:
 2 
 3         //16位
 4 
 5         public static string GetMd5(string str)
 6         {
 7             System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
 8             string a = BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str)), 48);
 9             a = a.Replace("-""");
10             return a;
11         }
12 
13         //32位 
15         public static string GetMd51(string str)
16         {
17             System.Security.Cryptography.MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
18             string a = BitConverter.ToString(md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(str)));
19             a = a.Replace("-""");
20             return a;
21         }
22 
23         //方法2:
24 
25         public static string Hash(string toHash)
26         {
27             MD5CryptoServiceProvider crypto = new MD5CryptoServiceProvider();
28             byte[] bytes = Encoding.UTF8.GetBytes(toHash);
29             bytes = crypto.ComputeHash(bytes);
30             StringBuilder sb = new StringBuilder();
31             foreach (byte num in bytes)
32             {
33                 sb.AppendFormat("{0:x2}", num);
34             }
35             return sb.ToString();        //32位
36             return sb.ToString().Substring(816);        //16位
37         }

Java加密出来的是900cb2d97c87b750fd1adafbb322fa45

C#加密出来的是:900cb2d97c87b750fd1adafbb322fa45 

请继续参考http://www.jeanwen.com/blog/page/82

写的很好。