This site was last updated:Monday 13 October 2008, 11:30 GMT

Educational Community

DATA ENCRYPTION IN C#
(0 votes, average: 0 out of 5)
Written by Christopher   
Thursday, 12 June 2008 18:19

INTRODUCTION

Data is most vulnerable when transferred across a network or stored on a disk drive. An attacker with some knowledge and skills can bypass security software and gain access to sensitive data. However, you can use cryptography to your advantage. You can protect your private data by encrypting their information with a key code, only you will know.

CRYPTOGRAPHY WITH C#

Most of the .Net’s cryptography functionality is in the System.Security.Cryptography namespace. The following encryption algorithms are included in it:

·         RijndaelManaged: It uses 128 to 256 bits in 32-bit increments. It is also known as the AES algorithm (Advanced Encryption Standard). It is the only encryption class fully managed. All other classes are unmanaged.

·         RC2: Similar to DES with variable key sizes.

·         DES: It uses 52 bits key sizes. It is not very safe but remains popular due to its compatibility with legacy platforms.

·         TripleDES:  The .Net implementation of the Triple DES algorithm. It applies the DES algorithm three consecutive times.

All classes share the following properties:

·         BlockSize: Gets or sets the block size of the cryptographic operation in bits. It is the number of bits the cryptography algorithm process at any given time.

·         IV: Gets or sets the initialization vector for the symmetric algorithms. Both the encryptor and decryptor must specify the same initialization vector value.

·         Key: Gets or set the secret key for the algorithm’s operation. If left unspecified they will be automatically generated. During decryption you must specify the same value used for the encryption.

Using encryption is similar to reading and writing files and streams and requires only some additional lines of code. In general, you should perform the following tasks:

1.       Create a Stream object to interface with the file you want to encrypt/decrypt.

2.       Create the encryption algorithm object.

3.       Specify the algorithm’s Key, IV or both.

4.       Create an ICryptoTransform object with the appropriate method: CreateEncryptor() or CreateDecryptor() .

5.       Create a CryptoStream object.

6.       Read from or write to the CryptoStream object.

 

CODE SAMPLE

 

The following example reads a text file, encrypts it with the Rijndael algorithm and saves the encrypted data as a new file:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

using System.Security.Cryptography;

 

 

namespace DataEncryption

{

    class Program

    {

        static void Main(string[] args)

        {

            string input = @"C:\input.txt";

            string output = @"C:\output.txt";

            // step1: Create the stream objects

            FileStream inputStream = new FileStream(input, FileMode.Open, FileAccess.Read);

            FileStream outputStream = new FileStream(output, FileMode.OpenOrCreate, FileAccess.Write);

            //step2: Create the symmetric algorithm object

            SymmetricAlgorithm myAlgo = new RijndaelManaged();

           //step3: Specify the key

            myAlgo.GenerateKey();

            //Read the unencrypted data

            byte[] fileData = new byte[inputStream.Length];

            inputStream.Read(fileData, 0, (int)inputStream.Length);

            //step4:  Create the ICrypto transform object

            ICryptoTransform encryptor = myAlgo.CreateEncryptor();

            //step5: Create the CryptoStream object

            CryptoStream encryptStream = new CryptoStream(outputStream, encryptor, CryptoStreamMode.Write);

            //step6: Write the contents to the crypto stream

            encryptStream.Write(fileData, 0 , fileData.Length);

            encryptStream.Close();

            inputStream.Close();

            outputStream.Close();

 

        }

    }

}

 

Check your files in the “C:\” directory and see the output produced by the algorithm. In order to decrypt the produced file you should know the Key and IV used. You can change the code in the previous example to store them in a file or you could manually specify them. To reverse the process make the following modifications:

1.       Change the code for step 3 to read the key and IV that was used to encrypt the data.

2.       Change the code for step 4 to use the CreateDecryptor method instead of the CreateEncryptor.

3.       Change the code for step 5 to use the CryptoStreamMode.Read.

4.       Change the code for step 6 to read from the CryptoStream object.

 

 

Trackback(0)
Comments (0)add comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
smile
wink
laugh
grin
angry
sad
shocked
cool
tongue
kiss
cry
smaller | bigger

busy
 

User Menu

None
Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor KNOGE.com offers free video and text tutorials on various softwares also free resources to improve your economy and start making money online. THIS IS ONLY A TEST AD TO DISPLAY HOW IT MIGHT LOOK Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor Become a sponsor

Adobe InDesign CS3 - Working with objects

Objects in Adobe InDesign CS3 are the basic building block of any design that you do. In this video tutorial you will learn how to create those blocks easily in...

InDesign Videos | Christopher

Read More

Adobe InDesign CS3 - Working with Panels & More

In this Adobe InDesign CS3 video tutorial you will learn how to set keys and work with panels, you will also learn how to create customized keyboard shortcuts for optimized...

InDesign Videos | Christopher

Read More

Adobe Photoshop CS3 - Create customized rust on cars

This Adobe Photoshop CS3 video tutorial will teach you how to create your own customized rust on cars for a more grungy and perhaps dusty effect. This technique does also...

Photoshop Videos | Christopher

Read More
100%
-
+
3
Show options