简体   繁体   English

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

[英]How to sync Crypto++ RSA with C# RSA crypto service provider?

I crypt a string text with use of Crypto++, but when want to decrypt it by C# RSA crypto service provider I have an exception.我使用 Crypto++ 加密了一个字符串文本,但是当想要通过 C# RSA 加密服务提供者解密它时,我有一个例外。

My code produces same cipher string when encrypt a same string with constant public key by Crypto++ in several time, while there are different results (cipher string) with use of C# RSA crypto service provider.我的代码在多次使用 Crypto++ 用常量公钥加密相同的字符串时产生相同的密码字符串,而使用 C# RSA 加密服务提供程序有不同的结果(密码字符串)。

Is main reason of this problem (run-time error) related to different type of RSA?此问题(运行时错误)的主要原因是否与不同类型的 RSA 相关?

My encryption code using Crypto++ is in below:我使用 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);

And the Crypto++ decryption code is:而 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());

the encryption and decryption operations are done well in same side, but there is mentioned problem in decryption operation in other side.加密和解密操作在同一侧完成得很好,但在另一侧的解密操作中提到了问题。

for encryption use this code:加密使用此代码:

    RSAES_OAEP_SHA_Encryptor e(publicKey);
    string cipher;
    StringSource stringSource(message, true,
        new PK_EncryptorFilter(rng, e,
            new StringSink(cipher)
        )
    );

and decryption:和解密:

    RSAES_OAEP_SHA_Decryptor d(privateKey);
    StringSource stringSource(cipher, true,
        new PK_DecryptorFilter(rng, d,
            new StringSink(recovered)
        )
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM