You are not generating Base64 output from the Android code, as is
blindingly obvious by looking at your output and seeing that it is not
Base64-encoded. I have no idea if EncodingUtils is supposed to support
Base64 -- that's not documented.

On Fri, Jun 3, 2011 at 3:10 PM, [email protected]
<[email protected]> wrote:
> I'm using the following Android code to generate a HmacMD5 hash.  When
> I generate the hash from the Android App I get the following:
> J0t��j#߸� bq- �.  When I generate the hash from a .net console
> application I get: SjB0j+xqI9+4hxticS0Cjw==.  Notice the difference
> and that the Android has has special chars in it.  Is this a character
> set issue?
>
> Aaron
>
> ==============================================================
> Here's the Android code:
>
> public class HMACMD5 {
>
>   private final String HMAC_MD5_NAME = "HmacMD5";
>
>   private SecretKeySpec sk;
>   private Mac mac;
>
>   public HMACMD5(byte[] key) throws GeneralSecurityException {
>      init(key);
>   }
>
>   public HMACMD5(String key) throws GeneralSecurityException {
>      init(EncodingUtils.getAsciiBytes(key));
>   }
>
>   private void init(byte[] key) throws GeneralSecurityException {
>      sk = new SecretKeySpec(key, HMAC_MD5_NAME);
>      mac = Mac.getInstance(HMAC_MD5_NAME);
>      mac.init(sk);
>   }
>
>   public byte[] ComputeHash(byte[] data) {
>      return mac.doFinal(data);
>   }
>
>   public byte[] ComputeHash(String data) {
>      return ComputeHash(EncodingUtils.getAsciiBytes(data));
>   }
> }
>
> public String encodeText(String sKey, String sSrc) throws Exception {
>      HMACMD5 hmacMD5 = new HMACMD5(sKey);
>      byte[] textBytes = EncodingUtils.getBytes(sSrc, "UTF-8");
>      byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
>      String sEncodedText = EncodingUtils.getString(encodedTextBytes,
> "BASE64");
>      return sEncodedText;
> }
>
> ==============================================================
> Here's the .NET code:
>
> public static string EncodeText(byte[] key, string sText, Encoding
> encoding)
> {
>   HMACMD5 hmacMD5 = new HMACMD5(key);
>   byte[] textBytes = encoding.GetBytes(sText);
>   byte[] encodedTextBytes = hmacMD5.ComputeHash(textBytes);
>   string sEncodedText = Convert.ToBase64String(encodedTextBytes);
>   return sEncodedText;
> }
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>



-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://github.com/commonsguy
http://commonsware.com/blog | http://twitter.com/commonsguy

_The Busy Coder's Guide to *Advanced* Android Development_ Version
1.9.3 Available!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to