OAEPまたはPKCS#1を使用せずに、Crypto++を使用して生のRSAでメッセージを暗号化する方法がわかりません。誰かが私にいくつかのサンプルコードを見せてくれるほど親切にしてくれませんか?
どこを見ればよいかがわかれば、これは非常に簡単です。Crypto ++wikiのRawRSAです。以下のコードはページから取得したものです。
暗号化
Integer n("0xbeaadb3d839f3b5f"), e("0x11"), d("0x21a5ae37b9959db9");
RSA::PublicKey pubKey;
pubKey.Initialize(n, e);
///////////////////////////////////////////////////////////////
Integer m, c;
string message = "secret";
cout << "message: " << message << endl;
// Treat the message as a big endian byte array
m = Integer((const byte *)message.data(), message.size());
cout << "m: " << hex << m << endl;
// Encrypt
c = pubKey.ApplyFunction(m);
cout << "c: " << hex << c << endl;
復号化
Integer n("0xbeaadb3d839f3b5f"), e("0x11"), d("0x21a5ae37b9959db9");
AutoSeededRandomPool prng;
RSA::PrivateKey privKey;
privKey.Initialize(n, e, d);
///////////////////////////////////////////////////////////////
Integer c("0x3f47c32e8e17e291"), r;
string recovered;
// Decrypt
r = privKey.CalculateInverse(prng, c);
cout << "r: " << hex << r << endl;
// Round trip the message
size_t req = r.MinEncodedSize();
recovered.resize(req);
r.Encode((byte *)recovered.data(), recovered.size());
cout << "recovered: " << recovered << endl;
出力例は次のとおりです。
$ ./cryptopp-raw-rsa.exe
message: secret
m: 736563726574h
c: 3f47c32e8e17e291h
r: 736563726574h
recovered: secret
注意点が1つありますc = m ^ e mod n
。したがって、平文テキストのサイズと暗号文のサイズにはいくつかの制限があります。基本的に、m
。c
より小さくする必要がありますn
。この例では、文字列をに変換した場合よりも大きいため、文字列secret
をに置き換えると失敗します。now is the time for all good men to come to the aid of their country
n
Integer
関数を使用してプレーンテキストの最大サイズを取得し、を使用MaxPreImage()
して暗号文の最大サイズを取得できますMaxImage()
。
PCからのメッセージを暗号化して署名する必要があります。次に、デバイスはメッセージを復号化して検証します。次に、デバイスは暗号化されたメッセージに応答してサインオンします。PCはメッセージを復号化し、後で確認します。
表面的には、これはリプレイ攻撃を受けるように見えます。保護付きのプロトコルが必要になる場合があります。