locked
Problem my project RRS feed

  • Question

  • hi, i've problem project.

    i make project encrypt and decrypt using algorithm AES with C#.

    i've problem in decrypt, when i encrypt file time faster than decrypt.

    I do not think there is a problem, but please check my coding for the optimization, the possibility of my making functions that are not good.

    this my code:

    using System;
    using System.IO;
    using System.Windows.Forms;
    
    namespace latihan_29_testing
    {
      public partial class Form1 : Form
      {
        private string _nama = "";
        private string _namaFile = "";
        private static string[] _strCopy = new string[5];
        private static string[] _replace = new string[5];
        private static byte[] _plaintext;
        private static byte[] _cipher;
        private static string[] _strnama = new string[5];
        private int _count = 0;
    
        public Form1()
        {
          InitializeComponent();
        }
        private void Button3Click(object sender, EventArgs e)
        {
          var fileku = ofd1.ShowDialog();
          try
          {
            if (fileku == DialogResult.OK)
            {
              _strnama[_count] = ofd1.FileName;
              _nama = Path.GetFileName(_strnama[_count]);
              _strCopy[_count] = @"E:\En\" + _nama + ".encx";
              button5.Enabled = true;
              datax.Items.Add(_strnama[_count]);
              _count++;
            }
          }
          catch (Exception ex)
          {
            MessageBox.Show(ex.Message);
          }
        }
    
        private void Button5Click(object sender, EventArgs e)
        {
          if ((kunci.Text == "") || (kunci.TextLength < 16))
          {
            MessageBox.Show(Properties.Resources.Form1_button5_Click_kunci_kunci_kurang_dari_16_atau_kunci_belum_diisi, Properties.Resources.Form1_Button5Click_peringatan, MessageBoxButtons.OK,
                    MessageBoxIcon.Asterisk);
          }
          else
          {
            ts1.Text = "Proses starting...";
            _namaFile = Path.GetFileName(_strnama[_count]);
            for (int i = 0; i < _count; i++)
            {
              _namaFile = Path.GetFileName(_strnama[i]);
              //File.Copy(_strnama[i], _strCopy[i]);
              _plaintext = File.ReadAllBytes(_strnama[i]);
              ts1.Text = "Proses reading " + _namaFile + "...";
              var arrPlaint = Baca.Enkrip2(_plaintext, kunci.Text);
              ts1.Text = "Proses enkripsi " + _namaFile + "...";
              File.WriteAllBytes(_strCopy[i], arrPlaint);
              ts1.Text = "Proses writing " + _namaFile + "...";
              msg.Items.Add(_namaFile + " Success Enkripsi");
              ts1.Text = "File " + _namaFile + " Success enkripsi";
            }
            _count = 0;
            ts1.Text = "All File Successfull to enkripsi";
          }
        }
    
        private void Button4Click(object sender, EventArgs e)
        {
          var fileku2 = ofd2.ShowDialog();
          if (fileku2 == DialogResult.OK)
          {
            _strnama[_count] = ofd2.FileName;
            _nama = Path.GetFileNameWithoutExtension(_strnama[_count]);
            _replace[_count] = @"E:\En\" + _nama;
            datax.Items.Add(_strnama[_count]);
            button6.Enabled = true;
            _count++;
          }
        }
    
        private void Button6Click(object sender, EventArgs e)
        {
          if ((kunci.Text == "") || (kunci.TextLength < 16))
          {
            MessageBox.Show(Properties.Resources.Form1_button5_Click_kunci_kunci_kurang_dari_16_atau_kunci_belum_diisi, Properties.Resources.Form1_Button5Click_peringatan, MessageBoxButtons.OK,
                    MessageBoxIcon.Asterisk);
          }
          else
          {
            ts1.Text = "Proses starting...";
            for (int i = 0; i < _count; i++)
            {
              _namaFile = Path.GetFileName(_strnama[i]);
              //File.Copy(_strnama[i], _replace[i]);
              _cipher = File.ReadAllBytes(_strnama[i]);
              //ts1.Text = "Proses reading " + _namaFile + "...";
              var arrCipher = Baca.Dekrip2(_cipher, kunci.Text);
              //ts1.Text = "Proses enkripsi " + _namaFile + "...";
              File.WriteAllBytes(_replace[i], arrCipher);
              //ts1.Text = "Proses writing " + _namaFile + "...";
              msg.Items.Add(_namaFile + " Success Dekripsi");
              //ts1.Text = "File " + _namaFile + " Success dekripsi";
            }
            _count = 0;
            ts1.Text = "All File Successfull to enkripsi";
          }
        }
      }
    }
    
    
    using System.Text;
    using AESDecryption;
    using AESEncription;
    
    namespace latihan_29_testing
    {
      class Baca
      {
        
        private static byte[] _getout;
        private static byte[] _getout2Bytes;
    
    
        public static byte Tkurang(byte num)
        {
          byte[] kbox = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
          return kbox[num];
        }
    
        public static byte[] Enkrip2(byte[] plaintext, string text)
        {
          var p = plaintext.Length;
          var count = 0;
          var backcount = 0;
    
          var state = new byte[16];
          byte[] bufferkey = Encoding.ASCII.GetBytes(text);
    
          //Padding Byte
          var data = (byte)(p % 16);
          var perulangan = (byte)(16 - data);
          var kekurangan = Tkurang(perulangan);
          System.Array.Resize<byte>(ref plaintext, (plaintext.Length + perulangan));
          var cipher = new byte[16];
          _getout = new byte[plaintext.Length];
    
          for (int i = (p - 1); i < plaintext.Length; i++)
          {
            if (i == (plaintext.Length - 1))
            {
              plaintext[i] = kekurangan;
            }
            else
            {
              plaintext[i] = 0x00;
            }
          }
    
          //proses enkripsi
          while (plaintext.Length > count)
          {
            for (int k = 0; k < 16; k++)
            {
              state[k] = plaintext[count++];
            }
            var aes = new AESEncript(bufferkey, 4);
    
            aes.Cipher(state, cipher);
    
            for (var k = 0; k < 16; k++)
            {
              _getout[backcount] = cipher[k];
              backcount++;
            }
          }
    
          return _getout;
        }
    
        
        public static byte TLast(byte num)
        {
          byte[] kbox = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
          return kbox[num];
        }
    
        
        public static byte[] Dekrip2(byte[] cipher, string text)
        {
          var p = cipher.Length;
          int count = 0;
          var backcount = 0;
    
          var state = new byte[16];
          var plain = new byte[16];
          _getout2Bytes = new byte[p];
    
          //proses dekripsi
          byte[] bufferkey = Encoding.ASCII.GetBytes(text);
          while (p > count)
          {
            for (int k = 0; k < 16; k++)
            {
              state[k] = cipher[count++];
            }
    
            var aesDec = new AESdecrypt(bufferkey, 4);
    
            aesDec.InvCipher(state, plain);
    
            for (int k = 0; k < 16; k++)
            {
              _getout2Bytes[backcount] = plain[k];
              backcount++;
            }
          }
    
          //padding dekripsi
          if ((_getout2Bytes.Length - 1) < 0x11)
          {
            var cek = TLast(_getout2Bytes[_getout2Bytes.Length - 1]);
            System.Array.Resize<byte>(ref _getout2Bytes, (_getout2Bytes.Length - cek));
          }
    
          return _getout2Bytes;
        }
      }
    }

    please optimization my code,.

    thanks

    regads

    Saturday, July 3, 2010 1:03 PM

Answers

  • Hi,

    How do you determine the time of encryption and decryption?

    You may take a look at the article which shows how to implement them in c#:
    http://msdn.microsoft.com/en-us/magazine/cc164055.aspx

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by GunkMahendra Friday, July 9, 2010 6:36 AM
    Friday, July 9, 2010 6:02 AM

All replies

  • Hi,

    How do you determine the time of encryption and decryption?

    You may take a look at the article which shows how to implement them in c#:
    http://msdn.microsoft.com/en-us/magazine/cc164055.aspx

    Harry


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by GunkMahendra Friday, July 9, 2010 6:36 AM
    Friday, July 9, 2010 6:02 AM
  • thanks, this really help me,.
    Friday, July 9, 2010 6:36 AM