therealtomlapp
Member
- Joined
- Apr 29, 2008
- Messages
- 12
- Programming Experience
- 10+
"Bad Data" or "Error occurred while decoding OAEP padding" using RSA encryption
I'm trying to implement encryption using the RSACryptoServiceProvider. I've already created a key pair, the public key will be exported to other machines so they can encrypt data, and the private key is stored locally in a key container.
Anyhow i've tried just to get the basic encryption/decryption going, i've tried loading the keys either from the key store, or just loading them using the fromxml function.
If I turn OAEP padding on (which I should be able to use since my machine is xp pro) I get the following error while decrypting:
Error occurred while decoding OAEP padding
of I turn OAEP padding off I get this
Bad Data
Here is my code
The info encrypts with no problem, just won't decrypt. I have scoured google for the answer to this problem and haven't found an answer. It is very important that I am able to export the public key to xml format so that it can be distributed to the app on other machines. If anyone has any ideas as to what im doing wrong please help, thank you.
I'm trying to implement encryption using the RSACryptoServiceProvider. I've already created a key pair, the public key will be exported to other machines so they can encrypt data, and the private key is stored locally in a key container.
Anyhow i've tried just to get the basic encryption/decryption going, i've tried loading the keys either from the key store, or just loading them using the fromxml function.
If I turn OAEP padding on (which I should be able to use since my machine is xp pro) I get the following error while decrypting:
Error occurred while decoding OAEP padding
of I turn OAEP padding off I get this
Bad Data
Here is my code
VB.NET:
Imports System
Imports System.Security.Cryptography
Imports System.Text
Public Class Form1
Dim ByteConverter As New UnicodeEncoding
Public Function RSAEncryptString(ByVal ClearText As String) As Byte()
Dim UnencryptedStrAsByt As Byte()
Dim EncryptedStrAsByt As Byte()
Dim EncryptedStrAsString As String
'Create the RSA Instance
Dim RSA2 As RSACryptoServiceProvider = New RSACryptoServiceProvider()
'Load the public key
RSA2.FromXmlString(My.Settings.RSAPublicKey)
'Get the bytes
UnencryptedStrAsByt = ByteConverter.GetBytes(ClearText)
'Encrypt the Byte data
EncryptedStrAsByt = RSA2.Encrypt(UnencryptedStrAsByt, True)
'Convert the bytes back to a string
EncryptedStrAsString = ByteConverter.GetString(EncryptedStrAsByt)
Return EncryptedStrAsByt
End Function
Public Function RSADecryptString(ByVal EncryptedByt As Byte()) As String
Dim DecryptedStrAsByt As Byte()
Dim DecryptedStrAsString As String
Dim cspParam As New CspParameters
cspParam.Flags = CspProviderFlags.UseMachineKeyStore
cspParam.KeyContainerName = "CCINFOKEY"
'Create the RSA Instance
Dim RSA3 As RSACryptoServiceProvider = New RSACryptoServiceProvider(cspParam)
'Decrypt the byte data
DecryptedStrAsByt = RSA3.Decrypt(EncryptedByt, True) <-- Where exception is thrown
'Convert the unencrypted bytes to a string
DecryptedStrAsString = ByteConverter.GetString(DecryptedStrAsByt)
Return DecryptedStrAsString
End Function
The info encrypts with no problem, just won't decrypt. I have scoured google for the answer to this problem and haven't found an answer. It is very important that I am able to export the public key to xml format so that it can be distributed to the app on other machines. If anyone has any ideas as to what im doing wrong please help, thank you.