使用背景
毕设要求使用ECC椭圆曲线加密算法为用户信息加密,并数字签名。鉴于设计要求,ECIES的公私钥同样为ECDSA的公私钥。数字签名是后加部分。在已完成的设计部分中,ECIES的公私钥已将转化为std::string类型,在最小改动前提下,做出程序。
软件平台
Crypto++-7.0.0
GCC-Configuredwith:–prefix=/Applications//Contents/Developer/usr–with-gxx-include-dir=/usr/include/c++/4.2.1;()
设计语言-C++
用前必读
在中文程序员网站上,有关Crypto++的ECDSA的使用Demo很少。我是查看Crypto++官网上的维基百科中的使用介绍来写出的。本文章并非旨在教学,而是旨在记录,其中我所提供的Demo根据我自身需求有所更改。如果本文章不能帮助你,请查看Crypto++官网中您所需求的函数的使用方法。本文的使用方法是我偶然发现,并尝试出可用,但我并不知道原理,如果您放在您的程序中,出现任何问题,我不负任何责任
发现过程
ECC公钥(第一组)-3059301306072A8648CE3D020106082A8648CE3D0301070342000432AAB20C1C02D8844834D5AAC7E12814A53266AA5F0350190A9C1A9C43AA4178FFDC04D0785EE52676D061B5D836FEB34D766E710CE2196B5ADAF7876E805C01
ECC私钥(第一组)-3041020100301306072A8648CE3D020106082A8648CE3D030107042730250201010420C044B0620BAC02DA779426C1292A6398AA7EB99D9F624E9921186211AFFB6117
ECC公钥(第二组)-3059301306072A8648CE3D020106082A8648CE3D030107034200045EF50890EDC7FAAA2420533FA500AC897236DDE114A4FE7215B48F908585208E3DD9F1CEB1D923EEDB3C95027D82FE6AF64F82CB61631DEB7B68028899D7328C
ECC私钥(第二组)-3041020100301306072A8648CE3D020106082A8648CE3D030107042730250201010420FD9BB947E95A503645CAD68DC70E21E3D04AF2C52206B9057ED52D94299E93BE
ECC公钥(第三组)-3059301306072A8648CE3D020106082A8648CE3D0301070342000419514747D4D4F91C484DC529D1695E05D8383D04EB6D84A0BF40C4641CB3490FF17BF5ECA97A7C0F325D62C5EE2180D9C5ABC977CBEE1A40B45F5B32EB1F61F3
ECC私钥(第三组)-3041020100301306072A8648CE3D020106082A8648CE3D0301070427302502010104203DB7FB2335C3368485C1A51AD9F36FA3FF4B2BC187D0D3FAE40977066EE74116
是的,你可能发现了。ECC公钥的前54个字符是相通的,每次变化的只是后面的。同理,ECC私钥每次变化的也只是前70个字符,我猜想,是不是后面变化的才是公私钥。经过尝试,可以使用充当ECDSA的公私钥。
ECIES加解密Demo
该Demo是我从网上找到的,网上有很多Demo,但是有些我发现用不了,在可用的Demo中最符合我需求的是这个,同时,我稍微进行了更改。如有侵权,告知必删。
include""include""include""defineECC_ENCRYPTION_ALGORITHM_H_ifvoidEccEncryption::GenerateEccKeys(unsignedintuiKeySize,std::stringsPrivateKey,std::stringsPublicKey){usingnamespaceCryptoPP;//Randompool,thesecondparameteristhelengthofkey//随机数池,第二个参数是生成密钥的长AutoSeededRandomPoolrnd(false,256);ECIESECP::PrivateKeyprivateKey;ECIESECP::PublicKeypublicKey;//(rnd,ASN1::secp256r1());//(publicKey);ECIESECP::Encryptorencryptor(publicKey);HexEncoderpubEncoder(newStringSink(sPublicKey));(pubEncoder);();ECIESECP::Decryptordecryptor(privateKey);HexEncoderprvEncoder(newStringSink(sPrivateKey));(prvEncoder);();}std::stringEccEncryption::Encrypt(conststd::stringsPublicKey,conststd::stringsMsgToEncrypt){usingnamespaceCryptoPP;//Iftosavethekeysintoafile,FileSourceshouldbereplaceStringSourceStringSourcepubString(sPublicKey,true,newHexDecoder);ECIESECP::Encryptorencryptor(pubString);//Calculatethelengthofciphertextsize_tuiCipherTextSize=(());std::stringsCipherText;(uiCipherTextSize);RandomPoolrnd;(rnd,(byte*)(_str()),(),(byte*)(()));returnsCipherText;}std::stringEccEncryption::Decrypt(conststd::stringsPrivateKey,conststd::stringsMsgToDecrytp){usingnamespaceCryptoPP;StringSourceprivString(sPrivateKey,true,newHexDecoder);ECIESECP::Decryptordecryptor(privString);autosPlainTextLen=(());std::stringsDecryText;(sPlainTextLen);RandomPoolrnd;(rnd,(byte*)_str(),(),(byte*)());returnsDecryText;}intmain(){std::stringsStrToTest=std::string("++opensourcelibrary.");EccEncryptionecc;std::stringsPrivateKey,sPublicKey;(1024,sPrivateKey,sPublicKey);std::cout"Generatedprivatekeyis:"std::l;std::coutsPrivateKeystd::l;std::cout"***********************************************************"std::l;std::cout"Generatedpublickeyis:"std::l;std::coutsPublicKeystd::l;std::cout"***********************************************************"std::l;std::cout"Themessagetobeencryptedis:"std::l;std::coutsStrToTeststd::l;std::cout"***********************************************************"std::l;std::stringsEncryptResult=(sPublicKey,sStrToTest);std::cout"Theresultofencryptis:"std::l;std::coutsEncryptResultstd::l;std::cout"***********************************************************"std::l;std::stringsDecryptResult=(sPrivateKey,sEncryptResult);std::cout"Theresultofdecryptis:"std::l;std::coutsDecryptResultstd::l;std::cout"***********************************************************"std::l;return0;}12345678910111281920212223242526272829303373839404474849505575859606162636465666768697077778798088788899091929394959697989910010110210310410510610710810911011111211121122123124125126ECIES-ECSDSA联合使用Demo
/*auteur:GXCSDN:GuoXuan_CHN*/includestringinclude""include""include""#include""usingnamespacestd;CryptoPP::ECIESCryptoPP::ECP::PrivateKeyePrivateKey;CryptoPP::ECIESCryptoPP::ECP::PublicKeyePublicKey;stringsPrivateKey,sPublicKey;voidGenerateEccKeys(){usingnamespaceCryptoPP;//Randompool,thesecondparameteristhelengthofkey//随机数池,第二个参数是生成密钥的长AutoSeededRandomPoolrnd(false,256);//Generateprivatekey//生成私钥(rnd,ASN1::secp256r1());//Generatepublickeyusingprivatekey//用私钥生成密钥(ePublicKey);HexEncoderpubEncoder(newStringSink(sPublicKey));(pubEncoder);();HexEncoderprvEncoder(newStringSink(sPrivateKey));(prvEncoder);();}stringsigne(stringmessage){std::stringsignature="";//数字签名过程CryptoPP::ECDSACryptoPP::ECP,CryptoPP::SHA1::PrivateKeyprivateKey;std::stringexp=(70);CryptoPP::HexDecoderdecoder;((CryptoPP::byte*)exp[0],());();CryptoPP::Integerx;(decoder,());(CryptoPP::ASN1::secp256r1(),x);CryptoPP::ECDSACryptoPP::ECP,CryptoPP::SHA1::Signersigner(privateKey);CryptoPP::AutoSeededRandomPoolprng;//签名结果signature="";CryptoPP::StringSources(message,true/*pumpall*/,newCryptoPP::SignerFilter(prng,signer,newCryptoPP::StringSink(signature))//SignerFilter);//StringSourcereturnsignature;//签名过程结束}boolVerifierSignature(stringsignature,stringmessage){std::stringpt="";//验签过程CryptoPP::ECDSACryptoPP::ECP,CryptoPP::SHA1::PublicKeypublicKey;pt=(54);CryptoPP::HexDecoderdecoder;((CryptoPP::byte*)pt[0],());();CryptoPP::ECP::Pointq;size_tlen=();=false;(decoder,len/2);(decoder,len/2);(CryptoPP::ASN1::secp256r1(),q);CryptoPP::ECDSACryptoPP::ECP,CryptoPP::SHA1::Verifierverifier(publicKey);//Resultoftheverificationprocessboolresult=false;CryptoPP::StringSourcess(signature+message,true/*pumpall*/,newCryptoPP::SignatureVerificationFilter(verifier,newCryptoPP::ArraySink((CryptoPP::byte*)result,sizeof(result))));returnresult;}intmain(){std::stringmessage="Yodasaid,";std::stringsignature="";boolresult=false;GenerateEccKeys();signature=signe(message);result=VerifierSignature(signature,message);cout"******testerlabon*****"l;coutresultl;result=VerifierSignature(signature,"1234567890");cout"******testerlamauvais*****"l;coutresultl;}1234567891011128192021222324252627282930337383940447484950557585960616263646566676869707777879808878889909192939495969798991001011021031041051061071081091101111121112112212312412512612712812937138139编译指令
该指令为在我本机上使用的指令,请合理更改
g++Demo_/usr/local/include/cryptopp-lcryptopp
