如何在C#中解密OpenPGP RSA加密文本文件?
我希望这个问题可以针对开始在C#中使用加密/解密的人提供一些指导。网络上有关于此的现有示例,但我真的很努力地在给定的情况下将其全部付诸实践。
如果给出了使用rsa使用OpenPGP进行加密的文本文件,则在C#中解密此文件的最佳方法是什么?
这是我尝试的:
- 使用Kleopatra OpenPGP,我使用2048bit RSA生成了一个钥匙对。这会生成一个私钥和公钥。
- 然后,我正在对文本文件进行加密加密/签名,其中有几个单词作为测试。
- 在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:
- Using Kleopatra OpenPGP, I am generating a key pair using 2048bit RSA. This generates a private and public key.
- I am then encrypting/signing a text file with a few word in it as a test.
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来您需要此库(请参阅解密部分) https://github.com/github.com/mattosaurus/mattosaurus/pgpcore
It's looks like you need this library (see Decrypt section) https://github.com/mattosaurus/PgpCore