2012-03-23 221 views
4

我需要在PC和支持使用SHA1进行RSA加密和签名的设备之间建立安全通信。因为我已经在我的应用程序的其他部分使用了Crypto ++,所以我想为此使用Crypto ++。RAW RSA使用Crypto ++加密和解密

该设备是非常原始的,但允许执行我写的程序。它内置了原始的RSA和SHAa功能;但是,它的内存很少,2K字节要精确。

我必须加密并签名来自PC的消息。然后设备解密并验证消息。设备将回复一条加密的消息并在其上签名。 PC将解密消息并在之后进行验证。我已经使用内置函数在设备内部使用SHA1实现了原始RSA加密,签名和验证。这些消息足够短,可以在一轮中完成。

但是,我不知道如何在不涉及OAEP或PKCS#1的情况下使用Crypto ++以原始RSA加密消息。有人能够给我看一些示例代码吗?万分感谢!

回答

2

我不知道如何使用加密加密与原RSA消息++没有 涉及OAEP或PKCS#1。有人能够给我看一些示例代码吗?

当你知道去哪里看看就很容易:Raw RSA来自Crypto ++ wiki。下面的代码是从页面中提取的。


加密

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 

有一点需要注意:c = m^e mod n,所以有上感叹文字大小和密文大小一些限制。本质上,mc必须小于n。在此示例中,将字符串secret替换为now is the time for all good men to come to the aide of their country将会失败,因为其大于n(当转换为Integer时)。

您可以通过功能MaxPreImage()获得最大纯文本大小,最大密文大小为MaxImage()


我必须加密,并从PC登录的消息。然后设备解密 并验证消息。该设备然后将回复加密的消息 并在其上签名。 PC将解密消息并在之后进行验证。

从表面上看,这看起来会遭受重播攻击。您可能需要一个带有保护的协议。

1

这是我第一次使用Crypto ++进行RSA加密和解密时写的演示函数。我只是为了理解基础而写的。我希望它能帮助:

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

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 

    CryptoPP::AutoSeededRandomPool rng; 

    // Create a private RSA key and write it to a file using DER. 
    CryptoPP::RSAES_OAEP_SHA_Decryptor priv(rng, 4096); 
    CryptoPP::TransparentFilter privFile(new CryptoPP::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; 
    CryptoPP::RSAES_OAEP_SHA_Decryptor pri(rng, 2048); 
    CryptoPP::TransparentFilter privSink(new CryptoPP::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 
    CryptoPP::InvertibleRSAFunction params; 
    params.GenerateRandomWithKeySize(rng, 1536); 

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

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

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

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

    assert(plain == decrypted_data); 
} 
+0

感谢您的代码。我了解你的代码。但是,我的情况有点不同。 – crackpot 2012-03-23 03:16:48

+0

该代码进行RSA加密和解密。如果这不是你想要的,我只会删除它。 – 01100110 2012-03-23 03:23:57

+0

对不起,评论是用返回键发布的,但我只是想插入一行。 我已经进一步阐述了我在这个问题上的处境。 我真的很感谢你的帮助。^_^ – crackpot 2012-03-23 03:43:59