如何将Crypto ++ RSA与C#RSA加密服务提供商同步? [英] How to sync Crypto++ RSA with C# RSA crypto service provider?

查看:121
本文介绍了如何将Crypto ++ RSA与C#RSA加密服务提供商同步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的代码产生相同的密码当使用C#RSA加密服务提供商时,使用Crypto ++在几个时间内使用常量公钥加密同一个字符串,而不同的结果(密码字符串)。



与不同类型的RSA有关的这个问题(运行时错误)的主要原因?



我使用Crypto ++的加密代码如下:

  ((char *)hi,2); 
整数messageInteger((const byte *)message.data(),message.size());
整数cipherMessage = hostPublicKey.ApplyFunction(messageInteger);
size_t len = cipherMessage.MinEncodedSize();
string str;
str.resize(len);
cipherMessage.Encode((byte *)str.data(),str.size(),Integer :: UNSIGNED);

Crypto ++解密代码是:

 整数cipherMessage1((byte *)str.data(),str.size()); 
int size1 = cipherMessage1.ByteCount();
整数plainInteger = privateKey.CalculateInverse(prng,cipherMessage1);
string recover;
size_t req = plainInteger.MinEncodedSize();
recover.resize(req);
plainInteger.Encode((byte *)recover.data(),restored.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(recover)

);


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.

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.

Is main reason of this problem (run-time error) related to different type of RSA?

My encryption code using Crypto++ is in below:

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:

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)
        )
    );

这篇关于如何将Crypto ++ RSA与C#RSA加密服务提供商同步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