-2

I am creating a file encryption application using Advanced Encryption Standard Algorithm, but I find that my application is so slow especially when I am encrypting a large amount of data, ex. 80MB of file size, 30 mins. was already past but my application is not yet done encrypting my file with the size of 80mb.

I am using ECB(Electronic Code Book) mode in my Encryption Algorithm

how can I speed up my application in encrypting a large amount of data? I do some research and I find this, http://en.wikipedia.org/wiki/Speedup, but I'm not sure if this is the answer to my problem... or is it effective if I will used BackgroundWorker? by the way, I am using Visual Studio 2008 in the development of my project.

here is my codes in encrypting a file..

private void cmdEncrypt_Click(object sender, EventArgs e)
{
        AESECB aes = new AESECB();
        FileInfo fInfo = new FileInfo(txtFileSource.Text);

        if (txtFileSource.Text == "")
        {
            MessageBox.Show("Please Select a File", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            return;
        }

        if (txtSecretKey.Text == "")
        {
            MessageBox.Show("Please Enter the password", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            return;
        }

        if (fInfo.Exists == false)
        {
            MessageBox.Show("File Not Found!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Warning);

            return;
        }

        byte[] bytePadding = aes.filePadding(txtFileSource.Text);
        byte[] fByte = aes.getFileByte(txtFileSource.Text);
        int farrLength = (bytePadding.Length + fByte.Length);
        byte[] newFbyte = new byte[farrLength];
        byte[] encryptedFByte = new byte[farrLength];
        int counterBytePadding =0;
        byte firstByte = 0;

        for (int i = 0; i < farrLength; i++)
        {
            if (i < fByte.Length)
            {
                newFbyte[i] = fByte[i];
            }
            else
            {
                newFbyte[i] = bytePadding[counterBytePadding];
                counterBytePadding++;
            }
        }

        int plainFileBlock = newFbyte.Length / 16;

        progressBar1.Maximum = plainFileBlock-1;

        progressBar1.Visible = true;

        int counter = 0;
        int counter2 = 0;

        for (int j = 0; j < plainFileBlock; j++)
        {
            byte[] encfbyte = aes.fileEncrypt(txtSecretKey.Text, newFbyte, counter);

            for (int k = 0; k < 16; k++)
            {
                encryptedFByte[counter2] = encfbyte[k];
                counter2++;
            }

            progressBar1.Value = j;
            counter = counter + 16;

        }

        progressBar1.Visible = false;

        int bytesToRead = encryptedFByte.Length;

        string newPath = txtFileSource.Text + ".aesenc";

        using (FileStream newFile = new FileStream(newPath, FileMode.Create, FileAccess.Write))
        {
            newFile.Write(encryptedFByte, 0, bytesToRead);

        }


        MessageBox.Show("Encryption Done!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
user188228
  • 19
  • 1
  • 2
  • 7

2 Answers2

2

It's all very well being "confident in your codes", but all good programmers measure.

This is simple, do this:

using System.Diagnostics;

var startTime = DateTime.Now;
//some code here
Debug.WriteLine("This took {0}", DateTime.Now.Subtract(startTime));

Then look at your output window in VS (View->Output).

By wrapping different parts of the method with these two lines you will identify the slow bit.

My suspicions are on the loop where you copy 80MB byte by byte. Try Array.Resize here.

weston
  • 54,145
  • 21
  • 145
  • 203
  • how can I identify what method is slow? because all the result is 0:00:00 – user188228 Jun 15 '12 at 10:23
  • Start wide, with the first line at the top of all of your code, and the last line at the bottom. That should not show zero. Then narrow it down. You can add multiple of the second lines to do this, giving each a different bit of text, just remember the time shown is the total time from the start line to that point but you can reset the clock with the line: `startTime = DateTime.Now;` at any time. – weston Jun 15 '12 at 10:35
0

As suggested by weston, do measure your code in order to identify what is being slow. That said, it is probably the file encryption for loop which is slowing you down. If so you can definitely speed up the process by using more than 1 cpu. Look up the parallel constructs in C#: "Parallel.For".

Here is a simple example: http://www.dotnetcurry.com/ShowArticle.aspx?ID=608