Error when signing a SHA256 Hash using Digital Certificate

Asked

Viewed 4,420 times

4

Is returning error from Invalid algorithm Specified, when it will be signed, how to proceed?

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

 Dim sha As SHA256 = SHA256Managed.Create()
 Dim hash As Byte() = New Byte() {}
 hash = sha.ComputeHash(data)

 Dim encrypted As Byte() = csp.SignHash(hash, "SHA256")
  • Which line returned error? Which Exception returned (not just the message)? Which version of . Net used?

  • On the signature line, when will you add the key in the Encrypted variable, VS12

  • Are you sure this is how you sign? SHA-256 is a hash algorithm, not a digital signature, but it is used on other signature algorithms, such as the HMAC-SHA256 (although you wouldn’t believe it to be your case). You wouldn’t be looking for the RSAPKCS1SignatureFormatter? Behold that example. It’s very common - when you want to sign an arbitrary data - first hash this data and then sign the hash, this is used in several protocols, I just don’t know if it applies to your case.

  • So when I use the SHA1 standard, changing only the classes, sure, but when I use SHA256 of this error, because all the researches I did the guys uses this model that I mentioned above, not to mention that besides I have to generate in SHA256 I have to sign using my digital certificate.

  • @Felipewalleg The about the digital certificate, would be the new version of certificates? There are certificates chain v2 and v3. If I’m not mistaken, this month or last month have changed the chain of digital certificates. Check if the certificate is G2,G5... G7, because the version G7(V3) are having some problems giving error in the validation of it. I don’t know if it involves the same problem with your signature, but it follows one of the problems I’ve had around here about signatures and have been solved, If that is, I put as answer the resolution of the problem.

3 answers

1

There are several possible causes; but the most common is when the certificate issued does not support the desired signature algorithm.

To check, open the certificate and select Detalhes. The following item must be present:

inserir a descrição da imagem aqui

If necessary, re-issue the certificate with signature algorithm support SHA256.

  • 1

    Ono, is thus mine, same as the image you posted, even so returns the invalid algorithm error Specified

0

Look at this simple example, running SHA256 directly in a text, debug the methods to add the certificate, in case, the certificate id right? to use as a keyword.

Private Function Encriptar(ByVal TextoEncriptar As String) As String
    Dim TextoEncriptado As String
    Dim TextoBytes() As Byte

    'Saber os Bytes do texto a encriptar
    TextoBytes = System.Text.Encoding.Unicode.GetBytes(TextoEncriptar)

    'Nova Instancia SHA256
    Dim HashSha As New SHA256Managed

    'Calcular hash do texto em bytes
    TextoBytes = HashSha.ComputeHash(TextoBytes)

    'Converter o array de bytes 
   TextoEncriptado  = Convert.ToBase64String(TextoBytes)


    Return TextoEncriptado 
  End Function

http://www.portugal-a-programar.pt/topic/18165-criptografia/

  • So, but if you notice this method does not have the signature, this I have working here, but the problem is when you add Signhash or Signdata, both give this invalid algorithm error

0

Try it like this.

Dim csp As SHA256 = Directcast(Certificate.Privatekey, Sha256cryptoserviceprovider)

I can’t test it now, but I think it’s conflicting with the application RSA x SHA256.

  • And what you suggest to me?

  • good guy, try using this class, X509 Certificates, I use in xmls to sign https://msdn.microsoft.com/pt-br/library/system.security.cryptography.x509certificates.x509certificate(v=vs.110). aspx

Browser other questions tagged

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