title: Authenticating (matching) tokens from Java to C#
body: <p>I am trying to make my Java program generate a signature by using a message and a secret key.  My C# program should generate the same token by using the same message and secret key.  </p>
  
  <p>However, something isn't working properly because the tokens that get generated are different.  </p>
  
  <p>Can someone spot out what the difference is between these two programs and why they aren't generating matching keys?</p>
  
  <p>Java Code:</p>
  
  <pre><code>this.algorithm = "hmacSHA256";
  private static Mac mac;
  String message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum id urn";
  String secretKey = "5771CC06-B86D-41A6-AB39-9CA2BA338E27";
  
  if( mac == null ) {
      mac = Mac.getInstance(algorithm);
      SecretKeySpec secret = new SecretKeySpec(secretKey.getBytes("US-ASCII"), 
          mac.getAlgorithm());
      mac.init(secret);
  }
  this.signature = new String(Base64.encodeBase64(mac.doFinal(message.getBytes("US-ASCII"))));
  </code></pre>
  
  <p>C# Code:</p>
  
  <pre><code>string message = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum id urn";
  string secret = "5771CC06-B86D-41A6-AB39-9CA2BA338E27";
  
  secret = secret ?? "";
  var encoding = new System.Text.ASCIIEncoding();
  byte[] keyByte = encoding.GetBytes(secret);
  byte[] messageBytes = encoding.GetBytes(message);
  using (var hmacsha256 = new System.Security.Cryptography.HMACSHA256(keyByte))
  {
      byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
      return Convert.ToBase64String(hashmessage);
  }
  </code></pre>