Click here to Skip to main content
Click here to Skip to main content

Simple Cryptographic Engine

, 8 Aug 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Simple Cryptographic Engine showing how to implement Rijndael, RC2 and DES Cryptographic Algorithims using Cryptographic Serives built in .Net Framework
Screenshot - encryption.jpg

Introduction

This is a simple Encryption / Decrypton program using Cryptographic Services built in .NET Framework for Symmetric Encryption.

Background

I was working on Encryptiong and Decrypting data for my project and stuck at a Cryptographic Exception "Bad Data" . This is caused due to the mismatch in the Keys used for encryption and decryption. so, come up this small piece of code that help resolve it.

Using the code

You can simply copy the CryptoEngine.cs and start using in your code. I have created static methods for Encryption and Decryption. It can be called as follows"

//For Encryption of the Text
txtEncrypt.Text = CryptoEngine.RijndaelManagedEncryption(txtClearText.Text);

// For Decryption
txtDecrypt.Text = CryptoEngine.RijndaelManagedDecryption(txtEncrypt.Text);

Similarly you can use the other encryption and decryption providers:

RijndaelManagedEncryption

RC2ProviderEncryption

DESProviderEncryption

RijndaelManagedDecryption

RC2ProviderDecryption

DESProviderDecryption

Points of Interest

Derive Key from Password and Initialize the Encryptor and Decryptor.

PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, null);
// generate an RC2 key 
byte[] iv = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] key = pdb.CryptDeriveKey("DES", "SHA1", desProvider.KeySize, iv);

desProvider.Key = key;
desProvider.IV = new byte[] { 21, 22, 23, 24, 25, 26, 27, 28 };
 
// Use the key to create the Encryptor or Decryptor objects.
ICryptoTransform encryptor = desProvider.CreateEncryptor(desProvider.Key, desProvider.IV); 

You can extend this code to support more types of encryption supported by .NET Framework.

Here is how you can extend the code for Triple DES Provider.

// Add static field to the CryptoEngine Class
static TripleDESCryptoServiceProvider tripledesProvider;

// Init tripledesProvider in the Constructor
tripledesProvider = new TripleDESCryptoServiceProvider();

// Create static methods for TripleDES Encryption and Decryption

public static string TripleDESProviderEncryption(string plainText)
{
    byte[] text = Encoding.ASCII.GetBytes(plainText);

    ICryptoTransform encryptor = tripledesProvider.CreateEncryptor();
    byte[] encrypted = GenericEncryptor(text, encryptor);
    return Convert.ToBase64String(encrypted);
}
    
public static string TripleDESProviderDecryption(string plainText)

{
    byte[] text = Convert.FromBase64String(plainText);
    
    ICryptoTransform decryptor = tripledesProvider.CreateDecryptor();
    string decStr = GenericDecryptor(text, decryptor);
    return decStr;
}

History

This is the Initial Version of the submission.

I have updated the CryptoEngine to allow password while encrypting and decrypting, this adds a more security I believe.

References

http://blogs.msdn.com
http://msdn2.microsoft.com/en-us/library/default.aspx

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Vivek Krishnamurthy
Architect
India India
No Biography provided

Comments and Discussions

 
GeneralPadding is invalid and cannot be removed." Pinmembersrimal_s29-Jan-08 2:03 
GeneralNot Secure PinmemberRi Qen-Sin8-Aug-07 13:28 
GeneralRe: Not Secure PinmemberVivek Krishnamurthy9-Aug-07 0:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411022.1 | Last Updated 8 Aug 2007
Article Copyright 2007 by Vivek Krishnamurthy
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid