简体   繁体   English

RSACryptoServiceProvider使用公钥解密失败

[英]RSACryptoServiceProvider Decrypt failing using public key

I am thinking I should be able to decrypt using the public key when the server sends data by encrypting with the private key. 我想当服务器通过使用私钥加密发送数据时,我应该能够使用公钥解密。 But this is throwing an error. 但这引发了错误。

    var message = "test";

    byte[] encryped;
    byte[] decryped;

    {
        string path = ".\\1.pfx";
        string password = "1";
        X509Certificate2 certificate = new X509Certificate2(path, password);
        RSACryptoServiceProvider provider = 
                certificate.PrivateKey as RSACryptoServiceProvider;

        encryped = RSAEncrypt(provider, Encoding.Unicode.GetBytes(message));
    }

    {
        string path = ".\\1.cer";
        X509Certificate2 certificate = new X509Certificate2(path);
        RSACryptoServiceProvider provider = 
                certificate.PublicKey.Key as RSACryptoServiceProvider;

        decryped = RSADecrypt(provider, encryped);
    }

    Assert.IsTrue(message == Encoding.Unicode.GetString(decryped));

The methods themselves are simple. 方法本身很简单。

    public static byte[] RSAEncrypt(RSACryptoServiceProvider rsa, byte[] plaintext)
    {
        byte[] encryptedData;
        encryptedData = rsa.Encrypt(plaintext, true);
        return encryptedData;
    }



    public static byte[] RSADecrypt(RSACryptoServiceProvider rsa, byte[] ciphertext)
    {
        byte[] decryptedData;
        decryptedData = rsa.Decrypt(ciphertext, true);
        return decryptedData;
    }

This is throwing the following error. 这将引发以下错误。

System.Security.Cryptography.CryptographicException: 'Error occurred while decoding OAEP padding.' System.Security.Cryptography.CryptographicException:'解码OAEP填充时发生错误。

.NET does not expose "raw" (or "unpadded") RSA operations. .NET不公开“原始”(或“未填充”)RSA操作。

In a signing operation with RSA, the signer takes the hash algorithm and the hash value, builds the padded structured message around it, and does the RSA operation using the private key. 在使用RSA进行签名的操作中,签名者采用哈希算法和哈希值,在其周围构建填充的结构化消息,并使用私钥进行RSA操作。

In a verification operation, the verifier does the RSA operation using the public key, checks that the padding structure is intact, and (directly or indirectly) checks that the hash algorithm and hash value match the expected results. 在验证操作中,验证者使用公钥执行RSA操作,检查填充结构是否完整,并(直接或间接)检查哈希算法和哈希值是否与预期结果匹配。

In an encryption operation the message is put into an encryption padding structure and the RSA operation is performed with the recipient's public key. 在加密操作中,将消息放入加密填充结构中,并使用收件人的公共密钥执行RSA操作。

In a decryption operation the message goes through the RSA operation using the recipient's private key, the padding structure is verified, and then the encapsulated message is returned. 在解密操作中,消息使用接收者的私钥通过RSA操作,验证填充结构,然后返回封装的消息。

|-----------|--------------|-----------------|
| Operation | Pub/Priv Key | Add/Rem Padding |
|-----------|--------------|-----------------|
| Sign      | Private      | Add PKCS#1/PSS  |
| Encrypt   | Public       | Add PKCS#1/OAEP |
| Decrypt   | Private      | Remove (Encrypt)|
| Verify    | Public       | Remove (Sign)   |
|-----------|--------------|-----------------|

Since you have a signature you need an operation which uses the public key with the RSA operation and removes padding (instead of adds it). 由于您具有签名,因此需要一个将公钥与RSA操作结合使用并删除填充(而不是添加填充)的操作。 That means only VerifyData or VerifyHash will do what you want. 这意味着只有VerifyDataVerifyHash可以执行您想要的操作。

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

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