zngsai 2021-07-23 18:33
浏览 32
已结题

C#之RSACryptoServiceProvider加密与解密问题

//有私钥strPrivateKey,有公钥strPublickKey,该私钥不包含该公钥;
//有密文strToDecrypt

RSACryptoServiceProvider cryptoServiceProvider = new RSACryptoServiceProvider();
cryptoServiceProvider.FromXmlString(strPrivateKey);

//记录下私钥对应的公钥
string strPublicKeyOfPrivateKey = cryptoServiceProvider.ToXmlString(false);

strDecrypted = cryptoServiceProvider.Decrypt(strToDecrypt, false);

//记录下密文对应的SHA1签名
strSignData = cryptoServiceProvider.Decrypt(strToDecrypt, SHA1);
//strDecrypted为能够正确解密的数据,接下来逆运算

RSACryptoServiceProvider cryptoServiceProvider2 = new RSACryptoServiceProvider();
cryptoServiceProvider2.FromXmlString(strPublicKeyOfPrivateKey);
strEncrypted = cryptoServiceProvider2.Encrypt(strDecrypted, false);

//按说strEncrypted应该就是strToDecrypt啊,但是就是不对,声明字符串转换肯定没问题。

//不过
RSACryptoServiceProvider cryptoServiceProvider3 = new RSACryptoServiceProvider();
cryptoServiceProvider3.FromXmlString(strPublickKey);//此处为公钥
bool bOK = cryptoServiceProvider3.VerifyData(strToDecrypt,SHA1,strSignData);

//bOK为true验证通过。

//问题:
//根据这些条件,怎么样才能够正确的加密,确保strEncrypted==strToDecrypt?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    问题事件

    • 系统已结题 7月31日
    • 创建了问题 7月23日