“坏数据” CryptographicException [英] "Bad Data" CryptographicException

查看:196
本文介绍了“坏数据” CryptographicException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我只是为了学术目的而写下面的代码。我说这是因为我不把这个放在生产环境,因此绕过我需要做的一些开销,如果我是,我只需要能够加密/解密一个字符串使用下面的代码。我能够做到这一点,但由于某种原因,我开始收到CryptographicException坏数据,不知道是什么可能导致的问题。

First, I have only written the code below for academic purposes. The reason I say this is because I am not putting this in a production environment, and therefor am "bypassing" some of the overhead that I would need to do if I was, I simply need to be able to encrypt/decrypt a string using the code below. I was able to do it a few time, but for some reason, I started receiving "CryptographicException Bad Data" and am not sure what might be causing the problem.

   private string RSAEncrypt(string value)
    {
        byte[] encryptedData = Encoding.Unicode.GetBytes(value);

        CspParameters cspParams = new CspParameters();
        cspParams.KeyContainerName = _rsaContainerName;
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048,cspParams))
        {
            encryptedData = RSA.Encrypt(encryptedData, false);
            return Convert.ToBase64String(encryptedData);

        }

    }



    private string RSADecrypt(string value)
    {

        byte[] encryptedData = Encoding.Unicode.GetBytes(value);

        CspParameters cspParams = new CspParameters();
        cspParams.KeyContainerName = _rsaContainerName;
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048,cspParams))
        { 
            encryptedData = RSA.Decrypt(encryptedData,false);
            return Convert.ToBase64String(encryptedData);

        }
    }

RSADecrypt调用。

It is only throwing this exception on the RSADecrypt call.

有什么想法吗?我读的地方可能与传递到RSA.Decrypt的加密数据的预期大小有关。

Any ideas? I read somewhere it might have to do with the expected size of encryptedData that is passed into RSA.Decrypt.

感谢
}

推荐答案


  • 使用字符串编码来转换明文,即 Encoding.Unicode

    使用Base-64来回转换加密的数据(即 / From] Base64String );

    Convert the encrypted data back and forth using Base-64 (i.e. Convert.[To/From]Base64String);

    b

    private string RSAEncrypt(string value)
    {
        byte[] plaintext = Encoding.Unicode.GetBytes(value);
    
        CspParameters cspParams = new CspParameters();
        cspParams.KeyContainerName = _rsaContainerName;
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048,cspParams))
        {
            byte[] encryptedData = RSA.Encrypt(plaintext, false);
            return Convert.ToBase64String(encryptedData);
        }
    }
    
    private string RSADecrypt(string value)
    {
        byte[] encryptedData = Convert.FromBase64String(value);
    
        CspParameters cspParams = new CspParameters();
        cspParams.KeyContainerName = _rsaContainerName;
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(2048,cspParams))
        { 
            byte[] decryptedData = RSA.Decrypt(encryptedData,false);
            return Encoding.Unicode.GetString(decryptedData);
        }
    }
    

    这篇关于“坏数据” CryptographicException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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