none
Encryption and Decryption using Private-Public key pairs in dot net 2.0

    Question

  •  

    Hi All,

            Want to encrypt a file using Private and Public Key paris in dot net 2.0. The requirement is,

     

    1. i have to encrypt a file and send it to my client and he has to decrypt it using Publick Key.

    2. The client can encrypt the file with the pubic key which i had supplied and i should decrypt the same with my private key.

    3. I want to generate the Key pairs only once for the first time and use the same in the subsequent encryption of files. How can i get and store the private and public keys?

     

    Can i use RSACryptoServiceProvider for this ? The msdn say this is be useful for only small amount of data. I tried encrypting a File and it throws below error...

     

    code snippet

    RSACryptoServiceProvider encry = new RSACryptoServiceProvider();

    FileStream infile;

    infile = new FileStream(@"c:\MyTest2.txt", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

    byte[] buffer = new byte[infile.Length];

    int count = infile.Read(buffer, 0, buffer.Length);

    encryptedData = encry.Decrypt(buffer, false);

     

    err msg:The data to be decrypted exceeds the maximum for this modulus of 128 bytes.

    stackTrace : System.Security.Cryptography.RSACryptoServiceProvider.Decrypt(Byte[] rgb, Boolean fOAEP)\

     

    When i tried with encry a small string it throws below error and stact trace

    RSACryptoServiceProvider encry = new RSACryptoServiceProvider();

    encryptedData = encry.Decrypt(ASCIIEncoding.ASCII.GetBytes("This is normal Text")), false);

     

    Err. Msg : Bad Data

    StackTrace : StackTrace = "   at System.Security.Cryptography.CryptographicException.ThrowCryptogaphicException(Int32 hr)\r\n   at System.Security.Cryptography.Utils._DecryptKey(SafeKeyHandle hPubKey, Byte[] key, Int32 dwFlags)\r\n   at System.Security.Cryptography.RSACryptoServiceP...

     

    Kindly advice how to approach the above 3 scenarious in dot net 2.0.

     

    Thanks in advance.

     

     

    Wednesday, February 27, 2008 9:18 PM

All replies

  • Hello,

    I am facing same kind of problem you are facing.

    Please help me if you got to know the solution of this problem...
    Friday, May 23, 2008 8:26 AM
  • Did you find any solution? I have similar problem.

     

    Thanks

     

    Monday, November 17, 2008 8:12 PM
  • Hi,

    For implementing Public Private key pair concept using RSA Encryption, I tried following steps:

    1. Generated RSA Public/Private Key pair:

                using (var rsa = new RSACryptoServiceProvider())
                {
    // This String consists only Public Key Information
    String publicKeyOnly = rsa.ToXmlString(false);
    // This String consists both Private/Public Key information
    String publicPrivate = rsa.ToXmlString(true);
    }

    2. At the Client side, Encrypt the text you want to secure:

    using (var rsaPublicOnly = new RSACryptoServiceProvider())
                {
    rsaPublicOnly.FromXmlString(publicKeyOnly);
    byte[] encryptedData = rsaPublicOnly.Encrypt(Encoding.UTF8.GetBytes("This String is to be Secured."), true);
     }


    3. Send this "encryptedData" to the Server for further use.

    4. Decrypt the string for further use:

                    using (var rsaPublicPrivate = new RSACryptoServiceProvider())
                    {
                        // Providing Private key information to RSA Object
                        rsaPublicPrivate.FromXmlString(publicPrivate);
                        // Decrypting the encrypted data by using RSA object "rsaPublicPrivate"
                        String decryptedPassword = rsaPublicPrivate.Decrypt(encryptedData, true);
                    }
    decryptedPassword contains the original password which Client want to send.

    You need to take care of few things:

    1. Use System.Security.Cryptography namespace.
    2. Only "publicKeyOnly" should be sent to client. As using "publicKeyOnly", one can only encrypt the text. One can not decrypt the encrypted text without using "publicPrivate".
    3. Server where the decryption need to take place must have "
    publicPrivate".
    4. You can also set the key size of the RSA but overloading the RSACryptoServiceProvider method:
    using (var rsaPublicOnly = new RSACryptoServiceProvider(KeySize))
    where KeySize is the size of the key you want to take. This is an optional field. You can skip that if your requirement is not specific.

    Hope it works for you... All the best...

    Tuesday, November 18, 2008 12:36 PM