Click here to Skip to main content
15,880,972 members
Articles / Programming Languages / C#
Article

Simple Cryptographic Engine

Rate me:
Please Sign up or sign in to vote.
1.52/5 (10 votes)
8 Aug 2007CPOL1 min read 30.3K   1.2K   18   3
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)


Written By
Architect
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralPadding is invalid and cannot be removed." Pin
srimal_s29-Jan-08 1:03
srimal_s29-Jan-08 1:03 
i used your code and when i try to decrypt it says "Padding is invalid and cannot be removed". what is this?

srimal_hs

GeneralNot Secure Pin
Ri Qen-Sin8-Aug-07 12:28
Ri Qen-Sin8-Aug-07 12:28 
GeneralRe: Not Secure Pin
Vivek Krishnamurthy8-Aug-07 23:03
Vivek Krishnamurthy8-Aug-07 23: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.