[英]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.