2011-12-06 6 views
5

私は怒鳴るC#コードを等しくするには、AndroidのMD5ハッシュ文字列を作成しようとしています:Android:HMAC MD5文字列を作成するには?

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のコードを[ は同じC#のコードを生成しない]:

 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; 
     } 

ありがとうございます。

+0

アンドロイドコードを確認してください。 – Basbous

+0

[HMAC MD5をどのようにしてAndroidに生成するのですか?](http://stackoverflow.com/questions/3140650/how-to-generate-hmac-md5-in-android) – Thilo

+1

@Thilo:あなたのリンクを確認しました私の自己を提供し、解決策は動作していません。 –

答えて

6

「動作していません」と定義します。例外?出力は期待できないと?など

1つの明らかな事はあなたが二度同じデータを処理しているということです。

mac.update(sData.getBytes()); 
bytes = mac.doFinal(sData.getBytes()); 

1回のパスですべてのデータを処理するには、ちょうどdoFinal()を使用します(それは大きすぎるではありませんと仮定します) 。 もう1つ間違っているのは、キーのフォーマットです:String sKeyという形式は何ですか。理想的には、getString()への呼び出しではなく、BASE64でエンコードされた文字列を使用する必要があります。

+0

私はあなたが正しいと思っています。ヒントのために私の解決策をチェックしてください。 – Basbous

+0

両方のプログラムで 'byte [] keyByte'をダンプ(またはデバッガを使用)して、バイトが同じであることを確認してください。 'byte [] messageBytes'と同じことをします。これらがすべて一致していれば、 'byte [] hashmessage'も同様ですが、それもチェックしてください。一致した場合、エラーは最後の16進数エンコーディングパートにあります。 –

15
public static String sStringToHMACMD5(String s, String keyString) 
    { 
     String sEncodedString = null; 
     try 
     { 
      SecretKeySpec key = new SecretKeySpec((keyString).getBytes("UTF-8"), "HmacMD5"); 
      Mac mac = Mac.getInstance("HmacMD5"); 
      mac.init(key); 

      byte[] bytes = mac.doFinal(s.getBytes("ASCII")); 

      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.toString(); 
     } 
     catch (UnsupportedEncodingException e) {} 
     catch(InvalidKeyException e){} 
     catch (NoSuchAlgorithmException e) {} 
     return sEncodedString ; 
    } 
+0

例外を無視しないでください。それらを記録する。 – siamii

+0

私は上記のコードを減らすために例外を削除します。 – Basbous

関連する問題