.NET And Triple DES Security
page 6 of 8
by Kay Lee
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 40089/ 48

The Transform Method Exposed

[ Download Code ] | [ Download Help ]

Transform the Input and Get an Output

The transform method was designed to handle both encryption and decryption since symmetric algorithms are very similar in terms of how to encrypt and how to decrypt.  That's one of the advantages of implementing symmetric encryption in your application.  The example's Transform method was adopted from http://www.developer.com/net/vb/article.php/3077901 when the original implementation was written a couple of months ago.  Since then, it was expanded to fit the mold a little better using C# and more of the System.Security.Crypography.TripleDESCryptoServiceProvider

private byte[] Transform(byte[] inputvar, ICryptoTransform transform) 
   System.IO.MemoryStream stream = 
      new System.IO.MemoryStream(2048);
  CryptoStream encryptstream = 
        new CryptoStream(stream, transform, CryptoStreamMode.Write);
   encryptstream.Write(inputvar, 0, (int)inputvar.Length);
   stream.Position = 0;     
   byte[] returnvar;
   returnvar = new byte[(int)stream.Length];
   stream.Read(returnvar, 0, (int)returnvar.Length);
   encryptstream = null;
   stream = null;
   return returnvar;

The Transform method has two parameters that require a brief examination.  As mentioned before, DES-based encryptions are byte encryptions and will alter bytes through the DES algorithm.  The first parameter is a byte array holding the value that's to be transformed.  The second parameter is the ICryptoTransform implementing class that we pass in via Encrypt or Decrypt.  That's where most of the logic is and is a very important part of the processes.  This method was set to a private declaration since it should only be used by the Encrypt or Decrypt methods.  By making it public, it would defeat the purpose of having all the operations in a specific class or assembly, and it's also bad practice.   

CryptoStream encryptstream = 
 new CryptoStream(stream, transform, CryptoStreamMode.Write);

The CryptoStream object is found in the System.Security.Cryptography namespace and is used in most encryption implementations.  It requires three parameters: System.IO.Stream, ICryptoTransform, and CryptoStreamMode.  The Stream object we use is the MemoryStream since the value of the parameter will be filled as the input byte array is fed in.  The ICryptoTransform containing the algorithm instructions is passed in directly by the calling methods (Encrypt and Decrypt), and the CryptoStreamMode we use is Write.  We use System.Security.Cryptography.CryptoStreamMode.Write because we want to write the outcome into the MemoryStream.

// Write the input array values into the crypto stream, and transform.
encryptstream.Write(inputvar, 0, (int)inputvar.Length);

It's safe to assume many are shaking their heads thinking "lazy."  We're using CryptoStream.Write to write the transformation into the stream.  The first parameter is the byte array storing what's to be read into the CryptoStream.  The next two are integer based indicating the start and how many to read and transform into the CryptoStream.  We use the FlushFinalBlock method to clear the remaining space in the MemoryStream.

In English, we're sending in a byte array, telling it to get the first to last items, and destroy whatever is left of the stream after it's been filled with the transformed bytes.

stream.Position = 0;     
byte[] returnvar;
returnvar = new byte[(int)stream.Length];
stream.Read(returnvar, 0, (int)returnvar.Length);

Next on the plate is to retrieve the output from the transformation.  To achieve this, we first set the stream to rewind back to the beginning by setting the property Position to 0.  We declare the output byte array (this can be declared at the beginning of the method if you prefer), and we set the output byte array to a new byte array with the size of the stream.  Using the Read method, we read the stream into the byte array in a similar fashion as we wrote previously.

All that's left is to close the streams and return the output byte array.

This example is a fairly easy implementation and can be altered to meet your application's needs.  The best bet is to avoid making the methods more complicated than they need to be.  The .NET Framework offers a lot of high-level objects that will do most of the work for you.  It's obvious that 90% of the time, a custom implementation can be faster by using lower-level objects.  However, in the case of Cryptography, it's best to let the scientist do what they're good at, and let the programmers do what they're best at.

View Entire Article

User Comments

Title: Hai   
Name: mangala
Date: 2008-07-22 7:54:10 AM
Title: Public Key   
Name: Joe Grant
Date: 2006-12-08 12:12:50 PM
How would one go about create a public key to share with someone so they can decrypt without sharing the private key?
Title: Excellent coverage of TripleDES   
Name: Sam
Date: 2005-08-02 9:11:49 PM
Great stuff... simple but with enough background information to understand the important elements eg. PK, IV, Cipher etc. I will be using parts of it in my enterprise application.
Title: triple des   
Name: Josh
Date: 2004-10-13 10:12:45 PM
we've been looking at different encryption methods in class and lectures have been so boring. i decided to search around and found your article to be a great practical implementation. i wish my class would provide us with pratical examples like this. Thanks.
Title: Thanks   
Name: Kay Lee
Date: 2004-10-13 6:42:03 PM
Thanks. I'm glad you like it, and I hope the source works well for you.
Title: Triple DES Encryption   
Name: Brian Chiasson
Date: 2004-10-12 3:35:52 PM
Excellent article. I have been looking for a couple of hours for something on encryption. It shed light on the subject and provided me with useful code for my intranet application. Thanks a million...

Community Advice: ASP | SQL | XML | Regular Expressions | Windows

©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-06-15 3:47:45 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search