C# System.Security.Cryptography.CryptographyException:RSACryptoserviceProvider中的长度错误

C# System.Security.Cryptography.CryptographyException:RSACryptoserviceProvider中的长度错误,c#,encryption,windows-phone-8,rsa,C#,Encryption,Windows Phone 8,Rsa,我希望在wp8项目的c#中使用rsacyptoserviceprovider对数据进行加密和解密。我正在创建非对称密钥,如下所示: CspParameters parameters = new CspParameters(); parameters.KeyContainerName = "MyContainer"; RSACryptoServiceProvider provider = new RSACryptoServiceProvider(parameters); 现在我想加密数据。

我希望在wp8项目的c#中使用
rsacyptoserviceprovider
对数据进行加密和解密。我正在创建非对称密钥,如下所示:

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

RSACryptoServiceProvider provider = new RSACryptoServiceProvider(parameters);  
现在我想加密数据。我正在做:

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;
    }
}
现在我在加密时遇到异常:

RSA.EncryptSystem.Security.Cryptography.CryptographicException : Bad length in RSACryptoserviceProvider. 
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)
消息的长度不好


我不知道哪里会出错?

这表明您试图加密的数据量太长。您应该在较小的块中对其进行加密。

RSA仅用于加密少量数据。您可以加密的确切数量取决于密钥长度+填充使用的数量。1024位的密钥允许100字节以上的位


由于RSA速度非常慢,因此加密大型消息的常用方法是使用混合加密。在混合加密中,使用快速对称加密算法(如AES)使用随机密钥加密数据。然后使用RSA对随机密钥进行加密,并将其与对称密钥加密数据一起发送。

在登录请求中,我必须将公钥发送到服务器。作为响应,我得到的令牌为“eJzFVVlzo0YQf…”约1300-1400个字符,我希望在下一个请求中对此令牌签名并发送回服务器。由于服务器具有公钥,它将验证令牌并在resose中向我发送新令牌。我如何在windows phone 8项目中的c#中实现这一点?签名不需要加密完整消息,只需要消息的单向散列。然后使用私钥对该散列进行加密,并可以使用公钥进行验证。看一看给定的链接,有些方法没有被清除。而且他先加密,然后签名。我现在完全糊涂了,因为没有关于如何在设备端(c#)使用私钥对数据进行签名以及如何在服务器端(java)使用相应的公钥对签名数据进行验证的好文档。我急切地等待正确的解决方案。我们正从最初的问题转移到为什么会得到“坏长度”执行选项。答案是,RSA方法不是为加密大消息而设计的。如果您真的需要使用RSA进行签名,那么这是一个新问题。