3DES Encryption and decryption - Sample code

505
Views
1
Answers

Is 3DES encryption and decryption secure? How do we implement in .net?

edit | flag
William Patrick
Asked on: Jan 06, 2012 at 2:34AM

1 Answers

95
5
95

Though 3DES is secure a new technology AES is more secure than DES.

TripleDES uses three successive iterations of the DES algorithm. It can use either two or three 56-bit keys.

This algorithm supports key lengths from 128 bits to 192 bits in increments of 64 bits.

The sample code below has method that uses TripleDESCryptoServiceProvider with the specified key (Key)
and initialization vector (IV) to encrypt a file specified by inName. It then outputs the encrypted result to the file specified by outName.

 // Encrypting code
 
private static void EncryptData(String inName, String outName, byte[] tdesKey, byte[] tdesIV)
   
{    
       
//Create the file streams to handle the input and output files.
       
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
       
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
        fout
.SetLength(0);

       
//Create variables to help with read and write.
       
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
       
long rdlen = 0;              //This is the total number of bytes written.
       
long totlen = fin.Length;    //This is the total length of the input file.
       
int len;                     //This is the number of bytes to be written at a time.

       
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();          
       
CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(tdesKey, tdesIV), CryptoStreamMode.Write);

       
Console.WriteLine("Encrypting...");

       
//Read from the input file, then encrypt and write to the output file.
       
while(rdlen < totlen)
       
{
            len
= fin.Read(bin, 0, 100);
            encStream
.Write(bin, 0, len);
            rdlen
= rdlen + len;
           
Console.WriteLine("{0} bytes processed", rdlen);
       
}

        encStream
.Close();                    
   
}

// Decrypting code
private static void EncryptData(String inName, String outName, byte[] tdesKey, byte[] tdesIV)
       
{
           
//Create the file streams to handle the input and output files.
           
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
           
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
            fout
.SetLength(0);

           
//Create variables to help with read and write.
           
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
           
long rdlen = 0;              //This is the total number of bytes written.
           
long totlen = fin.Length;    //This is the total length of the input file.
           
int len;                     //This is the number of bytes to be written at a time.

           
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
           
CryptoStream encStream = new CryptoStream(fout, tdes.CreateDecryptor(tdesKey, tdesIV), CryptoStreamMode.Write);

           
Console.WriteLine("Encrypting...");

           
//Read from the input file, then encrypt and write to the output file.
           
while (rdlen < totlen)
           
{
                len
= fin.Read(bin, 0, 100);
                encStream
.Write(bin, 0, len);
                rdlen
= rdlen + len;
               
Console.WriteLine("{0} bytes processed", rdlen);
           
}

            encStream
.Close();
       
}
edit | flag
Gokul A
Answered on: Jan 06, 2012 at 2:42AM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags