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());