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