How to decrypt an OpenPGP RSA Encrypted text file in C#?

2.2k views Asked by At

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.

1

There are 1 answers

0
Andrii Khomiak On

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