Standing on the shoulders of giants. RSS 2.0
# Thursday, December 08, 2005

Code to create a random validationKey and decryption for use in the machinekey element of the web.config.

Note: valid values for the decryptionKey length are 8 (for DES) or 24 (for 3DES), the resulting key is twice as long and for validationkey 20 to 64.

using System;

using System.Text;

using System.Security.Cryptography;

namespace Crypto {

public class KeyCreator {

public static void Main(String[] args) {

String[] commandLineArgs = System.Environment.GetCommandLineArgs();

string decryptionKey = CreateKey(System.Convert.ToInt32(commandLineArgs[1]));

string validationKey = CreateKey(System.Convert.ToInt32(commandLineArgs[2]));

Console.WriteLine("<machineKey validationKey=\"{0}\" decryptionKey=\"{1}\" validation=\"SHA1\"/>", validationKey, decryptionKey);

}

static String CreateKey(int numBytes) {

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

byte[] buff = new byte[numBytes];

rng.GetBytes(buff);

return BytesToHexString(buff);

}

static String BytesToHexString(byte[] bytes) {

StringBuilder hexString = new StringBuilder(64);

for (int counter = 0; counter < bytes.Length; counter++) {

hexString.Append(String.Format("{0:X2}", bytes[counter]));

}

return hexString.ToString();

}

}

}

source: mskb312906

Thursday, December 08, 2005 3:33:01 PM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] - Trackback
Codesnippet
Comments are closed.
About
© Copyright 2009
Paul van Brenk
Sign In
newtelligence dasBlog 2.3.8275.16006
All Content © 2009, Paul van Brenk
DasBlog theme 'Business' created by Christoph De Baene (delarou)