User Guide:eccrypto.h学习笔记(翻译)

来源:互联网 发布:网络话费卡怎么用 编辑:程序博客网 时间:2024/06/10 01:01

User Guide:eccrypto.h学习笔记(翻译)

原文链接

https://www.cryptopp.com/wiki/User_Guide:_eccrypto.h

eccrypto.h

eccrypto.h给椭圆曲线密码操作提供模板类.这些类主要是模板,原因是在使用两种类型的椭圆曲线被人们使用;因此,GF(2^n)基础上的

用EC2N(ec2n.h)代表,GF(p)用ECP(ecp.h)代表

椭圆曲线参数用模板类ECParameters表示.参数可以用多种方式初始化;但是更常用的一个是使用方法LoadRecommendedParameters()

这个方法提供其中一个推荐参数OIDs(recommended-parameters-OIDs), 后者被定义在oids.h中

示例-生成一个EC2N密钥对并且保存它

 // ECPrivateKey 被直接使用因为次要不会被用来做任何密码操作 AutoSeededRandomPool rng; ECPrivateKey privkey(rng, ASN1::sect233k1); Base64Encoder privkeysink(new FileSink("c:\\privkey.txt")); privkey.DEREncode(privkeysink); privkeysink.MessageEnd();   // 清除base64Encoder缓存区 // 假设由于要发送给第三方,我们需要分开存储公钥 ECPublicKey pubkey(privkey); Base64Encoder pubkeysink(new FileSink("c:\\pubkey.txt")); pubkey.DEREncode(pubkeysink); pubkeysink.MessageEnd();    // Need to flush Base64Encoder's buffer

示例-加载公钥并且加密文件

 string sContents; FileSource("c:\\tobesigned.dat", true,     new StringSink(sContents)); ECEncryptor pubkey(     FileSource("c:\\pubkey.txt", true,         new Base64Decoder))); // 不能使用std::string作为缓存区因为它的内存可能不是连续的 SecByteBlock sbbCipherText(pubkey.CipherTextLength(sContents.size())); // ECIES encryption 很棒因为他能在内部处理整个加密过程,无论数据的长短 // 我们不需要生成对称密钥并且分开加密 AutoSeededRandomPool rng; pubkey.Encrypt(     rng,     (byte const*) sContents.data(),     sContents.size(),     sbbCipherText.Begin()); FileSink("c:\\encrypted.dat").Put(sbbCipherText.Begin(), sbbCipherText.Size());