locked
Public Key / Private Key RRS feed

  • Question

  • Hello,

    I want to define a Public Key and Private Key and Encrypt / Decrypt data using RSACryptoServiceProvider. I tried the following:

        public static String Encrypt(String data, String key, Encoding encoding) {
    
          using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider()) {
            provider.FromXmlString(key);
            Byte[] encrypted = provider.Encrypt(encoding.GetBytes(data), true);
            provider.Clear();
            return Convert.ToBase64String(encrypted);
          }
        } // Encrypt
    
        public static String Decrypt(String data, String key, Encoding encoding) {
    
          using (RSACryptoServiceProvider provider = new RSACryptoServiceProvider()) {
            provider.FromXmlString(key);
            Byte[] decrypted = provider.Decrypt(Convert.FromBase64String(data), true);
            provider.Clear();
            return encoding.GetString(decrypted);
          }
    
        } // Decrypt

    This is working if I first define the Key as follows:

    String key = new RSACryptoServiceProvider().ToXmlString(true);

    But how can I explicity define a public key and a private key and then use them.

    And can the public and private keys be guids? Or should they be guids?

    Thank You,

    Miguel

    Tuesday, September 11, 2012 5:27 PM

Answers

  • this might get you started: RSACryptoServiceProvider Class

    see the example at the bottom


    the RSA (an asymmetrical key algorithm (that uses a public/private keys) is only meant to be used on small amount of data. Why? Too slow due the math/algorithm for large data.
    For large data, it is suggested to use symmetrical algorithms (3DES, Rijndael, AES, etc) . These generate their own keys (no public/private keys).

    • Proposed as answer by Norkk Tuesday, September 11, 2012 7:04 PM
    • Edited by Scruffy John Tuesday, September 11, 2012 7:09 PM
    • Marked as answer by Jason Dot Wang Wednesday, September 19, 2012 9:26 AM
    Tuesday, September 11, 2012 7:01 PM