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


Question: 

I'm trying to create a signature to our bank from a specified key but my results is not the same as the info I got from the bank. Can anyone see what I am doing wrong?

Link to bank for reference (text in Swedish)

Example data are inside the citationmarks .. :)

Filedata: "00000000"

Key: "1234567890ABCDEF1234567890ABCDEF"

Expected result: "FF365893D899291C3BF505FB3175E880"

My result: "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);



1 Answer: 

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

For the correct output you need to convert it to an array of bytes:

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

More Articles


data structures - Is there any practical usage of Doubly Linked List, Queues and Stacks?

I've been coding for quite sometime now. And my work pertains to solving real-world business scenarios. However, I have not really come across any practical usage of some of the data structures like the Linked List, Queues and Stacks etc.Not even at the business framework level. Of course, there is

php - Using json_decode on cURL result

I have got the following code that retrieves data about a company.function getSSLPage($url) { $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_SSLVERSION,1); curl_setopt($ch, CURLOPT_SSL_VERI

c - Bypass default rule for make

I'm trying to compile several .c files through assembler:%.S: %.c $(XCC) -S -o $@ $(XCFLAGS) -c $<%.o: %.S $(XCC) -o $@ $(XCFLAGS) -c $<test.a: test.o foo.o $(LD) -o $@ $^ $(XLDFLAGS) $(XCC) is a cross-compile tool.Actually, I see: cc -c -o test0.o test0.ccc -c -o f


ubuntu - gstreamer rtp streaming webcam

im trying to stream my webcam using OpenCV and gstreamer... for this first i test using the command line with this:gst-launch v4l2src ! ffmpegcolorspace ! theoraenc ! rtptheorapay ! udpsink host=localhost port=5000 sync=false -vThen i try to see the streaming using this command line:gst-launch udpsr

php - the best implementation of RFC 2104 HMAC-SHA1

What is the best implementation of RFC 2104 HMAC-SHA1 alg. in php?I have read http://us2.php.net/manual/en/function.hash-hmac.php but when I try them I getdifferent results.Thank's

git - How to use libgit2sharp with ssh-transport-protocol?

When I use libgit2sharp in project to clone repository with ssh-transport protocol, like git@github.com:libgit2/libgit2sharp.git It throw an exception, says "This transport isn't implemented. Sorry"How can I clone repository with ssh-transport-protocol by using libgit2sharp ?


android - Mock location not working on Google map

I have used code from this. I have changed it a bit. Below is my code snippet. The problem is Google Map is not showing proper location which i have mocked.public class MockGpsProviderActivity extends Activity implements LocationListener {public static final String LOG_TAG = "MockGpsProviderActivity

php - Laravel openssl_private_encrypt(): key param is not a valid private key

I am trying to connect to Chef API with Laravel using PHP-Chef. I have tried to set up my chef config with the data that I got from knife.rb in .chef folder. I have setup client and a key according to the instructions from Knife.rb. But I get:openssl_private_encrypt(): key param is not a valid priva

parsing - Parse JQ output through external bash function?

I want to parse out data out of a log file which consist of JSON sting and I wonder if there's a way for me to use a bash function to perform any custom parsing instead of overloading jq command.Command:tail errors.log --follow | jq --raw-output '. | [.server_name, .server_port, .request_file] | @ts

Know of any setup tutorials for SQL Server Express and C#?

I'm a C# and MySQL developer, but I'm looking into Microsoft's SQL Server for a new project. I'm familiar with MySQL syntax and the .NET connector. Does anyone know of any decent tutorials that just cover the code to interact with SQL Server? No C# basics or SQL basics, just how to get the two tec