Click here to Skip to main content
13,295,218 members (72,270 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 8 Aug 2007

Simple Cryptographic Engine

, 8 Aug 2007
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


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


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:







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;


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.



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


About the Author

Vivek Krishnamurthy
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralPadding is invalid and cannot be removed." Pin
srimal_s29-Jan-08 2:03
membersrimal_s29-Jan-08 2:03 
GeneralNot Secure Pin
Ri Qen-Sin8-Aug-07 13:28
memberRi Qen-Sin8-Aug-07 13:28 
GeneralRe: Not Secure Pin
Vivek Krishnamurthy9-Aug-07 0:03
memberVivek Krishnamurthy9-Aug-07 0:03 

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

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

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