Skip to content

Instantly share code, notes, and snippets.

@jhoerr
Last active August 29, 2015 14:02
Show Gist options
  • Save jhoerr/d69c03cdfa50ce76c15e to your computer and use it in GitHub Desktop.
Save jhoerr/d69c03cdfa50ce76c15e to your computer and use it in GitHub Desktop.
Web.config machine key generator
[TestFixture]
public class MachineKeyGenerator
{
[Test]
public void GenerateMachineKey()
{
Console.Out.WriteLine(@"<machineKey validationKey=""{0}"" decryptionKey=""{1}""/>", Key(512), Key(192));
}
private static string Key(int bits)
{
var cryptoProvider = new RNGCryptoServiceProvider();
var buffer = new byte[bits/8];
cryptoProvider.GetBytes(buffer);
return BytesToHexString(buffer);
}
static string BytesToHexString(byte[] bytes)
{
var hexString = new StringBuilder(64);
foreach (byte t in bytes)
{
hexString.Append(String.Format("{0:X2}", t));
}
return hexString.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment