First off this code is taken from "SAMS ASP.Net Unleashed 2nd Ed". I found this code to be fairly interesting and thought it would good to share it with you all. I am currently working on creating my own Triple DES encrypt/decrypt web app. But the Hasher is what I started with when I was learning about .NET encryption Classes.
[%@ import Namespace="System.Security.Cryptography" %]
[script runat="Server"]
Function Convert2ByteArray( strInput As String) As Byte()
Dim IntCounter As Integer
Dim arrChar as Char()
arrChar = strInput.ToCharArray()
dim arrByte( arrChar.Length - 1) As Byte
For IntCounter = 0 To arrByte.Length - 1
arrByte(intCounter) = Convert.ToByte( arrChar( intCounter))
Next
Return arrByte
End Function
Sub Button_Click( s As Object, e As Eventargs)
dim arrHashInput as Byte()
dim arrHashoutput as Byte()
Dim objMD5 As MD5CryptoServiceProvider
objMD5 = New MD5CryptoServiceProvider
arrHashInput = Convert2ByteArray( txtInput.Text)
arrHashOutput = objMD5.CompputeHash( arrHashInput)
lblOutput.Text = BitConverter.ToString( arrHashOutput)
end sub
[/script]
[html]
[head][title] MD5 HASHER[/title][/head]
[body]
[Form runat="Server"]
[h2]Generate MD5 Hash[h2]
[b]Enter Text to Hash [/b]
[br]
[asp:TextBox id="txtInput" TextMode="Multiline" Columns="50" Rows="10" Runat="server" /]
[p]
[asp:Button Text="Compute Hash" OnClick="Button_Click" Runat="Server" /]
[/p]
[b]Hash Value: [b]
[br]
[asp:Label ID="lblOutput" Runat="server" /]
[/form]
[/body]
[/html]
NOTE: DUE TO THE FORUM NOT ALLOWING CERTAIN HTML TAGS I HAVE MODIFIED THEM SO YOU CAN SEE THE HTML CODE AS WELL
due to the fact that this isn't a tutorial I am not going to go into depth to explain what the code does(if you do wish for an explanation on what it does feel free to ask).
in Simple terms what this code does is it transforms the Plain Text inserted into an MD5 Hash, I was orginally going to try and create a decrypter for it but have since moved on to DES and Triple DES.
As a further note the Hasher isn't just limited to MD5 with some minor tweaking it can include SHA1,SHA256,SHA384 and SHA512.
Also for convenience if people wish to muck around with this code I am including C# code for it as well
C#
* public byte[] Convert2ByteArray(string strInput)
* {
* int IntCounter = 0;
* char[] arrChar = null;
*
* arrChar = strInput.ToCharArray();
* byte[] arrByte = new byte[arrChar.Length];
* for (IntCounter = 0; IntCounter <= arrByte.Length - 1; IntCounter++) {
* arrByte(intCounter) = Convert.ToByte(arrChar(intCounter));
* }
* return arrByte;
* }
*
* public void Button_Click(object s, Eventargs e)
* {
* byte[] arrHashInput = null;
* byte[] arrHashoutput = null;
* MD5CryptoServiceProvider objMD5 = default(MD5CryptoServiceProvider);
*
* objMD5 = new MD5CryptoServiceProvider();
* arrHashInput = Convert2ByteArray(txtInput.Text);
* arrHashOutput = objMD5.CompputeHash(arrHashInput);
* lblOutput.Text = BitConverter.ToString(arrHashOutput);
* }