私はCrypto++ライブラリを暗号関連の仕事に使用しています。タスクのサブ部分は、テキストを暗号化して解読することです。メッセージは英数字の数字スペースと特殊文字を含む最大256文字です。Crypto ++で生RSAアルゴリズムを使用してメッセージを暗号化および復号化しますか?
このコードは、テキストの長さが8以下で動作していますが、その後は暗号化されたテキストの復号化に失敗します。
// g++ -std=c++1y crypto.cpp -I /home/shravan40/cryptopp/build -lcryptopp
#include <iostream>
#include <cryptopp/rsa.h>
#include <cryptopp/integer.h>
#include <cryptopp/osrng.h>
int main(){
// Keys
CryptoPP::Integer n("0xbeaadb3d839f3b5f"), e("0x11"), d("0x21a5ae37b9959db9");
CryptoPP::RSA::PrivateKey privKey;
privKey.Initialize(n, e, d);
CryptoPP::RSA::PublicKey pubKey;
pubKey.Initialize(n, e);
// Encryption
std::string message = "Shravan Kumar";
CryptoPP::Integer m((const byte *)message.data(), message.size());
std::cout << "m: " << m << std::endl;
// m: 126879297332596.
CryptoPP::Integer c = pubKey.ApplyFunction(m);
std::cout << "c: " << std::hex << c << std::endl;
// c: 3f47c32e8e17e291h
// Decryption
CryptoPP::AutoSeededRandomPool prng;
CryptoPP::Integer r = privKey.CalculateInverse(prng, c);
std::cout << "r: " << std::hex << r << std::endl;
// r: 736563726574h
std::string recovered;
recovered.resize(r.MinEncodedSize());
r.Encode((byte *)recovered.data(), recovered.size());
std::cout << "recovered: " << recovered << std::endl;
// recovered: Expected : (Shravan Kumar), Received -> y5��dqm0
return 0;
}
メッセージはキー長に制限されるため、通常はメッセージを直接暗号化するためにPKIを使用しません。メッセージテキストを暗号化するには、対称アルゴリズム(CBCモードのAESなど)を使用する必要があります。公開鍵を使用して対称鍵を暗号化します。 –
Crypto ++ wikiの[RSA Cryptography](http://www.cryptopp.com/wiki/RSA_Cryptography)も参照してください。 – jww