Beispiel #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;
        }
Beispiel #2
0
 public void StringTest2()
 {
     //
     // TODO: Add test logic here
     //
     byte[] plain = Encoding.ASCII.GetBytes("kripto.grafi");
     CFB cfb = new CFB(plain, null, "1234567890123", "0987654321098");
     cfb.encrypt();
     CollectionAssert.AreEqual(plain, cfb.decrypt());
 }