5

PCと、SHA1によるRSA暗号化と署名をサポートするデバイスとの間に安全な通信を確立する必要があります。アプリケーションの他の部分ですでにCrypto++を使用しているので、これにもCrypto++を利用したいと思います。

デバイスは非常に原始的ですが、私が書いたプログラムを実行することができます。生のRSAおよびSHAa関数が組み込まれています。ただし、処理するメモリはほとんどなく、正確には2Kバイトです。

PCからのメッセージを暗号化して署名する必要があります。次に、デバイスはメッセージを復号化して検証します。次に、デバイスは暗号化されたメッセージに応答してサインオンします。PCはメッセージを復号化し、後で確認します。組み込み関数を使用して、デバイス内にSHA1を使用して、生のRSA暗号化、署名、および検証を実装しました。メッセージは、1回のラウンドで実行できるほど短いものです。

ただし、OAEPまたはPKCS#1を使用せずに、Crypto++を使用して生のRSAでメッセージを暗号化する方法がわかりません。誰かが私にいくつかのサンプルコードを見せてくれるほど親切にしてくれませんか?トンありがとう!

4

2 に答える 2

4

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。したがって、平文テキストのサイズと暗号文のサイズにはいくつかの制限があります。基本的に、mcより小さくする必要がありますn。この例では、文字列をに変換した場合よりも大きいため、文字列secretをに置き換えると失敗します。now is the time for all good men to come to the aid of their countrynInteger

関数を使用してプレーンテキストの最大サイズを取得し、を使用MaxPreImage()して暗号文の最大サイズを取得できますMaxImage()


PCからのメッセージを暗号化して署名する必要があります。次に、デバイスはメッセージを復号化して検証します。次に、デバイスは暗号化されたメッセージに応答してサインオンします。PCはメッセージを復号化し、後で確認します。

表面的には、これはリプレイ攻撃を受けるように見えます。保護付きのプロトコルが必要になる場合があります。

于 2013-10-04T02:15:17.920 に答える
2

これは、Crypto++を使用してRSA暗号化と復号化を最初に行ったときに作成したデモ関数です。基本を理解するためだけに書きました。お役に立てば幸いです。

#include <cryptopp/files.h>
#include <cryptopp/modes.h>
#include <cryptopp/osrng.h>
#include <cryptopp/rsa.h>
#include <cryptopp/sha.h>

using namespace CryptoPP;

void rsa_examples()
{
    // Keys created here may be used by OpenSSL.
    //
    // openssl pkcs8 -in key.der -inform DER -out key.pem -nocrypt 
    // openssl rsa -in key.pem -check

    AutoSeededRandomPool rng;

    // Create a private RSA key and write it to a file using DER.
    RSAES_OAEP_SHA_Decryptor priv( rng, 4096 );
    TransparentFilter privFile( new FileSink("rsakey.der") );
    priv.DEREncode( privFile );
    privFile.MessageEnd();

    // Create a private RSA key and write it to a string using DER (also write to a file to check it with OpenSSL).
    std::string the_key;
    RSAES_OAEP_SHA_Decryptor pri( rng, 2048 );
    TransparentFilter privSink( new StringSink(the_key) );
    pri.DEREncode( privSink );
    privSink.MessageEnd();

    std::ofstream file ( "key.der", std::ios::out | std::ios::binary );
    file.write( the_key.data(), the_key.size() );
    file.close();

    // Example Encryption & Decryption
    InvertibleRSAFunction params;
    params.GenerateRandomWithKeySize( rng, 1536 );

    std::string plain = "RSA Encryption", cipher, decrypted_data;

    RSA::PrivateKey privateKey( params );
    RSA::PublicKey publicKey( params );

    RSAES_OAEP_SHA_Encryptor e( publicKey );
    StringSource( plain, true, new PK_EncryptorFilter( rng, e, new StringSink( cipher )));

    RSAES_OAEP_SHA_Decryptor d( privateKey );
    StringSource( cipher, true, new PK_DecryptorFilter( rng, d, new StringSink( decrypted_keydata )));

    assert( plain == decrypted_data );
}
于 2012-03-23T03:01:31.713 に答える