In this VB.Net code I sign a test string and then verify the signature. When I use the same cryptoProviderRSA variable for both signing and verifying, the signature is verified successfully. But if I try to create another cryptoProviderRSA variable (it is called cryptoProviderRSA_2 in the code), the VerifySignature keeps returning False. This sample code is pretty simple, yet I don't see what makes cryptoProviderRSA_2 different from cryptoProviderRSA, so the signature verification fails. Any help is appreciated, thanks!
Private Sub SignAndVerifyString(testStringToSign As String)
'---- sign string
Dim testByteArrayToSign As Byte() = Encoding.ASCII.GetBytes(testStringToSign)
Dim hashAlgorithm As New SHA1Managed
Dim hashValue As Byte() = hashAlgorithm.ComputeHash(testByteArrayToSign)
Dim cryptoProviderRSA As New RSACryptoServiceProvider
Dim signatureFormatterRSA As New RSAPKCS1SignatureFormatter(cryptoProviderRSA)
signatureFormatterRSA.SetHashAlgorithm("SHA1")
Dim signatureByteArray As Byte() = signatureFormatterRSA.CreateSignature(hashValue)
'----- verify string
Dim cryptoProviderRSA_2 As New RSACryptoServiceProvider
'--- When this line is used, VerifySignature below returns True
Dim signatureDeFormatterRSA As New RSAPKCS1SignatureDeformatter(cryptoProviderRSA)
'--- When this line is used, VerifySignature below returns False
'Dim signatureDeFormatterRSA As New RSAPKCS1SignatureDeformatter(cryptoProviderRSA_2)
signatureDeFormatterRSA.SetHashAlgorithm("SHA1")
signatureDeFormatterRSA.VerifySignature(hashValue, signatureByteArray)
End Sub