RSA Encryption error

S

Scott Hamlin

I have been experimenting with encryption, but I am having a problem copying
the array of bytes into a Session variable and decrypting. For the following
code, the Page_Load works fine - encrypts the data, then decrypts the
message fine. When I click Button1, I received the message:
______________________
Exception Details: System.Security.Cryptography.CryptographicException:
Error occurred while decoding OAEP padding.

Source Error:

Line 61:
Line 62: 'Decrypt the plaintext
Line 63: Dim x_New_plaintext() As Byte = x_alg.Decrypt(x_ciphertext,
True)
Line 64:
Line 65: 'Print the message

Source File: C:\Inetpub\wwwroot\Research\Security\RSAEncrypt.aspx.vb
Line: 63
___________________

Here is the code. Any help is much appreciated!
____________________
Imports System.Security.Cryptography
Imports System.Text
....
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim x_plaintext As Byte() = Encoding.Default.GetBytes("Scott is
cool")

'Create an instance of the RSA algorithm
Dim x_alg As RSACryptoServiceProvider = New RSACryptoServiceProvider

'Encrypt the plaintext using OAEP padding
Dim x_ciphertext() As Byte = x_alg.Encrypt(x_plaintext, True)

'Print the encrypted data
Response.Write("<br>Encrypted value = ")
Dim MyByte As Byte
For Each MyByte In x_ciphertext
Response.Write(MyByte & " ")
Next

'Decrypt the plaintext
Dim x_New_plaintext() As Byte = x_alg.Decrypt(x_ciphertext, True)

'Print the message
Response.Write("<br>Descrypted message is " &
Encoding.Default.GetString(x_New_plaintext))

'Store in a Session variable
Session("Encryption") = x_ciphertext
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Get the encrypted data
Dim x_ciphertext() As Byte = CType(Session("Encryption"), Byte())

Dim x_alg As RSACryptoServiceProvider = New RSACryptoServiceProvider

'Decrypt the plaintext
Dim x_New_plaintext() As Byte = x_alg.Decrypt(x_ciphertext, True)

'Print the message
Response.Write("<br>Descrypted message is " &
Encoding.Default.GetString(x_New_plaintext))
End Sub
________________________
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top