28.12.2013 Views

Building Secure ASP.NET Applications - People Search Directory

Building Secure ASP.NET Applications - People Search Directory

Building Secure ASP.NET Applications - People Search Directory

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

How To: Create an Encryption Library 437<br />

2. Create a Console Test Application<br />

This procedure creates a simple console test application to test the encryption and<br />

decryption functionality.<br />

<br />

To create a console test application<br />

1. Add a new C# Console application called EncryptionTester to the current<br />

solution.<br />

2. In Solution Explorer, right-click the EncryptionTester project, and then click Set<br />

as StartUp Project.<br />

3. Use Solution Explorer to rename class1.cs as EncryptionTest.cs.<br />

4. In EncryptionTest.cs, rename Class1 as EncryptionTest.<br />

5. Add a project reference to the Encryption project.<br />

6. Add the following using statements at the top of EncryptionTest.cs.<br />

using System.Text;<br />

using Encryption;<br />

7. Add the following code to the Main method.<br />

// Set the required algorithm<br />

EncryptionAlgorithm algorithm = EncryptionAlgorithm.Des;<br />

// Init variables.<br />

byte[] IV = null;<br />

byte[] cipherText = null;<br />

byte[] key = null;<br />

try<br />

{ //Try to encrypt.<br />

//Create the encryptor.<br />

Encryptor enc = new Encryptor(EncryptionAlgorithm.Des);<br />

byte[] plainText = Encoding.ASCII.GetBytes("Test String");<br />

if ((EncryptionAlgorithm.TripleDes == algorithm) ||<br />

(EncryptionAlgorithm.Rijndael == algorithm))<br />

{ //3Des only work with a 16 or 24 byte key.<br />

key = Encoding.ASCII.GetBytes("password12345678");<br />

if (EncryptionAlgorithm.Rijndael == algorithm)<br />

{ // Must be 16 bytes for Rijndael.<br />

IV = Encoding.ASCII.GetBytes("init vec is big.");<br />

}<br />

else<br />

{<br />

IV = Encoding.ASCII.GetBytes("init vec");<br />

}<br />

}<br />

else

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!