Ejemplo n.º 1
1
        //enkripsi
        private void button2_Click(object sender, EventArgs e)
        {
            //menyimpan hasil paling akhir
            String result;

            //menyimpan hasil tiap mode
            byte[] modeResult = null;
            String mode = "";

            //check input file
            if (openFileDialog1.FileName.Equals("openFileDialog1"))
            {
                MessageBox.Show("Anda belum memasukan input file", "Peringatan", MessageBoxButtons.OK);
                return;
            }

            //validate key and IV
            Regex reg = new Regex("^[a-zA-Z0-9]*$");
            //key.length >= 8-byte
            if (keyBox.Text.Length < 8)
            {
                MessageBox.Show("Panjang kunci kurang dari 64-bit", "Peringatan", MessageBoxButtons.OK);
                return;
            }
            else if(!reg.IsMatch(keyBox.Text))
            {
                MessageBox.Show("Kunci hanya boleh mengandung karakter alfanumerik", "Peringatan", MessageBoxButtons.OK);
                return;
            }
            else
            {
                //IV.length = key.length
                if ((!radioButton2.Checked && !radioButton1.Checked) && (ivBox.Text.Length != keyBox.Text.Length))
                {
                    MessageBox.Show("Panjang IV tidak sama dengan panjang kunci", "Peringatan", MessageBoxButtons.OK);
                    return;
                }
                else if ((!radioButton2.Checked && !radioButton1.Checked) && (ivBox.Text.Contains(".")))
                {
                    MessageBox.Show("IV tidak boleh mengandung karakter \".\"", "Peringatan", MessageBoxButtons.OK);
                    return;
                }
            }

            //read file content (plaintext)
            String dialogfilename = openFileDialog1.FileName;
            byte[] plain = System.IO.File.ReadAllBytes(dialogfilename);

            //check used mode
            if (radioButton1.Checked)
            {
                //ECB mode
                mode = "ECB";
                ECB ecb = new ECB (plain, null, keyBox.Text, ivBox.Text);
                modeResult = ecb.encrypt();
            }
            else if (radioButton2.Checked)
            {
                //generate IV
                //IV==key
                //CBC mode
                mode = "CBC";
                CBC cbc = new CBC(Encoding.ASCII.GetString(plain), "", keyBox.Text, keyBox.Text);
                ivBox.Text = keyBox.Text;
                modeResult = cbc.encipher(plain);
            }
            else if (radioButton3.Checked)
            {
                //CFB mode
                mode = "CFB";
                CFB cfb = new CFB(plain, null ,keyBox.Text,ivBox.Text);
                modeResult = cfb.encrypt();
            }
            else if (radioButton4.Checked)
            {
                //OFB mode
                mode = "OFB";
                OFB ofb = new OFB(plain, null, keyBox.Text, ivBox.Text);
                modeResult = ofb.encrypt();
            }
            //convert byte to hex
            result = ByteArrayToString(modeResult);

            //set header
            result += "." + ivBox.Text + "." + Path.GetFileNameWithoutExtension(filepath) + Path.GetExtension(filepath) + "." + mode + "." + (keyBox.Text.Length- (plain.Length % keyBox.Text.Length)).ToString();
            textBox3.Text = result;
        }
