c# - 如何将 Crypto++ RSA 与 C# RSA 加密服务提供商同步?

标签 c# c++ encryption cryptography crypto++

我使用 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/

相关文章:

c++ - __FILE__ 编译时的宏操作处理

Java CryptoCipher 不消耗所有输入字节

c# - 安全存储密码的最佳方法

c# - 处理数据类型转换的通用函数

c# - Entity Framework `AsNoTracking` 不适用于匿名投影

c++ - 两个指针指向同一个引用

java - 如何解密从 Mifare Desfire EV1 发送的第一条消息

C#:定义类变量、默认值和获取设置不起作用?

c# - IntPtr 到 string.Format 中的十六进制字符串

c++ - 在没有手动分配缓冲区的情况下使用 sprintf