问题描述
请问如何使用crypto++实现privatekey加密,publickey解密的?看到一些例子,都是用公钥加密,私钥解密的,不知道有没有privatekey加密,publickey解密的用法??公钥加密,私钥解密的例子://#defineECC_ALGORITHMCryptoPP::ECP//#defineECC_ALGORITHMCryptoPP::EC2N//////StandardECCCurves//UsewhenECC_ALGORITHMisCryptoPP::ECP////#defineECC_CURVECryptoPP::ASN1::secp112r1()//#defineECC_CURVECryptoPP::ASN1::secp112r2()//#defineECC_CURVECryptoPP::ASN1::secp128r1()//#defineECC_CURVECryptoPP::ASN1::secp128r2()#defineECC_CURVECryptoPP::ASN1::secp160r1()//#defineECC_CURVECryptoPP::ASN1::secp160k1()//#defineECC_CURVECryptoPP::ASN1::secp160r2()//#defineECC_CURVECryptoPP::ASN1::secp192k1()//#defineECC_CURVECryptoPP::ASN1::secp224k1()//#defineECC_CURVECryptoPP::ASN1::secp224r1()//#defineECC_CURVECryptoPP::ASN1::secp256k1()//#defineECC_CURVECryptoPP::ASN1::secp384r1()//#defineECC_CURVECryptoPP::ASN1::secp521r1()////ECCCurvesoverGF(2)//UsewhenECC_ALGORITHMisCryptoPP::EC2N////#defineECC_CURVECryptoPP::ASN1::sect113r1()//#defineECC_CURVECryptoPP::ASN1::sect113r2()//#defineECC_CURVECryptoPP::ASN1::sect131r1()//#defineECC_CURVECryptoPP::ASN1::sect131r2()//#defineECC_CURVECryptoPP::ASN1::sect163k1()//#defineECC_CURVECryptoPP::ASN1::sect163r1()//#defineECC_CURVECryptoPP::ASN1::sect163r2()//#defineECC_CURVECryptoPP::ASN1::sect193r1()//#defineECC_CURVECryptoPP::ASN1::sect193r2()//#defineECC_CURVECryptoPP::ASN1::sect233k1()//#defineECC_CURVECryptoPP::ASN1::sect233r1()//#defineECC_CURVECryptoPP::ASN1::sect239k1()//#defineECC_CURVECryptoPP::ASN1::sect283k1()//#defineECC_CURVECryptoPP::ASN1::sect283r1()//#defineECC_CURVECryptoPP::ASN1::sect409k1()//#defineECC_CURVECryptoPP::ASN1::sect409r1()//#defineECC_CURVECryptoPP::ASN1::sect571k1()//#defineECC_CURVECryptoPP::ASN1::sect571r1()intmain(intargc,char*argv[]){try{CryptoPP::ECIES<ECC_ALGORITHM>::PrivateKeyPrivateKey;CryptoPP::ECIES<ECC_ALGORITHM>::PublicKeyPublicKey;CryptoPP::AutoSeededRandomPoolrng;//CurveKeyGenerationPrivateKey.Initialize(rng,ECC_CURVE);PrivateKey.MakePublicKey(PublicKey);//KeyValidationif(false==PrivateKey.Validate(rng,6)){throwstd::string("PrivateKeyValidation");}if(false==PublicKey.Validate(rng,6)){throwstd::string("PublicKeyValidation");}//EncryptorandDecryptorCryptoPP::ECIES<ECC_ALGORITHM>::EncryptorEncryptor(PublicKey);CryptoPP::ECIES<ECC_ALGORITHM>::DecryptorDecryptor(PrivateKey);//Message//std::stringPlainText="ECC";//std::stringPlainText="ECCTest";std::stringPlainText="Yodasaid,"Doordonot.Thereisnotry."";//std::stringPlainText="WinniethePoohsaid,"Iamabearofverylittlebrain,andlongwordsbotherme."";//RuntimeSizes...unsignedintPlainTextLength=PlainText.length()+1;unsignedintCipherTextLength=Encryptor.CiphertextLength(PlainText.length()+1);if(0==CipherTextLength){throwstd::string("plaintextLengthisnotvalid(toolong)");}//Diagnosticsstd::cout<<"Plaintext:"<<std::endl<<"'"<<PlainText<<"'"<<std::endl;std::cout<<"Theplaintextlengthis"<<PlainTextLength<<"(includingthetrailing'\0')"<<std::endl;////Takeabreak...//std::cout<<std::endl;std::cout<<"Theciphertextlengthforthismessageis"<<CipherTextLength<<std::endl;//ScratchforEncryptionbyte*CipherText=newbyte[CipherTextLength];if(NULL==CipherText){throwstd::string("CipherTextAllocationFailure");}::memset(CipherText,0xFB,Encryptor.CiphertextLength(PlainTextLength));//EncryptionEncryptor.Encrypt(rng,reinterpret_cast<constbyte*>(PlainText.c_str()),PlainTextLength,CipherText);////Takeabreak...//std::cout<<std::endl;//ScratchforDecryptionunsignedintRecoveredTextLength=Decryptor.MaxPlaintextLength(CipherTextLength);if(0==RecoveredTextLength){throwstd::string("ciphertextLengthisnotvalid(toolongortooshort)");}//DecryptionBufferchar*RecoveredText=newchar[RecoveredTextLength];if(NULL==RecoveredText){throwstd::string("RecoveredTextCipherTextAllocationFailure");}::memset(RecoveredText,0xFB,PlainTextLength);//DecryptionDecryptor.Decrypt(rng,CipherText,CipherTextLength,reinterpret_cast<byte*>(RecoveredText));//Diagnosticsstd::cout<<"Therecoveredtextlengthforthiscipheris"<<RecoveredTextLength<<std::endl;std::cout<<"Recoveredtext:"<<std::endl<<"'"<<RecoveredText<<"'"<<std::endl;//Cleanupif(NULL!=CipherText){delete[]CipherText;}if(NULL!=RecoveredText){delete[]RecoveredText;}}catch(CryptoPP::Exception&e){std::cerr<<"Crypto++Error:"<<e.what()<<std::endl;return-3;}catch(std::string&s){std::cerr<<"Error:"<<s<<std::endl;return-2;}catch(...){std::cerr<<"UnknownError"<<std::endl;return-1;}return0;}
解决方案
解决方案二:
这样行吗,不确定,你试一下。CryptoPP::ECIES<ECC_ALGORITHM>::EncryptorEncryptor(PrivateKey);CryptoPP::ECIES<ECC_ALGORITHM>::DecryptorDecryptor(PublicKey);
另外,私钥加密,公钥解密也没什么实用意义呀?你的需求是什么?直接用签名/验签不行吗?