Skip to content

Instantly share code, notes, and snippets.

@aaronhoffman
Last active October 15, 2019 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronhoffman/09b56f77a95205fe10365bde2f4a02e5 to your computer and use it in GitHub Desktop.
Save aaronhoffman/09b56f77a95205fe10365bde2f4a02e5 to your computer and use it in GitHub Desktop.
Generate Random AES Encryption Key
public static class AesKeyGenerator
{
public static string GenerateKey(int bitStrength)
{
// note: valid bit strength for aes: 128, 192, or 256 bits (16, 24, or 32 bytes)
var random = new System.Security.Cryptography.RNGCryptoServiceProvider();
var keyArray = new byte[bitStrength / 8];
random.GetBytes(keyArray);
var base64key = Convert.ToBase64String(keyArray);
return base64key;
}
}
Using Visual Studio's "C# Interactive" screen you can generate a key in a single line:
var keyArray = new byte[32]; (new System.Security.Cryptography.RNGCryptoServiceProvider()).GetBytes(keyArray); Convert.ToBase64String(keyArray);
@aaronhoffman
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment