内容简介:http://stackoverflow.com/questions/4192296/c-sharp-how-to-simply-encrypt-a-text-file-with-a-pgp-public-key
我已经研究了一些关于如何实现我在问题中所说的内容,并发现了几个API,但是大多数API看起来很复杂,而且由于我只是一个noobie在这个领域,我只想要一个简单的方法,如:
public String Encrypt(String message, PublicKey publicKey)
不知道这可以做到吗?如果没有,请有人开启我另一种方式来实现这个:)
谢谢.
更新:
到目前为止,我只看到OpenPGP加密的所有库都需要公钥和私钥进行加密,而我只想使用公钥进行加密(因为我没有使用私钥) !
public class PgpEncrypt { private PgpEncryptionKeys m_encryptionKeys; private const int BufferSize = 0x10000; /// <summary> /// Instantiate a new PgpEncrypt class with initialized PgpEncryptionKeys. /// </summary> /// <param name="encryptionKeys"></param> /// <exception cref="ArgumentNullException">encryptionKeys is null</exception> public PgpEncrypt(PgpEncryptionKeys encryptionKeys) { if (encryptionKeys == null) { throw new ArgumentNullException("encryptionKeys", "encryptionKeys is null."); } m_encryptionKeys = encryptionKeys; } /// <summary> /// Encrypt and sign the file pointed to by unencryptedFileInfo and /// write the encrypted content to outputStream. /// </summary> /// <param name="outputStream">The stream that will contain the /// encrypted data when this method returns.</param> /// <param name="fileName">FileInfo of the file to encrypt</param> public void Encrypt(Stream outputStream, FileInfo unencryptedFileInfo) { if (outputStream == null) { throw new ArgumentNullException("outputStream", "outputStream is null."); } if (unencryptedFileInfo == null) { throw new ArgumentNullException("unencryptedFileInfo", "unencryptedFileInfo is null."); } if (!File.Exists(unencryptedFileInfo.FullName)) { throw new ArgumentException("File to encrypt not found."); } using (Stream encryptedOut = ChainEncryptedOut(outputStream)) { using (Stream literalOut = ChainLiteralOut(encryptedOut, unencryptedFileInfo)) using (FileStream inputFile = unencryptedFileInfo.OpenRead()) { WriteOutput(literalOut, inputFile); } } } private static void WriteOutput(Stream literalOut, FileStream inputFile) { int length = 0; byte[] buf = new byte[BufferSize]; while ((length = inputFile.Read(buf, 0, buf.Length)) > 0) { literalOut.Write(buf, 0, length); } } private Stream ChainEncryptedOut(Stream outputStream) { PgpEncryptedDataGenerator encryptedDataGenerator; encryptedDataGenerator = new PgpEncryptedDataGenerator(SymmetricKeyAlgorithmTag.TripleDes, new SecureRandom()); encryptedDataGenerator.AddMethod(m_encryptionKeys.PublicKey); return encryptedDataGenerator.Open(outputStream, new byte[BufferSize]); } private static Stream ChainLiteralOut(Stream encryptedOut, FileInfo file) { PgpLiteralDataGenerator pgpLiteralDataGenerator = new PgpLiteralDataGenerator(); return pgpLiteralDataGenerator.Open(encryptedOut, PgpLiteralData.Binary, file); } }
当然,要运行这些代码,您必须在项目中包含 BouncyCastle library .
我已经测试加密,然后解密,运行正常:)
http://stackoverflow.com/questions/4192296/c-sharp-how-to-simply-encrypt-a-text-file-with-a-pgp-public-key
以上所述就是小编给大家介绍的《C#如何使用PGP公钥简单加密文本文件?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- 工业网络安全趋势:公钥加密
- Golang代码搜集-基于RSA的公钥加密私钥解密-私钥签名公钥验证
- TLS和HTTPS加密,公钥私钥体系
- 区块秘史|密码朋克的死亡圣器:公钥加密
- 简述密码学应用四阶段:对称加密、公钥加密、区块链与高等密码学
- 安全不安全001:openssl生成非对称加密RSA公钥密钥命令
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Web Design DeMYSTiFieD
Willard, Wendy / 2010-11 / $ 24.86
Website Design just got a whole lot easier! This title helps you to learn the latest website development tools, techniques, and best practices. "Web Design Demystified" provides the hands-on help you ......一起来看看 《Web Design DeMYSTiFieD》 这本书的介绍吧!