如何在C#中解密OpenPGP RSA加密文本文件?

发布于 2025-01-27 18:48:34 字数 997 浏览 3 评论 0原文

我希望这个问题可以针对开始在C#中使用加密/解密的人提供一些指导。网络上有关于此的现有示例,但我真的很努力地在给定的情况下将其全部付诸实践。

如果给出了使用rsa使用OpenPGP进行加密的文本文件,则在C#中解密此文件的最佳方法是什么?

这是我尝试的:

  1. 使用Kleopatra OpenPGP,我使用2048bit RSA生成了一个钥匙对。这会生成一个私钥和公钥。
  2. 然后,我正在对文本文件进行加密加密/签名,其中有几个单词作为测试。
  3. 在C#中,我想解密此文本文件。

当前代码:

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;

不幸的是,rsa.decrypt()呼叫引发了一个例外,读取“要解密的数据超过此模量为128个字节的最大值”。

我也不相信我的私钥实际上正在加载,因为我没有明确说明钥匙在哪里。但是我看不出rsaparameters对象应该如何被填充。

如果有人可以将我指向以这种方式解密文件的正确方向,请提前感谢您的时间和信息。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

(り薆情海 2025-02-03 18:48:34

看起来您需要此库(请参阅解密部分) https://github.com/github.com/mattosaurus/mattosaurus/pgpcore

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文