Ejemplo n.º 2
0
        //dekripsi
        private void button3_Click(object sender, EventArgs e)
        {
            //header variable
            String iv;
            int padding;
            byte[] content = new byte[1];
            String mode = "";

            //instead of keyBox.Text use this
            String newKey = keyBox.Text;

            //result for each mode
            byte[] modeResult = new byte[keyBox.Text.Length];

            //validate input file
            if (openFileDialog1.FileName.Equals("openFileDialog1"))
            {
                MessageBox.Show("Anda belum memasukan input file", "Peringatan", MessageBoxButtons.OK);
                return;
            }

            //validasi kunci
            //key.length >= 8-byte
            if (keyBox.Text.Length < 8)
            {
                MessageBox.Show("Panjang kunci kurang dari 64-bit", "Peringatan", MessageBoxButtons.OK);
                return;
            }

            //read file content (cipher)
            String dialogfilename = openFileDialog1.FileName;
            String cipher = System.IO.File.ReadAllText(dialogfilename);

            //no need for iv

            //get header
            String[] header = cipher.Split('.');
            if (header.Length != 6)
            {
                MessageBox.Show("Bukan file yang dapat didekripsi", "Peringatan", MessageBoxButtons.OK);
                return;
            }
            else
            {
                filepath = header[2];
                iv = header[1];
                mode = header[4];
                padding = Int32.Parse(header[5]);
                content = StringToByteArray(header[0]);
                extension = header[3];

                //validate key
                if (content.Length % keyBox.Text.Length != 0 && !mode.Equals("CBC"))
                {
                    newKey = "";
                    Random rnd = new Random(content.Length);
                    for (int i = 0; i < (content.Length); i++)
                    {
                        newKey += rnd.Next() % 255;
                    }
                }

                if (mode.Equals("ECB"))
                {
                    //ECB mode
                    ECB ecb = new ECB(null, content, newKey, iv);
                    byte[] pbytes = ecb.decrypt();
                    Console.WriteLine("hasil dekripsi: {0}", ByteArrayToString(pbytes));
                    textBox3.Text = ByteArrayToString(pbytes);

                    if (extension.Equals("txt"))
                    {
                        //show plaintext if using text extension
                        textBox3.Text += Environment.NewLine + Environment.NewLine + Encoding.ASCII.GetString(pbytes);
                        Console.WriteLine("textbox3 {0}", textBox3.Text);
                    }
                    else
                    {
                        //savefile
                        String path = System.IO.Directory.GetCurrentDirectory();
                        System.IO.File.WriteAllBytes(System.IO.Directory.GetCurrentDirectory() + "/" + filepath + "." + extension, pbytes);
                    }
                }
                else
                if (mode.Equals("CBC"))
                {
                    //Generate IV
                    //CBC mode
                    CBC cbc = new CBC("", Encoding.ASCII.GetString(content), newKey, iv);
                    byte[] pbytes = cbc.decipher(content);
                    Console.WriteLine("hasil dekripsi: {0}", ByteArrayToString(pbytes));
                    textBox3.Text = ByteArrayToString(pbytes);

                    if (extension.Equals("txt"))
                    {
                        //show plaintext if using text extension
                        textBox3.Text += Environment.NewLine + Environment.NewLine + Encoding.ASCII.GetString(pbytes);
                        Console.WriteLine("textbox3 {0}", textBox3.Text);
                    }
                    else
                    {
                        //savefile
                        String path = System.IO.Directory.GetCurrentDirectory();
                        System.IO.File.WriteAllBytes(System.IO.Directory.GetCurrentDirectory() + "/" + filepath + "." + extension, pbytes);
                    }
                }
                else
                if (mode.Equals("CFB"))
                {
                    //CFB mode
                    CFB cfb = new CFB(null, content, keyBox.Text, iv);
                    byte[] pbytes = cfb.decrypt();
                    Console.WriteLine("hasil dekripsi: {0}", ByteArrayToString(pbytes));
                    textBox3.Text = ByteArrayToString(pbytes);

                    if (extension.Equals("txt"))
                    {
                        //show plaintext if using text extension
                        textBox3.Text += Environment.NewLine + Environment.NewLine + Encoding.ASCII.GetString(pbytes);
                        Console.WriteLine("textbox3 {0}",textBox3.Text);
                    }
                    else
                    {
                        //savefile
                        String path = System.IO.Directory.GetCurrentDirectory();
                        System.IO.File.WriteAllBytes(System.IO.Directory.GetCurrentDirectory() + "/" + filepath + "." + extension,pbytes);
                    }
                }
                else
                if (mode.Equals("OFB"))
                {
                    //OFB mode
                    OFB ofb = new OFB(null, content, keyBox.Text, iv);
                    Console.WriteLine(iv);
                    byte[] pbytes = ofb.decrypt();
                    Console.WriteLine("hasil dekripsi: {0}", ByteArrayToString(pbytes));
                    textBox3.Text = ByteArrayToString(pbytes);

                    if (extension.Equals("txt"))
                    {
                        //show plaintext if using text extension
                        textBox3.Text += Environment.NewLine + Environment.NewLine + Encoding.ASCII.GetString(pbytes);
                        Console.WriteLine("textbox3 {0}", textBox3.Text);
                    }
                    else
                    {
                        //savefile
                        String path = System.IO.Directory.GetCurrentDirectory();
                        System.IO.File.WriteAllBytes(System.IO.Directory.GetCurrentDirectory() + "/" + filepath + "." + extension, pbytes);
                    }
                }
            }
        }