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
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
– Maniero
Got it. Would you have any solution for this case what I might be doing to get this 344 character final result? thank you.
– Felipe S
It depends on what you want to do. Why you need to reach this size?
– Maniero
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
– Felipe S
Isn’t there something missing? It doesn’t seem to be the problem of the SHA. Or do you have any?
– Maniero
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.
– Felipe S
Encrypted is not an SHA256... It is an RSA using SHA... SHA is a Hashing mechanism... RSA encryption.
– Danilo Breda
Daniloloko, I understood and what would be the solution to this? Thanks
– Felipe S
The certificate is a correct . pfx file or a key? I will make an example in c#
– Danilo Breda
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!
– Felipe S
@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?
– Maniero