Encryption and decryption of RAW RSA using Crypto ++

I need to establish a secure connection between a PC and a device that supports RSA encryption and signature with SHA1. Since I already used Crypto ++ in another part of my application, I would also like to use Crypto ++ for this.

The device is very primitive, but allows you to execute the program that I write on it. It has built-in RSA and SHAa functions; However, it has very little memory for working with 2 Kbytes, to be exact.

I need to encrypt and sign a message from a PC. The device then decrypts and verifies the message. The device will then respond to the encrypted message and sign it. After that, the PC decrypts the message and checks it. I implemented raw RSA encryption, signature and verification using SHA1 inside the device using the built-in functions. Messages are short enough to be completed in one round.

However, I do not know how to encrypt a message using raw RSA using Crypto ++ without the participation of OAEP or PKCS # 1. Can anyone be kind enough to show me some code? Thanks a ton!

+4
source share
2 answers

I do not know how to encrypt a message using raw RSA, using Crypto ++ without including OAEP or PKCS # 1. Can someone please give me some sample code?

It's easy enough when you know where to look: Raw RSA from the Crypto ++ wiki. The code below was taken from the page.


Encryption

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; 

decryption

 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; 

Here's an example output:

 $ ./cryptopp-raw-rsa.exe message: secret m: 736563726574h c: 3f47c32e8e17e291h r: 736563726574h recovered: secret 

There is one caveat: c = m ^ e mod n , so there are some restrictions on the text size and font size. Essentially, m and c must be less than n . In this example, replacing the secret string with now is the time for all good men to come to the aide of their country will fail because it will be greater than n when converting to Integer .

The maximum size of text text can be obtained using the MaxPreImage() function, and the maximum size of text to be encrypted is MaxImage() .


I need to encrypt and sign a message from a PC. The device then decrypts and verifies the message. The device will then respond to the encrypted message and sign it. After that, the PC decrypts the message and checks it.

At first glance, it looks like he will suffer from repeated attacks. You may need a security protocol.

+2
source

Here is a demo function that I wrote when I first performed RSA encryption and decryption using Crypto ++. I wrote this to understand the basics. Hope this helps:

 #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 ); } 
+1
source

Source: https://habr.com/ru/post/1403011/


All Articles