"Bad Data" or "Error occurred while decoding OAEP padding" using RSA encryption

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
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.
 
There seems to be a problem with UseMachineKeyStore, if global RSACryptoServiceProvider.UseMachineKeyStore = True or local CspParameters.Flags = CspProviderFlags.UseMachineKeyStore is used to store the key I get the same problem when retrieving it using the other method. I know the documentation say they are the same, but that's the results I got anyway.
RSACryptoServiceProvider.UseMachineKeyStore Property:
Setting this property to true is equivalent to passing the UseMachineKeyStore flag to a CspParameters object.
 
That's it, I simply deleted the line that sets it to use the machinekeystore and it worked. I guess I didn't need that line of code for what i'm trying to do. The only question I have is, when I create a new key and save it to a key container, is that saved for the local machine, or the local user?
 
The UseMachineKeyStore specification is for machine, else for user.
 
If you store in machine retrieve from machine, if you store for user retrieve for user.
 
Back
Top