Android:如何创建 HMAC MD5 字符串?
我正在尝试创建一个 android MD5 哈希字符串以等于下面的 C# 代码:
private string CalculateHMACMd5(string message, string key)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(key);
HMACMD5 hmacmd5 = new HMACMD5(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = hmacmd5.ComputeHash(messageBytes);
string HMACMd5Value = ByteToString(hashmessage);
return HMACMd5Value;
}
private static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2");
}
return (sbinary);
}
Android code that I currently use [not generating the same C# code]:
public static String sStringToHMACMD5(String sData, String sKey)
{
SecretKeySpec key;
byte[] bytes;
String sEncodedString = null;
try
{
key = new SecretKeySpec((sKey).getBytes(), "ASCII");
Mac mac = Mac.getInstance("HMACMD5");
mac.init(key);
mac.update(sData.getBytes());
bytes = mac.doFinal(sData.getBytes());
StringBuffer hash = new StringBuffer();
for (int i=0; i<bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
sEncodedString = hash.
return sEncodedString;
}
提前致谢。
I am trying to create an android MD5 hash string to equal the C# code bellow:
private string CalculateHMACMd5(string message, string key)
{
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] keyByte = encoding.GetBytes(key);
HMACMD5 hmacmd5 = new HMACMD5(keyByte);
byte[] messageBytes = encoding.GetBytes(message);
byte[] hashmessage = hmacmd5.ComputeHash(messageBytes);
string HMACMd5Value = ByteToString(hashmessage);
return HMACMd5Value;
}
private static string ByteToString(byte[] buff)
{
string sbinary = "";
for (int i = 0; i < buff.Length; i++)
{
sbinary += buff[i].ToString("X2");
}
return (sbinary);
}
Android code that I currently use [not generating the same C# code]:
public static String sStringToHMACMD5(String sData, String sKey)
{
SecretKeySpec key;
byte[] bytes;
String sEncodedString = null;
try
{
key = new SecretKeySpec((sKey).getBytes(), "ASCII");
Mac mac = Mac.getInstance("HMACMD5");
mac.init(key);
mac.update(sData.getBytes());
bytes = mac.doFinal(sData.getBytes());
StringBuffer hash = new StringBuffer();
for (int i=0; i<bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
hash.append('0');
}
hash.append(hex);
}
sEncodedString = hash.
return sEncodedString;
}
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
定义“不工作”。例外?输出不符合预期?等。
一个明显的事情是您正在处理相同的数据两次:
要一次性处理所有数据,只需使用
doFinal()
(假设它不是太大)。另一件可能出错的事情是密钥的格式:
String sKey
的格式是什么。理想情况下,您应该使用 BASE64 编码的字符串,而不是调用getString()
。Define 'not working'. Exception? Output not as expected?, etc.
One obvious thing is that you are processing the same data twice:
To process all data in one pass, just use
doFinal()
(assuming it's not too big).Another thing that can be wrong is the format of the key: what is the format of
String sKey
. Ideally you should be using a BASE64 encoded string, not calls togetString()
.