コード例 #1
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());
 }
コード例 #2
0
ファイル: Form1.cs プロジェクト: sonnythehottest/supercipher
        //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);
                    }
                }
            }
        }