内容简介: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公钥密钥命令
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
企业IT架构转型之道:阿里巴巴中台战略思想与架构实战
钟华 / 机械工业出版社 / 2017-4-1 / 79
在当今整个中国社会都处于互联网转型的浪潮中,不管是政府职能单位、业务规模庞大的央企,还是面临最激烈竞争的零售行业都处于一个重要的转折点,这个转折对企业业务模式带来了冲击,当然也给企业的信息中心部门带来了挑战:如何构建IT系统架构更好地满足互联网时代下企业业务发展的需要。阿里巴巴的共享服务理念以及企业级互联网架构建设的思路,给这些企业带来了不少新的思路,这也是我最终决定写这本书的最主要原因。本书从阿......一起来看看 《企业IT架构转型之道:阿里巴巴中台战略思想与架构实战》 这本书的介绍吧!
JS 压缩/解压工具
在线压缩/解压 JS 代码
UNIX 时间戳转换
UNIX 时间戳转换