Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8779287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:44:57+00:00 2026-06-13T19:44:57+00:00

I have the following code in Java: byte[] secretKey = secretAccessKey.getBytes(UTF-8); SecretKeySpec signingKey =

  • 0

I have the following code in Java:

byte[] secretKey = secretAccessKey.getBytes("UTF-8");
SecretKeySpec signingKey = new SecretKeySpec(secretKey, "HmacSHA256");
Mac mac = Mac.getInstance("HmacSHA256");
mac.init(signingKey);
byte[] bytes = data.getBytes("UTF-8");
byte[] rawHmac = mac.doFinal(bytes);
String result = javax.xml.bind.DatatypeConverter.printBase64Binary(rawHmac);

and the following code in C#:

UTF8Encoding enc = new UTF8Encoding();
byte[] secretKey = enc.GetBytes(secretAccessKey);
HMACSHA256 hmac = new HMACSHA256(secretKey);
hmac.Initialize();
byte[] bytes = enc.GetBytes(data);
byte[] rawHmac = hmac.ComputeHash(bytes);
string result = Convert.ToBase64String(rawHmac);

The byte arrays “secretKey” and “bytes” are equivalent but the byte array “rawHmac” is different, and the string “result” is different. Can anyone see why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:44:59+00:00Added an answer on June 13, 2026 at 7:44 pm

    Don’t do this:

    byte[] bytes = data.getBytes();
    

    That will use the platform default encoding to convert a string to a byte array. That can vary between platform, whereas you want something repeatable. I would suggest UTF-8:

    byte[] bytes = data.getBytes("UTF-8");
    

    (Do the same for the key, of course.)

    You should then use the same encoding in your C# – not ASCII, unless you really want to not handle non-ASCII characters.

    byte[] bytes = Encoding.UTF8.GetBytes(data);
    

    It’s also not clear how you’re comparing the results afterwards – don’t forget that byte is signed in Java, but unsigned in C#. It’s probably simplest to convert the hash to hex or base64 for comparison purposes.

    EDIT: I strongly suspect the last part was the problem – comparing the results.

    Here are two short but complete programs (using the iharder.net base64 converter in Java) which produce the same base64 output:

    Java:

    import java.util.*;
    import javax.crypto.*;
    import javax.crypto.spec.*;
    
    public class Test {
        public static void main (String[] args) throws Exception {
            String secretAccessKey = "mykey";
            String data = "my data";
            byte[] secretKey = secretAccessKey.getBytes();
            SecretKeySpec signingKey = new SecretKeySpec(secretKey, "HmacSHA256");
            Mac mac = Mac.getInstance("HmacSHA256");
            mac.init(signingKey);
            byte[] bytes = data.getBytes();
            byte[] rawHmac = mac.doFinal(bytes);
            System.out.println(Base64.encodeBytes(rawHmac));
        }
    }
    

    C#:

    using System;
    using System.Security.Cryptography;
    using System.Text;
    
    class Test
    {
        static void Main()
        {
            String secretAccessKey = "mykey";
            String data = "my data";
            byte[] secretKey = Encoding.UTF8.GetBytes(secretAccessKey);
            HMACSHA256 hmac = new HMACSHA256(secretKey);
            hmac.Initialize();
            byte[] bytes = Encoding.UTF8.GetBytes(data);
            byte[] rawHmac = hmac.ComputeHash(bytes);
            Console.WriteLine(Convert.ToBase64String(rawHmac));
        }
    }
    

    Output from both:

    ivEyFpkagEoghGnTw/LmfhDOsiNbcnEON50mFGzW9/w=
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following Java code: byte[] signatureBytes = getSignature(); String signatureString = new
I have the following Java code: final Future future = exeService.submit( new Runnable() {
If I have the following Java code: int[][] readAPuzzle() { Scanner input = new
I have the following code adding an ActionListener to a JTextField: chatInput.addMouseListener(new java.awt.event.MouseAdapter() {
I have the following Java code: public static BufferedImage createImage(byte[] data, int width, int
I have the following Java code: byte value = 0xfe; // corresponds to -2
i have written the following code to download file. java.io.BufferedInputStream in = new java.io.BufferedInputStream(new
I have the following java code: import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement;
I have following Code package cyclist.project; import java.util.ArrayList; import java.util.List; import org.json.JSONArray; import org.json.JSONException;
Say suppose I have the following Java code. public class Example { public static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.