matteoathen

Hi All,

I need help.

Does anyone know how to encrpt a text file using TripleDES I have tried some codes from the internet. Unfortunately, they doesn't work.

Here are my codes:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Security.Cryptography;

using System.IO;

namespace ConvertKey

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void menuItem1_Click(object sender, EventArgs e)

{

Application.Exit();

}

private void Form1_Load(object sender, EventArgs e)

{

try

{

// Create a new TripleDESCryptoServiceProvider object

// to generate a key and initialization vector (IV).

TripleDESCryptoServiceProvider tDESalg = new TripleDESCryptoServiceProvider();

// File to decrypt

string FileName = "EnText.txt";

// Decrypt the text from a file using the file name, key, and IV.

string Final = DecryptTextFromFile(FileName, tDESalg.Key, tDESalg.IV);

label1.Text = Final;

}

catch (Exception h)

{

label1.Text = h.message;

}

}

public static string DecryptTextFromFile(String FileName, byte[] Key, byte[] IV)

{

try

{

// Open the specified file.

FileStream fStream = File.Open(FileName, FileMode.Open);

// Create a CryptoStream using the FileStream

// and the passed key and initialization vector (IV).

CryptoStream cStream = new CryptoStream(fStream,

new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),

CryptoStreamMode.Read);

// Create a StreamReader using the CryptoStream.

StreamReader sReader = new StreamReader(cStream);

// Read the data from the stream

// to decrypt it.

string val = sReader.ReadLine();

// Close the streams and

// close the file.

sReader.Close();

cStream.Close();

fStream.Close();

// Return the string.

return val;

}

catch (CryptographicException e)

{

label2.Text = e.message;

}

catch (UnauthorizedAccessException e)

{

label3.Text = e.message;

}

}

}

}

EnText.txt is a text file containing some TripleDES encrypted text.

By right, label1 should show some encrypted text. However, in my case, it shows nothing.

Hope someone could clear my doubt.

Many thanks in advance!

Regards,

Matteoathen



Re: Smart Devices VB and C# Projects Decrypt a text file using TripleDES

Christopher Fairbairn

Hi Matteoathen,

I think your problem is that you are not setting the Key and Initialisation Vector (IV) properties of the TripleDESCryptoServiceProvider . This is causing the TripleDESCryptoServiceProvider to generate random Key and IV values, which will not be able to successfully decrypt your message.

To decrypt your message, you must set the Key and IV properties to the same values you used during the encryption process.

You may find the following blog entries on my blog of interest:

The first blog entry is a sample application (source is available to download) which should demonstrate how you can encrypt and decrypt the contents of a file.

While the second blog entry, discusses the specifics of how encryption works within the .NET Compact Framework in more detail.

Any feedback on these posts would be greatly appreciated.

Hope this helps,

Christopher Fairbairn






Re: Smart Devices VB and C# Projects Decrypt a text file using TripleDES

matteoathen

Hi Christ,

Now I see what the problem is. Thanks a lot for helping .

Regards,

Matteoathen





Re: Smart Devices VB and C# Projects Decrypt a text file using TripleDES

matteoathen

Hi Christ,

I have tried the sample code given in your blog. It was helpful .

However, there are some of the things which I am not sure of.

I used your sample project to generate a key (TripleDES 128). Then, I used the key to encrypt a text. Next, I used the same key to decrypt a file which contain the encrypted text. Unfourtunately, there were some decryption errors.

My question is that if my method stated above is correct

Here are my codes:

protected SymmetricAlgorithm algorithm = null;

private void Form1_Load(object sender, EventArgs e)

{

algorithm = new TripleDESCryptoServiceProvider();

// Set the algorithm's KeySize property

algorithm.KeySize = 128;

// Generate key of the required size

string yes = "RYmOKGAboXwQ6IrEgZmjuw==";

algorithm.Key = ParseKey(yes);

try

{

// Create a stream which reads from the desired file

using (Stream input = new FileStream("testA.txt", FileMode.Open))

{

// and wrap it up in a crypto stream that uses the desired encryption algorithm to

// decrypt data it reads from the underlying input stream.

using (CryptoStream cs = new CryptoStream(input, algorithm.CreateDecryptor(), CryptoStreamMode.Read))

{

// Now we can use the crypto stream (cs) to read decrypted data from

// the file. In this example we'll use a StreamReader to read the

// contents of the file as a string suitable to display in the text

// box.

using (TextReader tr = new StreamReader(cs))

{

label1.Text = tr.ReadToEnd();

}

}

}

}

catch (Exception)

{

label1.Text = "An error occurred during decryption";

}

}

Many thanks in advance.

Cheers,

Matteoathen





Re: Smart Devices VB and C# Projects Decrypt a text file using TripleDES

Christopher Fairbairn

Hi Matteoathen,

I must appologise for that... The incorrect version was uploaded to the blog. The version which made it to the blog was not the latest version and it suffered from a similiar problem to your original code sample.

I have uploaded the correct version of the sample project. You will notice in the original version that the initialisation vector (IV) property was not explictly set in the encrypt and decrypt methods. This meant that the encryption/decryption processes were using randomised values.

This meant strings encrypted with the sample application could not be decrypted once you had changed the encryption algorithm (or restarted the app). Since both occurranges caused the IV value to be regenerated again.

Please download the updated version (http://www.christec.co.nz/blog/wp-content/uploads/2007/08/fileencryptionexample.zip), and see the minor changes from the incorrect version which was previously uploaded.

In the case of your latest code snippet see if you change

// Generate key of the required size

string yes = "RYmOKGAboXwQ6IrEgZmjuw==";

algorithm.Key = ParseKey(yes);

into

// Generate key of the required size

string yes = "RYmOKGAboXwQ6IrEgZmjuw==";

algorithm.Key = ParseKey(yes);

algorithm.IV = new Byte[algorithm.BlockSize / 8]; // set IV to all zero's (not the wisest choice)

if this helps fix the problem. This additional line would also need to be in your encryption method. Typically a more "randomised" IV would be utilised, something which possibly is more message specific. How to generate the IV value, or transmit it, is reasonably application specific.

Sorry for the inconveniance,

Christopher Fairbairn






Re: Smart Devices VB and C# Projects Decrypt a text file using TripleDES

matteoathen

Hi Christ,

Thanks for the great help . Really appreciate it . LOL

Regards,

Matteoathen





Re: Smart Devices VB and C# Projects Decrypt a text file using TripleDES

matteoathen

Hi Christ,

I encountered an error while building your latest code.

It points to [algorithm.BlockSize / 8] where a constant value is expected there. Guess that you might forget to include the value.

Regards,

Matteoathen





Re: Smart Devices VB and C# Projects Decrypt a text file using TripleDES

Christopher Fairbairn

Sorry about that (it isn't my day is it...)

Remove the spurious { } at the end of that statement...






Re: Smart Devices VB and C# Projects Decrypt a text file using TripleDES

matteoathen

Hi Christ,

My decryption codes is working now .

Thanks very much for the great help.

Regards,

Matteoathen