2 years ago

#13318

test-img

Alven2

Verify failing with RSA VB.net

i have been working with an piece of code, to get it to work. I am using the Microsoft RSA Class I have gotten most of it to work, signing, encrypt, decrypt, but not the verify, it always says false, i can decrypt it, so i am a bit lost. Naturally i don't really want to use both keys for the verify part, but i am just attempting to understand it all. Can somebody tell me what i am doing wrong? So that i can get it working?

Imports System.Security.Cryptography
Imports System.Text
Public Class Generator

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ButtonEncrypt.Click
        Dim encoder As New UTF8Encoding
        Dim rsa As RSA
        Dim textbytes, encryptedtextbytes, signature As Byte()
        'set key size to 2048 bits
        rsa = RSA.Create()
        rsa.KeySize = 2048

        'get textbox value
        Dim TexttoEncrypt As String = TextBoxSource.Text

        'Use UTF8 to convert the text string into a byte array
        textbytes = encoder.GetBytes(TexttoEncrypt)

        'encrypt the text
        encryptedtextbytes = rsa.Encrypt(textbytes, RSAEncryptionPadding.Pkcs1)
        signature = rsa.SignData(textbytes, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1)

        'Convert the encrypted byte array into a Base64 string for display purposes
        TextBoxEncrypted.Text = Convert.ToBase64String(encryptedtextbytes)
        TextBoxSignature.Text = Convert.ToBase64String(signature)

        'Create an RSAParameters type
        Dim Parameters As New RSAParameters
        'export the key and other algorithm values
        Parameters = rsa.ExportParameters(True)
        'display the private key. Base64 encode the text to make display easier
        TextBoxPrivate.Text = rsa.ToXmlString(True)
        TextBoxPublic.Text = rsa.ToXmlString(False)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonDecrypt.Click
        Dim encoder As New UTF8Encoding
        Dim rsa As RSA
        'get the decrypted clear text byte array
        Dim data As Byte() = Convert.FromBase64String(TextBoxEncrypted.Text)
        rsa = RSA.Create()
        rsa.KeySize = 2048
        rsa.FromXmlString(TextBoxPublic.Text)
        rsa.FromXmlString(TextBoxPrivate.Text)

        Dim textbytes As Byte() = rsa.Decrypt(data, RSAEncryptionPadding.Pkcs1)
        'Dim out As Boolean = rsa.VerifyData(encryptedtextbytes, signature, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1)
        'convert the byte array to text using the same encoding format that was used for encryption
        TextBoxDecrypted.Text = encoder.GetString(textbytes)

        Dim signatureBytes As Byte() = Convert.FromBase64String(TextBoxSignature.Text)
        rsa = RSA.Create()
        rsa.KeySize = 2048
        rsa.FromXmlString(TextBoxPublic.Text)
        rsa.FromXmlString(TextBoxPrivate.Text)
        Dim out As Boolean = rsa.VerifyData(data, signatureBytes, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1)
        TextBoxOutput.Text = out

    End Sub

End Class

vb.net

rsa

verify

0 Answers

Your Answer

Accepted video resources