Possible SHA256 return 128bytes after signing?

Asked

Viewed 756 times

2

I’m using the following code to get my certificate and sign my Cnpjs, but I’m using the SHA256 algorithm, but it’s returning 128bytes. Can anyone tell me what’s wrong? Follows the Code:

  Dim data = Encoding.UTF8.GetBytes(Me.txtCNPJSoftwareHouse.Text + Me.txtCNPJEmpresa.Text)
  Dim csp As RSACryptoServiceProvider = DirectCast(cert.PrivateKey, RSACryptoServiceProvider)


            ' cert = certificado X509
            Dim sha As New SHA256Managed()
            Dim hash As Byte() = sha.ComputeHash(data)
            Dim encrypted As Byte() = csp.SignHash(hash, CryptoConfig.MapNameToOID("SHA256"))



            Me.txtBox.Text = Convert.ToBase64String(encrypted)

The date variable is 28 bytes

The hash variable is 32 bytes

The Encrypted variable gets 128 bytes < - PROBLEM

At the end my txtBox.text receives only 172Bytes and should receive 344bytes if the variable "Encrypted" was receiving the 256bytes

  • 1

    And why do you think SHA256 will have 256 bytes? It has 256 bits. to tell you the truth I’m trying to understand how it got so branded. There is even the need for 64 bytes to represent hexadecimally, but not 128. Is it spending 2 bytes because it is UTF-16? Are bytes or characters? I will search. http://www.xorbin.com/tools/sha256-hash-calculator

  • Got it. Would you have any solution for this case what I might be doing to get this 344 character final result? thank you.

  • It depends on what you want to do. Why you need to reach this size?

  • Because this would be a key link between my system and S@t-fiscal Federal Revenue, they explain that the key signed with my ICP-Brazil (Digital Certificate) + the concatenation of Cnpjs (AC + Contributor) should be in total 344Chars with Base64

  • Isn’t there something missing? It doesn’t seem to be the problem of the SHA. Or do you have any?

  • Here is the PDF provided by the Recipe:http://www.fazenda.sp.gov.br/sat/downloads/Especificacao_SAT_v_ER_2_11_4.pdf In paragraph 2.1.3 Pag. 15 he explains.

  • 1

    Encrypted is not an SHA256... It is an RSA using SHA... SHA is a Hashing mechanism... RSA encryption.

  • Daniloloko, I understood and what would be the solution to this? Thanks

  • 1

    The certificate is a correct . pfx file or a key? I will make an example in c#

  • So Danilo, is an A3 card, with 1 private key and another publish, and yes when exported it can be a file . pfx But I have the function to locate the Certificate Connected on my micro, in case the function searches the PRIVATE KEY! Can do, then I pass to Vb,rs THANKS!

  • @Felipewalleg Take a look at [tour]. You can accept an answer if it solved your problem. You can vote for all the posts on the site as well. Did any help you more? You need something to be improved?

Show 6 more comments

2 answers

2

The SHA-256 is 256 bits and not 256 bytes. I’m trying to understand how it got so big.

256 bits are 32 bytes. There is even a need for 64 bytes, or more precisely 64 characters, after all each byte will need two digits to hexadecimal.

I have no idea where the 128 bytes are coming from. Only if it’s a problem of encoding and is using UTF-16 which is the standard of string for . NET and occupying 2 bytes by character.

  • I understood, but let’s face it, once he passed through here " Dim hash As Byte() = sha.Computehash(data)" He was supposed to generate 256, not 128. Here it only provides UTF-7 / UTF-8 and UTF-32

  • 1

    but will not Me.txtBox.Text isn’t converting to UTF-16? Just an assumption, without seeing it can’t be very sure.

  • Look I hadn’t thought about it, and it really makes sense. I’ll check... Want me to post something to help you? It’s just that I’m already breaking my head with this code and finally I’m close to finishing,rs

1


I’ll just start a discussion of what might be going on...

 Dim data = Encoding.UTF8.GetBytes(Me.txtCNPJSoftwareHouse.Text + Me.txtCNPJEmpresa.Text)
 Dim csp As RSACryptoServiceProvider = DirectCast(cert.PrivateKey, RSACryptoServiceProvider)

 //vou continuar o exemplo em c#(não entendo muito vb.net)

 byte[] signature = csp.SignData(data, "SHA256");
 bool isValid = csp.VerifyData(data, "SHA256", signature);//verifica se e valido

//Conversao para UTF8 caso for necessario ser lido...
string final = Encoding.UTF8.GetString(signature);

A link that can help you

You can use Cryptoconfig.Mapnametooid("SHA256") instead of "SHA256", but I don’t know what difference will cause.

  • Opáá á! I’m going to go to VB here!! So I use Cryptoconfig.Mapnametooid("SHA256") However it is bringing the 32 Chars, but in case we take these 32(byte) * 8(bit), I get the final result. But I will convert here to VB.net! Thanks Even!

  • I only used the convention that is on the link in the answer... I do not know if it will work because the recipe conversation may have some particularity... But nothing we’re going to find out :D

  • rsrs So, Danilo. It would be funny if it wasn’t tragic,rs I did the code conversion and I managed to get to the same encrypted key I could before. E gave the same thing! byte[] Signature = csp.Signdata(date, "SHA256"); = 128Chars =[

  • 1

    isValid true return... try debugging and see if the data is right really... final string = Encoding.UTF8.Getstring(Signature);

  • So, is returning TRUE, I’m starting to think that the problem is my A3 certificate, because I saw some users talking about this key made in A1 and got the final result of 344chars

  • The Signac signature does not need to be exactly 344 characters, it is 1 to 344... the signatureQRCOD yes! (pag 55) This size is the key that was passed to the RSA... the final size is always based on the KEY (http://stackoverflow.com/questions/6658728/rsa-signature-size). I mean... try to make it right!

  • So, 172chars that is being generated is correct ? But even so I think I miss the 256 generate only the 128chars. But blz, Thanks so much for now, Danilo!!

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.