Home > Cryptography > DES Encryption in C# using System.Security.Cryptography Part 1

DES Encryption in C# using System.Security.Cryptography Part 1

Doing Encryption with various well know algorithm is not so easy. Writing those and managing is bit difficult. But microsoft provides assemblies/libraries and built in classes with the help of which you can peform encryption with even writting single code of encryption algorithm. 

There are two major kind of encryptions

1. Secret Key (Also know as Symmetric encryption since single key is used to encrypt and decrypt the data)

Here single shared key is maintained to secure the data from unauthorised access. These keys are used to perform the encryption and decryption of the data

2. Public Key

Here a pair of public and private key used in order to secure the data.

Now we will see example of Secret Key Encryption/Decryption (DES)


// Encrypt the string.

private void button1_Click(object sender, EventArgs e)
{
DESCryptoServiceProvider key = new DESCryptoServiceProvider();
byte[] buffer = Encrypt(textBox1.Text, key);
textBox2.Text = bytestostring(buffer);
textBox3.Text = Decrypt(buffer, key);
}

public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();

// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

// Create a StreamWriter to write a string
// to the stream.
StreamWriter sw = new StreamWriter(encStream);

// Write the plaintext to the stream.
sw.WriteLine(PlainText);

// Close the StreamWriter and CryptoStream.
sw.Close();
encStream.Close();

// Get an array of bytes that represents
// the memory stream.
byte[] buffer = ms.ToArray();

// Close the memory stream.
ms.Close();

// Return the encrypted byte array.
return buffer;
}

// Decrypt the byte array.
public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
{
// Create a memory stream to the passed buffer.
MemoryStream ms = new MemoryStream(CypherText);

// Create a CryptoStream using the memory stream and the
// CSP DES key.
CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);

// Create a StreamReader for reading the stream.
StreamReader sr = new StreamReader(encStream);

// Read the stream as a string.
string val = sr.ReadLine();

// Close the streams.
sr.Close();
encStream.Close();
ms.Close();

return val;
}

Here make sure that the key for the encryption and the decryption should be same in order to make it work properly. More on this will be posted soon.

  1. September 10, 2008 at 1:34 pm | #1

    Great article.Thanks

  2. April 15, 2009 at 1:22 pm | #2

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security.Cryptography;
    using System.IO;

    namespace Chp12_Exercise2
    {
    class Program
    {
    static void Main(string[] args)
    {
    /* Prompt user to enter text */
    Console.Write(“Enter text : “);
    string text = Console.ReadLine();
    Console.WriteLine(“\nYour plane text:” + text);

    /* Create an encypter */
    UnicodeEncoding encoder = new UnicodeEncoding();
    byte[] dataToEncrypt = encoder.GetBytes(text);
    byte[] encryptedData;
    byte[] decreptedData;
    RSACryptoServiceProvider encrypter = new RSACryptoServiceProvider();

    /* Encypter and display */
    encryptedData = encrypter.Encrypt(dataToEncrypt, true);
    string encryptedText = encoder.GetString(encryptedData);
    Console.WriteLine(“\nEncrypted text: ” + encryptedText);

    /* Write encrypted text to file */
    FileStream encryptFileStream = new FileStream(“c://EncryptedFile.txt”, FileMode.OpenOrCreate);
    StreamWriter encryptFileWriter = new StreamWriter(encryptFileStream);
    encryptFileWriter.WriteLine(encryptedText);
    Console.WriteLine(“\nEncrypted data write to c://EncryptedFile.txt”);
    encryptFileWriter.Close();
    encryptFileStream.Close();

    /* Decrypt and display */
    decreptedData = encrypter.Decrypt(encryptedData, true);
    string decryptedText = encoder.GetString(decreptedData);
    Console.WriteLine(“\nDecrypted text: ” + decryptedText);

    /* Write decrypted text to file */
    FileStream decryptFileStream = new FileStream(“c://DecryptedFile.txt”, FileMode.OpenOrCreate);
    StreamWriter decryptFileWriter = new StreamWriter(decryptFileStream);
    decryptFileWriter.WriteLine(decryptedText);
    Console.WriteLine(“\nEncrypted data write to c://DecryptedFile.txt”);
    decryptFileWriter.Close();
    encryptFileStream.Close();

    /* Clean up encrypter */
    encoder = null;
    encrypter.Clear();
    encrypter = null;
    }
    }
    }

  3. April 15, 2009 at 1:24 pm | #3

    using System;
    using System.Security.Cryptography;
    using System.IO;

    namespace Chp12_Exercise1
    {
    class Exercise
    {
    static void Main(string[] args)
    {
    /*Prompt user to enter plain text*/
    Console.WriteLine(“Enter text to encrypt and decrypt:”);
    string text = Console.ReadLine();
    Console.WriteLine(“\nPlain Text: ” + text);

    /* Generate key*/
    DESCryptoServiceProvider key = new DESCryptoServiceProvider();

    /* Call Encrypt() method to encrypt and show */
    byte[] EncryptedText = Encrypt(text, key);
    Console.Write(“\nEncrypted text: “);
    foreach (byte EText in EncryptedText)
    {
    Console.Write(EText);
    }
    Console.WriteLine(“\n\nDecrypted text: ” + Decrypt(EncryptedText, key));
    }

    /*Encrypt method*/
    public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
    {
    /* Create a memory stream */
    MemoryStream mStream = new MemoryStream();

    /* Create a CryptoStream */
    CryptoStream CryStream = new CryptoStream(mStream, key.CreateEncryptor(), CryptoStreamMode.Write);

    /* Create a StreamWriter to write a string to the stream */
    StreamWriter sw = new StreamWriter(CryStream);

    /* Write the plaintext to the stream */
    sw.WriteLine(PlainText);

    /* Close the StreamWriter and CryptoStream */
    sw.Close();
    CryStream.Close();

    /* Get an array of bytes that represents the memory stream */
    byte[] EncryptedText = mStream.ToArray();

    /* Close the memory stream */
    mStream.Close();

    /* Return the encrypted byte */
    return EncryptedText;
    }

    /* Decrypt method */
    public static string Decrypt(byte[] EncryptedText, SymmetricAlgorithm key)
    {
    /*Create a memory stream */
    MemoryStream mStream = new MemoryStream(EncryptedText);

    /* Create a CryptoStream using the memory stream and the CSP DES key */
    CryptoStream encStream = new CryptoStream(mStream, key.CreateDecryptor(), CryptoStreamMode.Read);

    /* Create a StreamReader for reading the stream */
    StreamReader sr = new StreamReader(encStream);

    /* Read the stream as a string */
    string value = sr.ReadLine();

    /* Close the streamStream */
    sr.Close();
    encStream.Close();
    mStream.Close();
    /* Return the string data*/
    return value;
    }
    }
    }

  1. No trackbacks yet.