campnerd
campnerd

Reputation: 74

Encryption and Decryption of a text file

Ok so I have a small program that I built in C# winforms that is basically using security cryptology and I am trying to figure out what I did wrong in the code. It will encrypt the text file fine. However, when I paste the encrypted file in the textbox and hit Decrypt, it does not decrypt the file but encrypts it again.

I am wondering if I did something wrong with the code.

namespace Encrypted
{
    class Encryptor
    {
        public static string IV = "1a1a1a1a1a1a1a1a";
        public static string Key = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a";

        public static string Encrypt (string decrypted)
        {
            byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
            AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
            endec.BlockSize = 128;
            endec.KeySize = 256;
            endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            endec.Padding = PaddingMode.PKCS7;
            endec.Mode = CipherMode.CBC;
            ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
            byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
            icrypt.Dispose();
            return Convert.ToBase64String(enc);
        }

        public static string Decrypted(string encrypted)
        {
            byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(encrypted);
            AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
            endec.BlockSize = 128;
            endec.KeySize = 256;
            endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
            endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
            endec.Padding = PaddingMode.PKCS7;
            endec.Mode = CipherMode.CBC;
            ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
            byte[] enc = icrypt.TransformFinalBlock(textbytes, 0,     textbytes.Length);
            icrypt.Dispose();
            return ASCIIEncoding.ASCII.GetString(enc);
        }
    }
 }

This is the class above and below is the Form1:

namespace Encrypted
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            string dir = textBox1.Text;
            Directory.CreateDirectory("data\\" + dir);

            var sw = new StreamWriter("data\\" + dir + "data.ls");

            string enctxt = Encryptor.Encrypt(textBox1.Text);

            sw.WriteLine(enctxt);

            sw.Close();
        }

        private void btnDecrypt_Click(object sender, EventArgs e)
        {
            string dir = textBox2.Text;
            Directory.CreateDirectory("data\\" + dir);

            var sw = new StreamWriter("data\\" + dir + "data.ls");

            string enctxt = Encryptor.Decrypted(textBox2.Text);

            sw.WriteLine(enctxt);

            sw.Close();
        }
    }

In the Decrypt button do I need to use StreamReader or StreamWriter. Once the encrypted file is in the data.ls I copy it and paste it into the text box for decrypt. However is does not decrypt, it only re-encrypts the file. Is there something I am doing wrong?

Upvotes: 0

Views: 18424

Answers (3)

Ammar Almasri
Ammar Almasri

Reputation: 1

I searched for it and made a program that worked fine with me :) So...

This is the class

   class Encryptor
{
    public static string IV = "1a1a1a1a1a1a1a1a";
    public static string Key = "1a1a1a1a1a1a1a1a1a1a1a1a1a1a1a13";

    public static string Encrypt(string decrypted)
    {
        byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
        AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
        endec.BlockSize = 128;
        endec.KeySize = 256;
        endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
        endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
        endec.Padding = PaddingMode.PKCS7;
        endec.Mode = CipherMode.CBC;
        ICryptoTransform icrypt = endec.CreateEncryptor(endec.Key, endec.IV);
        byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
        icrypt.Dispose();
        return Convert.ToBase64String(enc);
    }

    public static string Decrypted(string encrypted)
    {
        byte[] textbytes = Convert.FromBase64String(encrypted);
        AesCryptoServiceProvider endec = new AesCryptoServiceProvider();
        endec.BlockSize = 128;
        endec.KeySize = 256;
        endec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
        endec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
        endec.Padding = PaddingMode.PKCS7;
        endec.Mode = CipherMode.CBC;
        ICryptoTransform icrypt = endec.CreateDecryptor(endec.Key, endec.IV);
        byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
        icrypt.Dispose();
        return System.Text.ASCIIEncoding.ASCII.GetString(enc);
    }
}

...................................

and this is the form`

        private void button1_Click(object sender, EventArgs e)
    {
        string dir = textBox1.Text;
        Directory.CreateDirectory("data\\" + dir);

        var sw = new StreamWriter("data\\" + dir + "data.ls");

        string enctxt = Encryptor.Encrypt(textBox1.Text);

        sw.WriteLine(enctxt);

        sw.Close();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        string dir = textBox2.Text;

        StreamReader sr = new StreamReader(Application.StartupPath +"\\data\\"+ dir + "data.ls");
        string line = sr.ReadLine();

        textBox1.Text = Encryptor.Decrypted(Convert.ToString(line));
    }

........................

Best regards !

Upvotes: 0

Jim Flood
Jim Flood

Reputation: 8457

It's not only the mistake of not using CreateDecryptor. You also have to decode the ciphertext string from base64, which is what your encrypt returns.

You are doing this:

  1. take string, decode from ASCII to plaintext bytes

  2. take plaintext bytes, encrypt to ciphertext bytes

  3. take ciphertext bytes, convert to base64 string

Then you do this:

  1. take string, decode from ASCII to ciphertext bytes <-- your mistake!

instead:

  1. take string, decode from base64 to ciphertext bytes

  2. take ciphertext bytes, decrypt to plaintext bytes

  3. take plaintext bytes, encode to ASCII.

string -> bytes -> encrypted bytes -> base64 -> encrypted bytes -> bytes -> string

Upvotes: 0

sujith karivelil
sujith karivelil

Reputation: 29006

This is because you are using the Decrypted() also for encrypting the input file by Creating Encryptor. It should Create a Decryptor to perform the Decryption operation. This single word change will reverse the process. the CreateDecryptor Method (Byte[], Byte[]) of AesCryptoServiceProvider class will Creates a symmetric AES decryptor object using the specified key and initialization vector (IV).

ie. Change the following line in the Decrypted() method,

  ICryptoTransform icrypt = endec.CreateDecryptor(endec.Key, endec.IV);

Here is a Reference example from MSDN and you can see a Working example here

Upvotes: 4

Related Questions