Sarbjit

Hello Friends.

I am trying encryption first time and i don't know how to implement 3DES .net. I am doing in C#. Can any body help regarding . If u know any link regarding this please tell me.

Thanks in advance.



Re: Visual C# General 3DES encryption

ScubaSteve20001

the .NET framework already has built in support for 3DES. Look for the following namespace:

System.Security.Cryptography.TripleDESCryptoServiceProvider





Re: Visual C# General 3DES encryption

kleinma

moved to C# forum from VB.NET




Re: Visual C# General 3DES encryption

Martin Xie - MSFT

Thank you ScubaSteven for your instruction!

Sarbjit, welcome to this forum!

If you have any difficulties in future developing, please feel free to come back.

The algorithm implementations TripleDESCryptoServiceProvider corresponds to the simple name 3DES.

The following code example creates a TripleDESCryptoServiceProvider object and uses it to encrypt and decrypt data in a file.

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

class TrippleDESCSPSample
{

static void Main()
{
try
{
// Create a new TripleDESCryptoServiceProvider object
// to generate a key and initialization vector (IV).

TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();

// Create a string to encrypt.
string sData = "Here is some data to encrypt.";

// Encrypt the string to an in-memory buffer.
byte[] Data = EncryptTextToMemory(sData, tDESalg.Key, tDESalg.IV);

// Decrypt the buffer back to a string.
string Final = DecryptTextFromMemory(Data, tDESalg.Key, tDESalg.IV);

// Display the decrypted string to the console.
Console.WriteLine(Final);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}

}

public static byte[] EncryptTextToMemory(string Data, byte[] Key, byte[] IV)
{
try
{
// Create a MemoryStream.
MemoryStream mStream = new MemoryStream();

// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream cStream = new CryptoStream(mStream,
new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
CryptoStreamMode.Write);

// Convert the passed string to a byte array.
byte[] toEncrypt = new ASCIIEncoding().GetBytes(Data);

// Write the byte array to the crypto stream and flush it.
cStream.Write(toEncrypt, 0, toEncrypt.Length);
cStream.FlushFinalBlock();

// Get an array of bytes from the
// MemoryStream that holds the
// encrypted data.
byte[] ret = mStream.ToArray();

// Close the streams.
cStream.Close();
mStream.Close();

// Return the encrypted buffer.
return ret;
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}

}

public static string DecryptTextFromMemory(byte[] Data, byte[] Key, byte[] IV)
{
try
{
// Create a new MemoryStream using the passed
// array of encrypted data.
MemoryStream msDecrypt = new MemoryStream(Data);

// Create a CryptoStream using the MemoryStream
// and the passed key and initialization vector (IV).
CryptoStream csDecrypt = new CryptoStream(msDecrypt,
new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
CryptoStreamMode.Read);

// Create buffer to hold the decrypted data.
byte[] fromEncrypt = new byte[Data.Length];

// Read the decrypted data out of the crypto stream
// and place it into the temporary buffer.
csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

//Convert the buffer into a string and return it.
return new ASCIIEncoding().GetString(fromEncrypt);
}
catch(CryptographicException e)
{
Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
return null;
}
}
}