Using the Crypto Service

This demo illustrates how to use the cryptography classes in the .NET Framework (in conjunction with a custom DLL). To try out this demo, enter a key into the Key Value text box. Next, enter some source data (the data you'd like to encrypt) and click the Encrypt button. In the Processed Data text box you'll see the encrypted information. Now, to decrypt the information, enter the same key into the Key Value text box that you entered in that text box to begin with (the value should be persisted there automatically for you). Then, cut and paste the encrypted data into the Source Data text box and click the Decrypt button. The clear-text should appear in the Processed Data text box!
Download the complete code.


Key Value:

Source Data:

Processed Data:


Source Code
<%@ Import Namespace="FE_SymmetricNamespace" %>
<%@ Import Namespace="System.Security.Cryptography" %>
<%@ Page Language="C#" Trace="false"%> 

<html>

<script language="C#" runat="server">
String gKey, gstrSource;
String gstrProcess;	

public void Submit_Click(Object oSender, EventArgs E)
{
	try 
	{
		gKey = txtKey.Text;
		gstrSource = txtSource.Text;
		gstrProcess = txtProcess.Text;
	}
	catch (Exception) 
	{ 
		/* ignored */ 
	}
	
	FE_Symmetric feService = new FE_Symmetric();

	String strTmp;
	int i;
	switch (((Control)oSender).ID)
	{
		case "Encrypt": 
			strTmp = feService.EncryptData(gKey, gstrSource);
			Result.Text = "<b>Encrypted Data Length</b> = " + strTmp.Length.ToString();
			txtProcess.Text = strTmp;
		break;
		
		case "Decrypt": 
			strTmp = feService.DecryptData(gKey, gstrSource);
			Result.Text = "<b>Decrypted Data Length</b> = " + strTmp.Length.ToString();
			txtProcess.Text = strTmp;
			
			byte[] bpDe = new byte[strTmp.Length];
			ASCIIEncoding a = new ASCIIEncoding();
			a.GetBytes(strTmp, 0, strTmp.Length, bpDe, 0);
			for (i=0; i<strTmp.Length; i++) 
				Trace.Write(i.ToString() + "=" + strTmp[i] + "=" + Convert.ToString(bpDe[i]));
			
		break;

	}
}

</script>

<body style="font: 10pt verdana">

  <h4>Using the Crypto Service </h4>

  <form runat="server">  

  <div style="padding:15,15,15,15;background-color:beige;width:300;border-color:black;border-width:1;border-style:solid">

    Key Value: <br><asp:TextBox id="txtKey" Text="secret key" runat="server"/><br>
    Source Data: <br><asp:TextBox id="txtSource" Text="raw Data test and test" runat="server"/><br>
    Processed Data: <br><asp:TextBox id="txtProcess" runat="server"/><p>

    <input type="submit" id="Encrypt" value="Encrypt" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="Decrypt" value="Decrypt" OnServerClick="Submit_Click" runat="server">

    <p>

    <asp:Label id="Result" runat="server"/>

  </div>

  </form>

</body>
</html>


[Return to the Article] | [Download the complete code]