我在用Crypto++ http://git@github.com:weidai11/cryptopp.git密码学相关作品的图书馆。任务的子部分是加密和解密文本。该消息的长度最多为 256 个字符,包含字母数字、空格、点和特殊字符。
这段代码适用于文本长度小于或等于8的情况。但是之后它无法解密加密的文本。
// g++ -std=c++1y crypto.cpp -I /home/shravan40/cryptopp/build -lcryptopp
#include <iostream>
#include <cryptopp/rsa.h>
#include <cryptopp/integer.h>
#include <cryptopp/osrng.h>
int main(){
// Keys
CryptoPP::Integer n("0xbeaadb3d839f3b5f"), e("0x11"), d("0x21a5ae37b9959db9");
CryptoPP::RSA::PrivateKey privKey;
privKey.Initialize(n, e, d);
CryptoPP::RSA::PublicKey pubKey;
pubKey.Initialize(n, e);
// Encryption
std::string message = "Shravan Kumar";
CryptoPP::Integer m((const byte *)message.data(), message.size());
std::cout << "m: " << m << std::endl;
// m: 126879297332596.
CryptoPP::Integer c = pubKey.ApplyFunction(m);
std::cout << "c: " << std::hex << c << std::endl;
// c: 3f47c32e8e17e291h
// Decryption
CryptoPP::AutoSeededRandomPool prng;
CryptoPP::Integer r = privKey.CalculateInverse(prng, c);
std::cout << "r: " << std::hex << r << std::endl;
// r: 736563726574h
std::string recovered;
recovered.resize(r.MinEncodedSize());
r.Encode((byte *)recovered.data(), recovered.size());
std::cout << "recovered: " << recovered << std::endl;
// recovered: Expected : (Shravan Kumar), Received -> y5��dqm0
return 0;
}
理查德·克里顿是正确的在他的评论中 https://stackoverflow.com/questions/42046991/encrypte-decrypte-a-text-using-raw-rsa-algorithim-cryptopp#comment71267166_42046991通常使用混合加密(非对称密码(例如 RSA)和对称密码(例如 AES)。
对于这类不安全的例子尽管通常只需要将明文分割成与模数相同大小的部分n
。因此,在您的情况下,只需将每 8 个字节/字符放在一起,并将其用作(大端)数字。由于输入似乎是 ASCII,这 8 个字节的最高位将始终设置为零,因此加密/解密不会有任何问题。 RSA 的输入当然必须始终小于n
.
当然,您可能必须想出一种聪明的方法来处理字符串的最后部分。
Notes:
- 如果还没有被告知:没有填充的原始 RSA 也不安全。因此,如果在现场实施此示例,则不仅仅是密钥大小会成为问题。
- 我不知道你在解密方面做了什么。我想你应该再看看你的教科书。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)