getting error while decrypt while using RSA key container and encrypt data using exported public key

0

In below code I am first creating asymmetric key and exporting public key to location C:\Temp. Now I am encrypt and decrypt a text `test", but during decryption I am getting below error,

What is issue with code? Is this not encrypting properly? How to fix it? Please suggest

System.Security.Cryptography.CryptographicException: 'Error occurred while decoding OAEP padding.'

System.Security.Cryptography.CryptographicException HResult=0x80131430 Message=Error occurred while decoding OAEP padding. Source= StackTrace: at System.Security.Cryptography.RSACryptoServiceProvider.DecryptKey(SafeKeyHandle pKeyContext, Byte[] pbEncryptedKey, Int32 cbEncryptedKey, Boolean fOAEP, ObjectHandleOnStack ohRetDecryptedKey) at System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP) at net452.Program.Decrypt(String encryptText) in C:\Users\H190733\source\repos\net452\net452\Program.cs:line 52 at net452.Program.Main(String[] args) in C:\Users\H190733\source\repos\net452\net452\Program.cs:line 23

Here is the entire code for C# console app,

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace net452
{
class Program
{
    private const string KEY_STORE_NAME = "MyKeyStore";
    public static CspParameters csp = new CspParameters();
    public static RSACryptoServiceProvider rsa;

    static void Main(string[] args)
    {
        //create keys
        CreateAsmKeys(true);

        //encrypt the text "test"
        var encryptedData = Encrypt("test");

        //try to decrypt it and getting error
        var data = Decrypt(encryptedData);

        Console.ReadLine();
    }

    private static string Encrypt(string plainText)
    {
        using (var reader = new StreamReader(Path.Combine(@"C:\Temp\", "publicKey.txt")))
        {
            csp.KeyContainerName = KEY_STORE_NAME;
            rsa = new RSACryptoServiceProvider(csp);

            var publicKeyText = reader.ReadToEnd();

            rsa.FromXmlString(publicKeyText);
            rsa.PersistKeyInCsp = true;

            var encryptedAsBytes = rsa.Encrypt(Encoding.UTF8.GetBytes(plainText), true);
            return Convert.ToBase64String(encryptedAsBytes);
        }
    }


    public static string Decrypt(string encryptText)
    {
        var cspParams = new CspParameters { KeyContainerName = KEY_STORE_NAME };

        rsa = new RSACryptoServiceProvider(cspParams);

        var decryptBytes = rsa.Decrypt(Convert.FromBase64String(encryptText), true);
        return Encoding.Default.GetString(decryptBytes);
    }


    private static void CreateAsmKeys(bool useMachineKeyStore)
    {

        csp.KeyContainerName = KEY_STORE_NAME;

        if (useMachineKeyStore)
        {
            csp.Flags = CspProviderFlags.UseMachineKeyStore;
        }

        rsa = new RSACryptoServiceProvider(csp) { PersistKeyInCsp = true };

        //export public key to location C:\Temp\
        using (var writer = new StreamWriter(Path.Combine(@"C:\Temp\", "publicKey.txt")))
        {
            writer.Write(rsa.ToXmlString(false));
        }
    }
}
}
c#
cryptography
rsa
asked on Stack Overflow Jan 14, 2021 by user584018 • edited Jan 14, 2021 by user584018

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0