C # HMAC SHA-256-128 Calculation result not as expected

I am trying to create a signature in our bank using the specified key, but my results do not match the information I received from the bank. Can anyone see what I am doing wrong?

Bank link for reference (Swedish text)

Sample data is inside character characters .. :)

Filedata: "00000000"

Key: "1234567890ABCDEF1234567890ABCDEF"

Expected Output: "FF365893D899291C3BF505FB3175E880"

My result is "05CD81829E26F44089FD91A9CFBC75DB"

My code:

        // Using ASCII teckentabell
        System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

        // Using HMAC-SHA256
        byte[] keyByte = encoding.GetBytes("1234567890ABCDEF1234567890ABCDEF");
        HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);

        byte[] messageBytes = encoding.GetBytes("00000000");
        byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);

        byte[] truncArray = new byte[16];
        Array.Copy(hashmessage, truncArray, truncArray.Length);

        // conversion of byte to string            
        string sigill = ByteArrayToString(truncArray);

        // show sigill
        MessageBox.Show("Sigill:\n" + sigill, "Sigill", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

      

+3


source to share


1 answer


Key

is a string of hexadecimal digits representing a binary key, not a string of individual characters.

For correct output, you need to convert it to a byte array:



var key = "1234567890ABCDEF1234567890ABCDEF";
byte[] keyByte = new byte[key.Length / 2];

for (int i = 0; i < key.Length; i += 2)
{
   keyByte[i / 2] = Convert.ToByte(key.Substring(i, 2), 16);
}

HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);

byte[] messageBytes = encoding.GetBytes("00000000");
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);

byte[] truncArray = new byte[16];
Array.Copy(hashmessage, truncArray, truncArray.Length);

      

+3


source







All Articles