Auteur:GX
CSDN:GuoXuan_CHN
#include <iostream>
#include "eccrypto.h"
#include "osrng.h"
#include "oids.h"
#include "hex.h"
#include "filters.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
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, 256);
ECIES<ECP>::PrivateKey privateKey;
ECIES<ECP>::PublicKey publicKey;
// Generate private key
privateKey.Initialize(rnd, ASN1::secp256r1());
// Generate public key using private key
privateKey.MakePublicKey(publicKey);
ECIES<ECP>::Encryptor encryptor(publicKey);
HexEncoder pubEncoder(new StringSink(sPublicKey));
publicKey.DEREncode(pubEncoder);
pubEncoder.MessageEnd();
ECIES<ECP>::Decryptor decryptor(privateKey);
HexEncoder prvEncoder(new StringSink(sPrivateKey));
privateKey.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
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;
}
int 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;
return 0;
}
ECIES-ECSDSA联合使用Demo
/*
auteur:GX
CSDN:GuoXuan_CHN
*/
#include <fstream>
#include <string>
#include <iostream>
#include "eccrypto.h"
#include "osrng.h"
#include "oids.h"
#include "hex.h"
#include "filters.h"
#include "des.h"
using namespace std;
CryptoPP::ECIES<CryptoPP::ECP>::PrivateKey ePrivateKey;
CryptoPP::ECIES<CryptoPP::ECP>::PublicKey ePublicKey;
string sPrivateKey, sPublicKey;
void GenerateEccKeys()
{
using namespace CryptoPP;
// Random pool, the second parameter is the length of key
// 随机数池,第二个参数是生成密钥的长
AutoSeededRandomPool rnd(false, 256);
// Generate private key
// 生成私钥
ePrivateKey.Initialize(rnd, ASN1::secp256r1());
// Generate public key using private key
// 用私钥生成密钥
ePrivateKey.MakePublicKey(ePublicKey);
HexEncoder pubEncoder(new StringSink(sPublicKey));
ePublicKey.DEREncode(pubEncoder);
pubEncoder.MessageEnd();
HexEncoder prvEncoder(new StringSink(sPrivateKey));
ePrivateKey.DEREncode(prvEncoder);
prvEncoder.MessageEnd();
}
string signe (string message)
{
std::string signature="";
//数字签名过程
CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA1>::PrivateKey privateKey;
std::string exp = sPrivateKey.substr(70);
CryptoPP::HexDecoder decoder;
decoder.Put((CryptoPP::byte *)&exp[0], exp.size());
decoder.MessageEnd();
CryptoPP::Integer x;
x.Decode(decoder, decoder.MaxRetrievable());
privateKey.Initialize(CryptoPP::ASN1::secp256r1(), x);
CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA1>::Signer signer( privateKey );
CryptoPP::AutoSeededRandomPool prng;
//签名结果
signature = "";
CryptoPP::StringSource s( message, true /*pump all*/,
new CryptoPP::SignerFilter( prng,
signer,
new CryptoPP::StringSink( signature )
) // SignerFilter
); // StringSource
return signature;
//签名过程结束
}
bool VerifierSignature(string signature,string message)
{
std::string pt="";
//验签过程
CryptoPP::ECDSA<CryptoPP::ECP, CryptoPP::SHA1>::PublicKey publicKey;
pt = sPublicKey.substr(54);
CryptoPP::HexDecoder decoder;
decoder.Put((CryptoPP::byte *)&pt[0], pt.size());
decoder.MessageEnd();
CryptoPP::ECP::Point q;
size_t len = decoder.MaxRetrievable();
q.identity = false;
q.x.Decode(decoder, len/2);
q.y.Decode(decoder, len/2);
publicKey.Initialize( CryptoPP::ASN1::secp256r1(), q );
CryptoPP::ECDSA<CryptoPP::ECP,CryptoPP::SHA1>::Verifier verifier(publicKey);
// Result of the verification process
bool result = false;
CryptoPP::StringSource ss( signature+message, true /*pump all*/,
new CryptoPP::SignatureVerificationFilter(
verifier,
new CryptoPP::ArraySink((CryptoPP::byte *)&result, sizeof(result) )
)
);
return result;
}
int main()
{
std::string message = "Yoda said, Do or do not. There is no try.";
std::string signature="";
bool result = false;
GenerateEccKeys();
signature = signe (message);
result = VerifierSignature(signature,message);
cout << "****** tester la bon*****" << endl;
cout << result << endl;
result = VerifierSignature(signature,"1234567890");
cout << "****** tester la mauvais*****" << endl;
cout << result << endl;
}