Error when signing a Hash on SHA256 using Digital Certificate

Posted on

Question :

Returning Invalid Algorithm Specified error, when will it 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")


Answer :

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

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

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


Try this.

Dim csp As SHA256 = DirectCast (Certificate.PrivateKey, SHA256CryptoServiceProvider)

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


Look at this simple example, by running the SHA256 directly in a text, debug the methods to add the certificate, in this case, the right certificate id? 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




Leave a Reply

Your email address will not be published. Required fields are marked *