using System.Security.Cryptography;
public static void Main()
var gen = new ASPNETMachineKeyGenerator();
var validationKey = gen.GenValidationKey();
var decryptionKey = gen.GenDecryptionKey();
StringBuilder aspnet20machinekey = new StringBuilder();
aspnet20machinekey.Append("<machineKey \n");
aspnet20machinekey.Append("validationKey=\"" + validationKey + "\"\n");
aspnet20machinekey.Append("decryptionKey=\"" + decryptionKey + "\"\n");
aspnet20machinekey.Append("validation=\"SHA1\" decryption=\"AES\"\n");
aspnet20machinekey.Append("/>\n");
Console.WriteLine(aspnet20machinekey.ToString());
public class ASPNETMachineKeyGenerator
public string GenValidationKey()
public string GenDecryptionKey()
string getRandomKey(int bytelength)
byte[] buff = new byte[bytelength];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
StringBuilder sb = new StringBuilder(bytelength * 2);
for (int i = 0; i < buff.Length; i++)
sb.Append(string.Format("{0:X2}", buff[i]));