我不知道如何使用加密加密与原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
,所以有上感叹文字大小和密文大小一些限制。本质上,m
和c
必须小于n
。在此示例中,将字符串secret
替换为now is the time for all good men to come to the aide of their country
将会失败,因为其大于n
(当转换为Integer
时)。
您可以通过功能MaxPreImage()
获得最大纯文本大小,最大密文大小为MaxImage()
。
我必须加密,并从PC登录的消息。然后设备解密 并验证消息。该设备然后将回复加密的消息 并在其上签名。 PC将解密消息并在之后进行验证。
从表面上看,这看起来会遭受重播攻击。您可能需要一个带有保护的协议。
来源
2013-10-04 02:15:17
jww
感谢您的代码。我了解你的代码。但是,我的情况有点不同。 – crackpot 2012-03-23 03:16:48
该代码进行RSA加密和解密。如果这不是你想要的,我只会删除它。 – 01100110 2012-03-23 03:23:57
对不起,评论是用返回键发布的,但我只是想插入一行。 我已经进一步阐述了我在这个问题上的处境。 我真的很感谢你的帮助。^_^ – crackpot 2012-03-23 03:43:59