简体   繁体   English

System.Security.Cryptography.CryptographicException:RSACryptoserviceProvider中的长度错误

[英]System.Security.Cryptography.CryptographicException : Bad length in RSACryptoserviceProvider

I want encrypt and decrypt data using RSACryptoServiceProvider in c# in wp8 project. 我想在wp8项目的c#中使用RSACryptoServiceProvider加密和解密数据。 I am creating asymmetric keys as : 我创建非对称密钥为:

CspParameters parameters = new CspParameters();
parameters.KeyContainerName = "MyContainer";

RSACryptoServiceProvider provider = new RSACryptoServiceProvider(parameters);  

Now I want do encrypt data. 现在我要加密数据。 I am doing: 我在做:

CspParameters parameters = new CspParameters();

parameters.KeyContainerName = "MyContainer";
RSACryptoServiceProvider obj = new RSACryptoServiceProvider(parameters);
byte[] a = Generic.RSAEncrypt(ByteConverter.GetBytes(s[0]),
                              obj.ExportParameters(false), false); 

public static byte[] RSAEncrypt(byte[] DataToEncrypt, RSAParameters RSAKeyInfo,
                                bool DoOAEPPadding)
{
    try {
        byte[] encryptedData;
        //Create a new instance of RSACryptoServiceProvider. 
        CspParameters parameters = new CspParameters();
        parameters.KeyContainerName = "TCSContainer";
        using (RSACryptoServiceProvider RSA = new RSACryptoServiceProvider(parameters))
        {
            //Import the RSA Key information. This only needs 
            //to include the public key information.

            RSA.ImportParameters(RSAKeyInfo);

            //Encrypt the passed byte array and specify OAEP padding.   
            //OAEP padding is only available on Microsoft Windows XP or 
            //later.  
            encryptedData = RSA.Encrypt(DataToEncrypt, DoOAEPPadding);
        }
        return encryptedData;
    } catch (CryptographicException e) {
        //Catch and display a CryptographicException   
        //to the console. 
        //Console.WriteLine(e.Message);
        return null;
    }
}

Now I am getting exception while encypting: 现在,在插入时出现异常:

RSA.EncryptSystem.Security.Cryptography.CryptographicException : Bad length in RSACryptoserviceProvider. 

Stacktrace is: Stacktrace是:

at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
at System.Security.Cryptography.RSACryptoServiceProvider.EncryptKey(SafeKeyHandle pKeyContext, Byte[] pbKey, Int32 cbKey, Boolean fOAEP, ObjectHandleOnStack ohRetEncryptedKey)
at System.Security.Cryptography.RSACryptoServiceProvider.Encrypt(Byte[] rgb, Boolean fOAEP)
at WindowsAppmart.Generic.RSAEncrypt(Byte[] DataToEncrypt, RSAParameters RSAKeyInfo, Boolean DoOAEPPadding)

and message is Bad Length. 和消息是错误的长度。

I am not getting where can I go wrong? 我没有弄错哪里可以去?

RSA is only meant to be used for encrypting small amounts of data. RSA仅用于加密少量数据。 The exact amount you can encrypt depends on the key length + the amount used by the padding. 您可以加密的确切数量取决于密钥长度+填充使用的数量。 A 1024 bit key would allow for a bit above 100 bytes. 1024位密钥将允许100字节以上的位。

Since RSA is quite slow, the usual way to encrypt large messages is using hybrid encryption. 由于RSA速度很慢,因此加密大型邮件的常用方法是使用混合加密。 In hybrid encryption you use a fast symmetric encryption algorithm (like AES ) for encrypting the data with a random key. 在混合加密中,您使用快速对称加密算法(例如AES )来使用随机密钥加密数据。 The random key is then encrypted with RSA and send along with the symmetric key encrypted data. 然后,使用RSA对随机密钥进行加密,并与对称密钥加密数据一起发送。

This indicates that the amound of data you are trying to encrypt is too long. 这表明您尝试加密的数据范围太长。 You should encrypt it in smaller bulks. 您应该以较小的批量对其进行加密。

暂无
暂无

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

相关问题 System.Security.Cryptography.CryptographicException:句柄无效 - System.Security.Cryptography.CryptographicException: The handle is invalid TwilioRequestValidator 中的瞬态 System.Security.Cryptography.CryptographicException - Transient System.Security.Cryptography.CryptographicException in TwilioRequestValidator System.Security.Cryptography.CryptographicException:参数不正确 - System.Security.Cryptography.CryptographicException: The parameter is incorrect System.Security.Cryptography.CryptographicException:'Cryptography_OAEPDecoding' - System.Security.Cryptography.CryptographicException: 'Cryptography_OAEPDecoding' 引发异常:mscorlib.dll中的'System.Security.Cryptography.CryptographicException'其他信息:错误的数据 - Exception thrown: 'System.Security.Cryptography.CryptographicException' in mscorlib.dll Additional information: Bad Data c#,对密码登录进行加密和解密。 System.Security.Cryptography.CryptographicException:错误的数据。 错误 - c# , encrypt and Decrypt for the password login. System.Security.Cryptography.CryptographicException: Bad Data. error 我正在接收 System.Security.Cryptography.CryptographicException:“要解密的数据长度无效。” - I amgetting System.Security.Cryptography.CryptographicException: 'Length of the data to decrypt is invalid.' System.Security.Cryptography.CryptographicException:系统找不到指定的文件 - System.Security.Cryptography.CryptographicException: The system cannot find the file specified 给出错误密码时出现System.Security.Cryptography.CryptographicException - System.Security.Cryptography.CryptographicException when wrong password given “System.Security.Cryptography.CryptographicException”类型的异常:密钥集不存在 - An exception of type 'System.Security.Cryptography.CryptographicException': keyset does not exist
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM