Results 1 to 1 of 1

Thread: C# - Symmetric Encryption Class

Threaded View

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2003
    Posts
    34

    C# - Symmetric Encryption Class

    Here is a class that encapsulates symmetric encryption using the TripleDES algorithm:

    The module is written in C#, but as you know, you can interop with it from any .NET aware language.

    Code:
    using System;
    using System.Text;
    using System.IO;
    using System.Security.Cryptography;
    using Encryption;
    
    namespace Encryption {
    
        public class TripleDESEncryption {
    
    	public static string EncryptData(string data, out byte[] desKey, out byte[] desIV) {		
       	    MemoryStream output = new MemoryStream();		
    	    byte[] byteData = new UnicodeEncoding().GetBytes(data);
    
      	    //Use the TripleDES symmetric encryption algorithm to encrypt our data. Without an IV, the 			
       	    //same input block of plaintext will encrypt to same output block of ciphertext. IV guarantees 
    	    //output of two identical plaintext blocks are different. 
    	    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    	    CryptoStream crypt = new CryptoStream(output, des.CreateEncryptor(), CryptoStreamMode.Write);
    		
    	    //Assign our crypto-generated key and iv values to our output arguments
    	    desKey = des.Key; desIV = des.IV;
    	    crypt.Write(byteData, 0, byteData.Length);			
    
        	    crypt.Close(); output.Close(); 		
    	    return new UnicodeEncoding().GetString(output.ToArray());
    	}
    
    	public static string EncryptData(byte[] desKey, byte[] desIV, string data) {		
    	    MemoryStream output = new MemoryStream();		
    	    byte[] byteData = new UnicodeEncoding().GetBytes(data);
    
       	    //Use the TripleDES symmetric encryption algorithm to encrypt our data. Without an IV, the 			
      	    //same input block of plaintext will encrypt to same output block of ciphertext. IV guarantees 
    	    //output of two identical plaintext blocks are different. 
    	    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    	    CryptoStream crypt = new CryptoStream(output, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
    	    crypt.Write(byteData, 0, byteData.Length);			
    
                crypt.Close(); output.Close(); 		
    	    return new UnicodeEncoding().GetString(output.ToArray());
    	}
    
    	public static string DecryptData(string data, byte[] desKey, byte[] desIV) {
    	    MemoryStream output = new MemoryStream();
    	    byte[] byteData = new UnicodeEncoding().GetBytes(data);
    
      	    //Use the TripleDES symmetric encryption algorithm to decrypt our data. In order for the ciphertext to be
    	    //successfully decrypted, the exact same key and iv must be used when initially encryted.
    	    TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
    	    CryptoStream crypt = new CryptoStream(output, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
    	    crypt.Write(byteData, 0, byteData.Length);
    
     	    crypt.Close(); output.Close(); 		
    	    return new UnicodeEncoding().GetString(output.ToArray());
    	}
        }
    }
    
    class CipherText {
    
        static void Main() {
    	byte[] key; byte[] iv; 
            string password = "secret";		
    	string cif = TripleDESEncryption.EncryptData(password, out key, out iv);
    	Console.WriteLine(TripleDESEncryption.DecryptData(cif, key, iv));
    	
            //NOTE: Key and IVector must be 16 bytes each
    	//byte[] key = UnicodeEncoding.Unicode.GetBytes("cornhle");	
    	//string cif = TripleDESEncryption.EncryptData(key, key, password);
    	//Console.WriteLine(TripleDESEncryption.DecryptData(cif, key, key));
        }
    }
    Last edited by SimonVega; Mar 14th, 2003 at 04:42 AM.
    AKA 'Lethal'

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width