Skip to main content

Generate a secure cryptographic randomized key in C#.

//
// Base64 version
public static string GenerateSecureRandomKey()
{
    using (var rijndael = System.Security.Cryptography.RijndaelManaged.Create())
    {
        rijndael.GenerateKey();
        return Convert.ToBase64String(rijndael.Key);
    }
}

//
// Hex version
public static string GenerateSecureRandomKey()
{
    using (var rijndael = System.Security.Cryptography.RijndaelManaged.Create())
    {
        rijndael.GenerateKey();
        var key = BitConverter.ToString(rijndael.Key)
            .Replace("-", string.Empty).ToLowerInvariant();
        return key;
    }
}

//
// Also see: https://github.com/ParticularLabs/CryptoKeyGenerator