Last active
October 15, 2019 21:19
-
-
Save aaronhoffman/09b56f77a95205fe10365bde2f4a02e5 to your computer and use it in GitHub Desktop.
Generate Random AES Encryption Key
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
more info: https://aaron-hoffman.blogspot.com/2017/08/generate-random-aes-encryption-key.html