Viewing source for Recipe1509cs.aspx

<%@ Page %>
<script Language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
	string clearText = "Try not.  Do.  Or do not.  There is no try.";
	string passPhrase = "Password";
	System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
	
	Response.Write("Clear Text: " + clearText);
	Response.Write("<hr>");

	byte[] cypherText = EncryptData(clearText, passPhrase);

	Response.Write("Encrypted Text: " + encoder.GetString(cypherText));
	Response.Write("<hr>");		
	Response.Write("Decrypted Text: " + DecryptData(cypherText, passPhrase));
	Response.Write("<hr>");		
}

private byte[] EncryptData(string ClearText, string password)
{
	int keySize = 24; // 192 bit
	byte[] hash;
	byte[] passwordArray = new Byte[password.Length];
	byte[] tdeskey = new byte[keySize];
	byte[] IV = {1,2,3,4,5,6,7,8};
	System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
	encoder.GetBytes(password, 0, password.Length, passwordArray, 0);

	// hash the password into the key byte array
	System.Security.Cryptography.SHA1CryptoServiceProvider SHA = new System.Security.Cryptography.SHA1CryptoServiceProvider();
	hash = SHA.ComputeHash(passwordArray);

	for(int i = 0; i < keySize; i++)
		tdeskey[i] = hash[i % hash.Length];

	// Encrypt the string data
	// Create a service provider
	System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
	// Create an ICryptTransform using the key and initialization vector
	System.Security.Cryptography.ICryptoTransform cryptoTransform = tdes.CreateEncryptor(tdeskey, IV);
	// Create an output stream to hold the encrypted data
	System.IO.MemoryStream EncryptedStream = new System.IO.MemoryStream();
	// Get the input string into a byte array
	byte[] input = encoder.GetBytes(ClearText);			
	// Create a CryptoStream to perform the encryption
	System.Security.Cryptography.CryptoStream crStream = new System.Security.Cryptography.CryptoStream(EncryptedStream, cryptoTransform, 
		System.Security.Cryptography.CryptoStreamMode.Write);
	// Write the input data to the output stream via the cryptoStream
	crStream.Write(input, 0, input.Length);
	crStream.FlushFinalBlock();
	// Reset the output stream to its beginning
	EncryptedStream.Position = 0;
	// Read the encrypted stream back into a string
	byte[] output = new byte[EncryptedStream.Length];
	EncryptedStream.Read(output, 0, (int)EncryptedStream.Length);
	crStream.Close();
	EncryptedStream.Close();
	// Return the result as a string
	return output;
}

private string DecryptData(byte[] CypherText, string password)
{
	int keySize = 24; // 192 bit
	byte[] hash;
	byte[] passwordArray = new Byte[password.Length];
	byte[] tdeskey = new byte[keySize];
	byte[] IV = {1,2,3,4,5,6,7,8};
	System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
	encoder.GetBytes(password, 0, password.Length, passwordArray, 0);

	// hash the password into the key byte array
	System.Security.Cryptography.SHA1CryptoServiceProvider SHA = new System.Security.Cryptography.SHA1CryptoServiceProvider();
	hash = SHA.ComputeHash(passwordArray);

	for(int i = 0; i < keySize; i++)
		tdeskey[i] = hash[i % hash.Length];

	// Decrypt the string data
	// Create a service provider
	System.Security.Cryptography.TripleDESCryptoServiceProvider tdes = new System.Security.Cryptography.TripleDESCryptoServiceProvider();
	// Create an ICryptTransform using the key and initialization vector
	System.Security.Cryptography.ICryptoTransform cryptoTransform = tdes.CreateDecryptor(tdeskey, IV);
	// Create an output stream to hold the encrypted data
	System.IO.MemoryStream DecryptedStream = new System.IO.MemoryStream();
	// Create a CryptoStream to perform the encryption
	System.Security.Cryptography.CryptoStream crStream = new System.Security.Cryptography.CryptoStream(DecryptedStream, cryptoTransform, 
		System.Security.Cryptography.CryptoStreamMode.Write);
	// Write the input data to the output stream via the cryptoStream
	crStream.Write(CypherText, 0, CypherText.Length);
	crStream.FlushFinalBlock();
	// Reset the output stream to its beginning
	DecryptedStream.Position = 0;
	// Read the encrypted stream back into a string
	byte[] output = new byte[DecryptedStream.Length];
	DecryptedStream.Read(output, 0, (int)DecryptedStream.Length);
	crStream.Close();
	DecryptedStream.Close();
	// Return the result as a string
	return encoder.GetString(output);
}
</script>