locked
Encrypt/Decrypt Data with RSACryptoServiceProvider RRS feed

  • Question

  • Hi,

    i am trying o get data en-/decrypted with the RSA Cryptoprovider.

    I created a key pair:

                 // Create a new key pair on target CSP
                    cspParams = new CspParameters();
                    cspParams.ProviderType = 1; // PROV_RSA_FULL 
                    //cspParams.ProviderName; // CSP name
                    cspParams.Flags = CspProviderFlags.UseArchivableKey;
                    cspParams.KeyNumber = (int)KeyNumber.Exchange;
                    rsaProvider = new RSACryptoServiceProvider(cspParams);
     
                    // Export public key
                    publicKey = rsaProvider.ToXmlString(false);
     
                   // Export private/public key pair 
                    privateKey = rsaProvider.ToXmlString(true);

    After that i used the following code to encrypt data with private key:

                TextFile.ReadFromFile(ref strRSA, Filename);
     
                RSACryptoServiceProvider rsaProvider = null;
                rsaProvider = new RSACryptoServiceProvider();
                rsaProvider.FromXmlString(strRSA);
     
                byte[] bData = new byte[DataToEncrypt.Length];
                StringToByteArray(DataToEncrypt, ref bData);
                byte[] bCryptData = rsaProvider.Encrypt(bData, false);

    And the tried to decrypt it with the public key:

               RSACryptoServiceProvider rsaProvider = null;
                rsaProvider = new RSACryptoServiceProvider();
                rsaProvider.FromXmlString(RSAParameter);
     
                byte[] bData = new byte[DataToDecrypt.Length / 2];
                ConvertAsciiHexString2ByteArray(ref bData, DataToDecrypt);
                byte[] bDecryptData = rsaProvider.Decrypt(bData, false);

    But the Decrypt method raised an exception: "Key is not available"

    Info: When i encrypt with public key and decrypt with private key it is ok. The other way ends in the descripted error!

    Can somebody tell me what i did wrong?

    Thanks in advance!



    • Edited by SteveSt27 Thursday, March 8, 2012 1:57 PM
    Thursday, March 8, 2012 1:21 PM

All replies

  • One problem that I see in your code is that you are calling "decrypt" for both encryption and decryption. If you post your complete code, I might be more of a help.

    ----
    Nima Sharifimehr.
    sbucsc at yahoo dot com

    Thursday, March 8, 2012 1:54 PM
  • Sorry! I made a copy/paste mistake to the forum... :(

    I replaced it already!

    Is there something more to configure, to be able to do the decrypt with the public key?
    • Edited by SteveSt27 Thursday, March 8, 2012 2:33 PM
    Thursday, March 8, 2012 1:58 PM
  • Sorry! I made a copy/paste mistake to the forum... :(

    I replaced it already!

    Is there something more to configure, to be able to do the decrypt with the public key?

    to be able to do the decrypt with the public key?

    Why you decrypt with the public key? you should use verify Signature functions!


    Qiusheng Xie

    Friday, March 9, 2012 12:31 AM