我使用 Crypto++ 对字符串文本进行加密,但是当我想通过 C# RSA 加密服务提供商对其进行解密时,出现异常。
当使用 Crypto++ 使用常量公钥加密同一个字符串时,我的代码多次生成相同的密码字符串,而使用 C# RSA 加密服务提供程序会产生不同的结果(密码字符串)。
此问题(运行时错误)的主要原因是否与不同类型的 RSA 相关?
我使用 Crypto++ 的加密代码如下:
string message((char*)"hi", 2);
Integer messageInteger((const byte *)message.data(), message.size());
Integer cipherMessage = hostPublicKey.ApplyFunction(messageInteger);
size_t len = cipherMessage.MinEncodedSize();
string str;
str.resize(len);
cipherMessage.Encode((byte *)str.data(), str.size(), Integer::UNSIGNED);
Crypto++解密代码为:
Integer cipherMessage1((byte *)str.data(), str.size());
int size1 = cipherMessage1.ByteCount();
Integer plainInteger = privateKey.CalculateInverse(prng, cipherMessage1);
string recovered;
size_t req = plainInteger.MinEncodedSize();
recovered.resize(req);
plainInteger.Encode((byte *)recovered.data(), recovered.size());
在同一端加密和解密操作都很好,但在另一端的解密操作中提到了问题。
最佳答案
加密使用此代码:
RSAES_OAEP_SHA_Encryptor e(publicKey);
string cipher;
StringSource stringSource(message, true,
new PK_EncryptorFilter(rng, e,
new StringSink(cipher)
)
);
和解密:
RSAES_OAEP_SHA_Decryptor d(privateKey);
StringSource stringSource(cipher, true,
new PK_DecryptorFilter(rng, d,
new StringSink(recovered)
)
);
关于c# - 如何将 Crypto++ RSA 与 C# RSA 加密服务提供商同步?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41848700/