gpt4 book ai didi

c++ - 为什么 Crypto++ 认为我的消息比它们实际的要大?

转载 作者:行者123 更新时间:2023-11-30 05:34:56 25 4
gpt4 key购买 nike

我正在使用 C++ 的 Crypto++ 库,使用磁盘上的公钥和我的私钥来加密 128 位消息。但是,当我调用我的函数时,程序终止并显示错误消息:

terminate called after throwing an instance of 'CryptoPP::InvalidArgument' what(): RSA/OAEP-MGF1(SHA-1): message length of 256 exceeds the maximum of 214 for this public key

我不确定为什么它认为我的消息是 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。我想我需要弄清楚为什么它会增加大小。

最佳答案

I'm not sure why it thinks my messages are 256-bit when they are clearly 128-bit. Below is my code:

那些是字节,而不是位。

您使用的是什么大小的 RSA 模数?我猜它是一个 2048 位的 key ,即 256 字节,由于 OAEP 填充,剩下 ≈214 字节。

所以问题就变成了,在这期间plaincipher1的大小是多少:

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++ 提供了其中的几个。

关于c++ - 为什么 Crypto++ 认为我的消息比它们实际的要大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34035207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com