AES 256 Encryption: public and private key how can I generate and use it .net

26,039

Solution 1

In .Net, you can create your key pair like this:

public static Tuple<string, string> CreateKeyPair()
{
    CspParameters cspParams = new CspParameters { ProviderType = 1 };

    RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(1024, cspParams);

    string publicKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(false));
    string privateKey = Convert.ToBase64String(rsaProvider.ExportCspBlob(true));

    return new Tuple<string, string>(privateKey, publicKey);
}

You can then use your public key to encrypt a message like so:

public static byte[] Encrypt(string publicKey, string data)
{
    CspParameters cspParams = new CspParameters { ProviderType = 1 };
    RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParams);

    rsaProvider.ImportCspBlob(Convert.FromBase64String(publicKey));

    byte[] plainBytes = Encoding.UTF8.GetBytes(data);
    byte[] encryptedBytes = rsaProvider.Encrypt(plainBytes, false);

    return encryptedBytes;
}

And use your private key to decrypt like this:

public static string Decrypt(string privateKey, byte[] encryptedBytes)
{
    CspParameters cspParams = new CspParameters { ProviderType = 1 };
    RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider(cspParams);

    rsaProvider.ImportCspBlob(Convert.FromBase64String(privateKey));

    byte[] plainBytes = rsaProvider.Decrypt(encryptedBytes, false);

    string plainText = Encoding.UTF8.GetString(plainBytes, 0, plainBytes.Length);

    return plainText;
}

Solution 2

I think you are mixing things up. AES is a symmetric cipher, thus only have one key both for encryption and decryption. Asymmetric ciphers like RSA have two keys. A public key for encryption and a private key for decryption.

And for reddit, you can indeed answer without being logged in.

Share:
26,039

Related videos on Youtube

Yasser-Farag
Author by

Yasser-Farag

Updated on April 21, 2020

Comments

  • Yasser-Farag
    Yasser-Farag about 4 years

    Regarding AES 256 Encryption:

    • What is the public and private key?
    • How can I generate these two keys?
    • How can I use the public to encrypt the data?
    • How can I use the private to decrypt the data?
    • metsburg
      metsburg over 10 years
      www (dot) what-have-you-tried (dot) com. Possible duplicate of: stackoverflow.com/questions/273396/…
    • Mark Rotteveel
      Mark Rotteveel over 10 years
      There is no public and private key in AES encryption, your question doesn't make sense.
  • Yasser-Farag
    Yasser-Farag over 10 years
    thank you I will try it now.
  • Yasser-Farag
    Yasser-Farag over 10 years
    Is this encrypt and decrypt functions using AES 256 Encryption?
  • dcastro
    dcastro over 10 years
    Like user2787670 explained, AES 256 is a symmetric cipher. This generates a RSA key pair.
  • Yasser-Farag
    Yasser-Farag over 10 years
    you mean that AES 256 use the same key for encrypt and decrypt functions?
  • dcastro
    dcastro over 10 years
    Exactly. Symmetric keys are good for encrypting large amounts of data, whereas asymmetric keys are better for small chunks. If two parties have their own key set, a typical scenario is to use asymmetric keys to securely exchange symmetric keys between two parties, and then use symmetric keys from then on to securely exchange large amounts of data. You should look into that. Of course, this HIGHLY depends on what you're trying to achieve.
  • Yasser-Farag
    Yasser-Farag over 10 years
    thank you in advance for your clarifications
  • Yasser-Farag
    Yasser-Farag over 10 years
    Hi, I tried your code but I have exception in the decrypt function "Key does not exist" I don't konw how can I fix it.
  • Kosmo零
    Kosmo零 over 8 years
    @Yasser-Farag - I don't know how need to use his code to receive such error. Everything is working fine. Tuple<string, string> keys = CreateKeyPair(); string test = "we licensed?"; byte[] encrypted = Encrypt(keys.Item2, test); string decrypted = Decrypt(keys.Item1, encrypted);
  • bottlenecked
    bottlenecked over 7 years
    Great answer! Who'd have thought it would be so easy?

Related