1

I am hoping that this question can be met with some guidance for someone who is beginning to work with encryption/decryption in C#. There are existing examples on the web regarding this, but I am truthfully struggling to put it all into practice for my given situation.

If given a text file that has been encrypted using OpenPGP with RSA, what is the best method to decrypt this in C#?

This is what I am attempting:

  1. Using Kleopatra OpenPGP, I am generating a key pair using 2048bit RSA. This generates a private and public key.
  2. I am then encrypting/signing a text file with a few word in it as a test.
  3. In C#, I want to decrypt this text file.

Current code:

byte[] encryptedData = File.ReadAllBytes("C:\\PGP Encryption\\test.txt.gpg"); // The encrypted text file generated by Kleopatra.

using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider())
{
    //Import the RSA Key information. This needs
    //to include the private key information.
    RSA.ImportParameters(RSAKeyInfo);

    //Decrypt the passed byte array and specify OAEP padding.  
    decryptedData = RSA.Decrypt(DataToDecrypt, DoOAEPPadding);
}

return decryptedData;

Unfortunately, the RSA.Decrypt() call throws an exception that reads "The data to be decrypted exceeds the maximum for this modulus of 128 bytes."

I also do not believe that my private key is actually being loaded, as I'm not explicitly stating where the key is. But I don't see how the RSAParameters object is supposed to get populated otherwise.

If anyone can point me in the right direction to decrypt a file in this way, thank you in advance for your time and information.

mherr
  • 348
  • 1
  • 7
  • 25
  • OpenPGP is a unique high-level format that requires a dedicate openpgp-specific library to work with. Bouncycastle has a C# library, you can hopefully find some examples on stackoverflow and elsewhere to learn how to use it. It's not easy. – President James K. Polk May 10 '22 at 21:52

1 Answers1

0

It's looks like you need this library (see Decrypt section) https://github.com/mattosaurus/PgpCore

Andrii Khomiak
  • 565
  • 4
  • 17