为什么Crypto++认为我的消息比实际情况更大
Why does Crypto++ think my messages are larger than they are?
我正在使用C++的Crypto++
库,用磁盘上的128位消息的公钥和私钥对其进行加密。然而,当我调用我的函数时,程序终止,并显示错误消息:
抛出的实例后调用的terminate"CryptoPP::InvalidArgument"what():RSA/OAEP-MGF1(SHA-1):消息256的长度超过了该公钥的最大值214
我不知道为什么它认为我的消息是256位的,而它们显然是128位的。以下是我的代码:
std::string encryptWithBothKeys(std::string key_name1, std::string key_name2, std::string plain){
std::string cipher1, cipher2;
AutoSeededRandomPool rng;
RSA::PublicKey pub_key;
RSA::PrivateKey priv_key;
loadPublicKey(key_name1, pub_key);
loadPrivateKey(key_name2, priv_key);
RSAES_OAEP_SHA_Encryptor e_pub(pub_key);
RSAES_OAEP_SHA_Encryptor e_priv(priv_key);
StringSource ss1(plain, true,
new PK_EncryptorFilter(rng, e_pub,
new StringSink(cipher1)
)
);
StringSource ss2(cipher1, true,
new PK_EncryptorFilter(rng, e_priv,
new StringSink(cipher2)
)
);
return cipher2;
}
void load(const std::string& filename, BufferedTransformation& bt){
FileSource file(filename.c_str(), true /*pumpAll*/);
file.TransferTo(bt);
bt.MessageEnd();
}
void loadPrivateKey(const std::string& filename, PrivateKey& key){
ByteQueue queue;
load(filename, queue);
key.Load(queue);
}
void loadPublicKey(const std::string& filename, PublicKey& key){
ByteQueue queue;
load(filename, queue);
key.Load(queue);
}
在主函数中,我这样称呼它:
std::string encrypted_msg = encryptWithBothKeys("their.pub", "my.key", "0123456789ABCDEF");
有什么建议吗?
编辑:第一次加密后的密文大小为256。我想我需要弄清楚为什么它的尺寸越来越大。
我不知道为什么它认为我的消息是256位的,而它们显然是128位的。以下是我的代码:
这些是字节,而不是位。
您使用的RSA模数大小是多少?我猜它是一个2048位的密钥,即256字节,由于OAEP填充,剩下≈214字节。
因此,问题变成了,在期间,plain
和cipher1
的大小是多少
StringSource ss1(plain, true,
new PK_EncryptorFilter(rng, e_pub,
new StringSink(cipher1)
)
);
我猜想plain1
已经足够小了。然而,当它被填充和加密时,它会扩展到256字节作为cipher1
。因此,执行此操作时消息cipher1
太大:
StringSource ss2(cipher1, true,
new PK_EncryptorFilter(rng, e_priv,
new StringSink(cipher2)
)
);
此外,以上是否应使用PK_DecryptorFilter
?你确定要再次加密吗?如果是,那么为什么要使用e_priv
?
使用私钥加密不是有效的加密操作。如果你想要"用私钥加密",这通常意味着你想要一个带恢复的概率签名方案(PSSR)。Crypto++提供了其中的几个。
相关文章:
- 如何在不违反类型别名规则的情况下解释消息负载?
- 我需要线程函数在不停止实际程序的情况下,每2秒打印一次随机数
- 编译器在这里做什么,允许在很少进行实际比较的情况下比较许多值
- 有谁知道ziplib是否能够在不实际提取所有文件的情况下验证zip库
- 从模板实例化/类型推断中查找错误消息的实际来源
- 有没有办法在不使用 set_buff() 的情况下从文件中读取并填充消息增益
- 有没有办法在不覆盖实际分配的情况下使用 vulkan 内部分配回调?
- 为什么GetMessage在不发送任何消息的情况下退出我的程序
- cout 输出乱码消息行,而不是实际用户的 cout 语句
- 如何在不复制内存的情况下复制Zeromq中的消息
- 关于在这种情况下消息队列与共享内存的适用性或适用性
- idTCPServer在没有客户端活动的情况下向idTCPClient发送消息
- Borland C++在没有消息的情况下崩溃
- 为实例变量(即对象)调用getter函数时实际发生的情况
- 在CPU中花费的时间比实际情况更快
- 为什么Crypto++认为我的消息比实际情况更大
- c++中函数参数的实际情况
- 在什么实际情况下bool(std::ifstream) != std::ifstream::good()
- visual当在C++中删除不完整的类型不是错误时,是否存在实际情况
- 在 C 样式类型转换期间,关于强制转换对象的地址空间的实际情况