最近工作有个项目需要用到加密算法,在网上搜索,Crypto++库是一个不错的加密算法库,然而在网上,用Crypto++库来做ECC加密算法的例子很少。而且网上的例子也大都是把公钥和私钥生成到文件中,而笔者需要的是把公钥和私钥直接放到字符串中。
经过自己的搜索和试验,终于找到了实现的方法,可能不是最好的,但也算一种
解决方案。
关于Crypto++库的编译、安装方法,请自行度娘,这里就不累述了。
一、ECC加密算法
关于ECC加密算法的原理,请自行度娘,这里只列出算法的代码。
下面的代码已经在Windows 10、Visual Studio community 2015、Crypto++ 5.6.5环境下编译运行通过
** 在Visual Studio中编译时,需要将 "Properties -> C/C++ -> Code Generateion -> Runtime Library "改为 “Multi-threaded Debug (/MTd)”,如下图所示:**
头文件:ecc_encryption_algorithm.h
#ifndef ECC_ENCRYPTION_ALGORITHM_H_
#define ECC_ENCRYPTION_ALGORITHM_H_
#include<string>
class EccEncryption
{
public:
/// This method is used to generate keys for ECC encryption algorithm
///
/// \param[in] uiKeySize, length of key
/// \param[out] sPrivateKey, private key
/// \param[out] sPublicKey, public key
void GenerateEccKeys(unsigned int uiKeySize, std::string& sPrivateKey, std::string& sPublicKey);
/// This method is used to encrypt the input message using public key
///
/// \param[in] sPublicKey, public key generated by the first method
/// \param[out] sMsgToEncrypt, message to encryppt
/// \return the message encrypted using the input public key
std::string Encrypt(const std::string& sPublicKey, const std::string& sMsgToEncrypt);
/// This method is used to decrypt the input message using private key
///
/// \param[in] sPrivateKey, private key used to decrypt the cipher text
/// \param[in] sMsgToDecrypt, cipher text used to decrypt to get the plain text
/// \return decrypted plain text
std::string Decrypt(const std::string& sPrivateKey, const std::string& sMsgToDecrytp);
};
#endif
cpp文件: ecc_encryption_algorithm.cpp
#include "ecc_encryption_algorithm.h" #include "eccrypto.h" #include "osrng.h" #include "oids.h" #include "hex.h" #include "filters.h" void EccEncryption::GenerateEccKeys(unsigned int uiKeySize, std::string& sPrivateKey, std::string& sPublicKey) { using namespace CryptoPP; // Random pool, the second parameter is the length of key // 随机数池,第二个参数是生成密钥的长 AutoSeededRandomPool rnd(false, 1024); ECIES<ECP>::PrivateKey privateKey; ECIES<ECP>::PublicKey publicKey; // Generate private key // 生成私钥 privateKey.Initialize(rnd, ASN1::secp521r1()); // Generate public key using private key // 用私钥生成密钥 privateKey.MakePublicKey(publicKey); ECIES<ECP>::Encryptor encryptor(publicKey); HexEncoder pubEncoder(new StringSink(sPublicKey)); publicKey.DEREncode(pubEncoder); //encryptor.DEREncode(pubEncoder); pubEncoder.MessageEnd(); ECIES<ECP>::Decryptor decryptor(privateKey); HexEncoder prvEncoder(new StringSink(sPrivateKey)); privateKey.DEREncode(prvEncoder); //decryptor.DEREncode(prvEncoder); prvEncoder.MessageEnd(); } std::string EccEncryption::Encrypt(const std::string& sPublicKey, const std::string& sMsgToEncrypt) { using namespace CryptoPP; // If to save the keys into a file, FileSource should be replace StringSource // 如果需要把密钥保存到文件里,可以用 FileSource StringSource pubString(sPublicKey, true, new HexDecoder); ECIES<ECP>::Encryptor encryptor(pubString); // Calculate the length of cipher text // 计算加密后密文的长度 size_t uiCipherTextSize = encryptor.CiphertextLength(sMsgToEncrypt.size()); std::string sCipherText; sCipherText.resize(uiCipherTextSize); RandomPool rnd; encryptor.Encrypt(rnd, (byte*)(sMsgToEncrypt.c_str()), sMsgToEncrypt.size(), (byte*)(sCipherText.data())); return sCipherText; } std::string EccEncryption::Decrypt(const std::string& sPrivateKey, const std::string& sMsgToDecrytp) { using namespace CryptoPP; StringSource privString(sPrivateKey, true, new HexDecoder); ECIES<ECP>::Decryptor decryptor(privString); auto sPlainTextLen = decryptor.MaxPlaintextLength(sMsgToDecrytp.size()); std::string sDecryText; sDecryText.resize(sPlainTextLen); RandomPool rnd; decryptor.Decrypt(rnd, (byte*)sMsgToDecrytp.c_str(), sMsgToDecrytp.size(), (byte*)sDecryText.data()); return sDecryText; }
测试代码: test_main.h
#include "ecc_encryption_algorithm.h"
#include <iostream>
void main()
{
std::string sStrToTest = std::string("Hello world. This is an example of Ecc\
encryption algorithm of Crypto++ open source library.");
EccEncryption ecc;
std::string sPrivateKey, sPublicKey;
ecc.GenerateEccKeys(1024, sPrivateKey, sPublicKey);
std::cout << "Generated private key is : "<< std::endl;
std::cout << sPrivateKey << std::endl;
std::cout << "***********************************************************" << std::endl;
std::cout << "Generated public key is : "<< std::endl;
std::cout << sPublicKey << std::endl;
std::cout << "***********************************************************" << std::endl;
std::cout << "The message to be encrypted is : " << std::endl;
std::cout << sStrToTest << std::endl;
std::cout << "***********************************************************" << std::endl;
std::string sEncryptResult = ecc.Encrypt(sPublicKey, sStrToTest);
std::cout << "The result of encrypt is : " << std::endl;
std::cout << sEncryptResult << std::endl;
std::cout << "***********************************************************" << std::endl;
std::string sDecryptResult = ecc.Decrypt(sPrivateKey, sEncryptResult);
std::cout << "The result of decrypt is : " << std::endl;
std::cout << sDecryptResult << std::endl;
std::cout << "***********************************************************" << std::endl;
}
运行结果如下图所示:
作者:Junnix
链接:https://www.jianshu.com/p/8177c322b695
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。