【发布时间】:2011-02-15 00:07:26
【问题描述】:
我同时在 android 和 c# 中进行 md-5 散列。但是对于相同的输入,两个结果应该是相同的。两种语言的处理方式有什么不同吗?
在这两种情况下我得到不同的输出。这是md-5计算的c#代码:
//this method hashes the values sent to it using MD5
public static String hashwithmd5(String toHashMD5)
{
byte[] keyArray;
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(toHashMD5));
hashmd5.Clear();
return Convert.ToBase64String(keyArray, 0, keyArray.Length);
}
这是在android中使用bouncycastle的md5代码
public byte[] Hashing(String toHash) throws Exception{
byte[] hashBytes = toHash.getBytes("UTF-8");
EditText et = (EditText) findViewById(R.id.entry);
org.bouncycastle.crypto.digests.MD5Digest digest = new org.bouncycastle.crypto.digests.MD5Digest();
digest.reset();
digest.update(hashBytes, 0, hashBytes.length);
int length = digest.getDigestSize();
byte[] md5 = new byte[length];
digest.doFinal(md5, 0);
et.setText(md5.toString());
return md5;
}
c#中md5的结果是:XUFAKrxLKna5cZ2REBfFkg==
android中md5的结果是:[B@4053cf40
【问题讨论】:
-
谁能告诉我这两种计算有什么不同,以及我应该如何改变它以获得相同的 o/p?
-
如果你比较字节数组呢?它们是一样的吗?
-
@cris 你能得到解决方案吗?"