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.