Search code examples
c#cryptographyrsabouncycastleencryption-asymmetric

Public and Private Key as variables C#


I'm writing a function where this function is receiving my public key as variable, and the value for this variable is the actually public key. I need two different functions apps in Azure, to encrypt and decrypt. The keys must match, but the problem is, every time I call the API the public key is different, I can encrypt without problems. But when I have to decrypt it doesn't work. I am not able to use the same key pairs for these functions. Thats why Im trying to use the keys I generated before as variables.

Example: string publicKey = "MMMFisIDUDHfhHSANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi7ZOKtc55v9NJuhQFR583BcFkcjflXNVMqC5/3b7t7v..."

This is the method I'm using to encrypt:

cipher.Init(true, publicKey);

My keys are being generated using Bouncy Castle.

RsaKeyPairGenerator g = new RsaKeyPairGenerator(); g.Init(new KeyGenerationParameters(new SecureRandom(), 2048)); AsymmetricCipherKeyPair keyPair = g.GenerateKeyPair();

It worked normally with the code below:

        string plainText = "test data here";
        byte[] plainTextToByte = Encoding.UTF8.GetBytes(plainText);

        //Generating Key Pair
        RsaKeyPairGenerator g = new RsaKeyPairGenerator();
        g.Init(new KeyGenerationParameters(new SecureRandom(), 2048));
        AsymmetricCipherKeyPair keyPair = g.GenerateKeyPair();

        //Extracting the private key from pair
        RsaKeyParameters privateKey = (RsaKeyParameters)keyPair.Private;
        RsaKeyParameters publicKey = (RsaKeyParameters)keyPair.Public;

        //Encryption proccess
        IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine());
        cipher.Init(true, publicKey);
        byte[] cipherText = cipher.ProcessBlock(plainTextToByte, 0, plainTextToByte.Length);
        string encryptedText = Encoding.UTF8.GetString(cipherText);
        Console.WriteLine(encryptedText);

        //Decryption Process
        cipher.Init(false, privateKey);
        byte[] decryptedText = cipher.ProcessBlock(cipherText, 0 , cipherText.Length);
        string decryptedTextToString = Encoding.UTF8.GetString(decryptedText);

        Console.WriteLine(decryptedTextToString);
        Console.ReadLine();`

I need the keys generated above as a variable to use in a function inside a console app.

But when I try pass the key as variable, I'm getting the error below:

https://i.sstatic.net/vLSOL.png

I could do same procedure using core classes from C#, it was similar with the code below:

C# RSA encryption/decryption with transmission

The same logic I follow for the example above is not working for me now. I am beginner into all this. Is there a way to do that?

This is the piece code I'm using to get the error on the screenshot. The keys were generated with the code I posted on the original post.

    string plainText = "test here";
    byte[] plainTextToByte = Encoding.UTF8.GetBytes(plainText);

    string publicKey = "MIIBIjANBgk...DAQAB";

    IAsymmetricBlockCipher cipher = new OaepEncoding(new RsaEngine());
    cipher.Init(true, publicKey);
    byte[] cipherText = cipher.ProcessBlock(plainTextToByte, 0, plainTextToByte.Length);
    string encryptedText = Encoding.UTF8.GetString(cipherText);
    Console.WriteLine(encryptedText);

    return new OkObjectResult(encryptedText);`

Att.


Solution

  • I'm not quite clear what the problem is. But based on the last snippet posted in the question, you are trying to import a public key. And according to your penultimate comment, it is a PEM encoded public key in X.509/SPKI format exported with a PemWriter:

    -----BEGIN PUBLIC KEY-----
    MIIB...
    ...AQAB
    -----END PUBLIC KEY-----
    

    Such a key can be imported and used in Cipher#Init() as follows (let publicKeyPem be the exported PEM key):

    using Org.BouncyCastle.OpenSsl;
    ...
    PemReader pemReader = new PemReader(new StringReader(publicKeyPem));
    RsaKeyParameters publicKeyReloaded = (RsaKeyParameters)pemReader.ReadObject();
    ...
    cipher.Init(true, publicKeyReloaded);