System.Sicherheit.Die Kryptographie.CryptographicException : Bad Länge in RSACryptoserviceProvider

Möchte ich verschlüsseln und entschlüsseln von Daten mit RSACryptoServiceProvider in c# in wp8 Projekt. Ich bin der Schaffung von asymmetrischen Schlüsseln als :

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

RSACryptoServiceProvider provider = new RSACryptoServiceProvider(parameters);  

Nun will ich tun, verschlüsseln von Daten. Ich bin dabei:

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;
    }
}

Jetzt bin ich immer die Ausnahme, während encypting:

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

Stacktrace ist:

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)

und die Nachricht ist Schlimm Länge.

Bin ich nicht immer, wo kann ich schief gehen?

InformationsquelleAutor Vaibhav | 2014-02-11
Schreibe einen Kommentar