经典 asp 使用 capicom 进行 md5 哈希 - 结果与 .net System.Security.Cryptography 不同
当在经典 ASP (VBScript) 中使用 CAPICOM 执行 MD5 哈希时,如下所示:
With server.CreateObject("CAPICOM.HashedData")
.Algorithm = 3 ' CAPICOM_HASH_ALGORITHM_MD5
.Hash "password"
md5Pwd = .Value
End With
我得到以下结果: B081DBE85E1EC3FFC3D4E7D0227400CD
当我使用 .NET 时,我得到以下结果: 5f4dcc3b5aa765d61d8327deb882cf99
为什么MD5字符串不同?我做错了什么?
这是我的 C# 函数:
MD5 md5Hasher = MD5.Create();
byte[] data = md5Hasher.ComputeHash( Encoding.Default.GetBytes( val ) );
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for( int i = 0; i < data.Length; i++ ) {
sBuilder.Append( data[i].ToString( "x2" ) );
}
// Return the hexadecimal string.
return sBuilder.ToString();
When using CAPICOM in Classic ASP (VBScript) to perform MD5 hashing like so:
With server.CreateObject("CAPICOM.HashedData")
.Algorithm = 3 ' CAPICOM_HASH_ALGORITHM_MD5
.Hash "password"
md5Pwd = .Value
End With
I get this result: B081DBE85E1EC3FFC3D4E7D0227400CD
When I use .NET, I get this result: 5f4dcc3b5aa765d61d8327deb882cf99
Why are the MD5 strings different? What am I doing wrong?
Here is my C# function:
MD5 md5Hasher = MD5.Create();
byte[] data = md5Hasher.ComputeHash( Encoding.Default.GetBytes( val ) );
StringBuilder sBuilder = new StringBuilder();
// Loop through each byte of the hashed data
// and format each one as a hexadecimal string.
for( int i = 0; i < data.Length; i++ ) {
sBuilder.Append( data[i].ToString( "x2" ) );
}
// Return the hexadecimal string.
return sBuilder.ToString();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您使用的
Encoding.Default
编码 代表 7 位 ASCII 字符。同时,“CAPICOM 在验证和生成数字签名时仅操作 Unicode 字符串 ”。因此,Encoding.Default.GetBytes 处理一字节字符(顺便丢失任何非 ASCII 数据),而 CAPICOM.HashedData 将它们视为 2 字节 Unicode 字符。
将
Encoding.Default
替换为Encoding.Unicode
以使您的 .NET 实现与 CAPICOM 兼容。另请注意,使用
data[i].ToString("X2")
生成大写结果,就像在 CAPICOM 实现中一样。The problem is that you are using
Encoding.Default
encoding which represents 7 bit ASCII characters. At the same time, “CAPICOM manipulates only Unicode strings while validating and generating digital signatures”.So,
Encoding.Default.GetBytes
deals with one-byte characters (losing any non-ASCII data by the way), while CAPICOM.HashedData treats them as 2-byte Unicode characters.Replace
Encoding.Default
withEncoding.Unicode
to make your .NET implementation to be compatible with CAPICOM.One more note, use
data[i].ToString("X2")
to produce upper-case result, as you have in CAPICOM implementation.