Пример #1
0
        public void HillCipherTestEnc5()
        {
            HillCipher algorithm = new HillCipher();
            string     cipher    = algorithm.Encrypt(mainPlain3, mainKey3);

            Assert.IsTrue(cipher.Equals(mainCipher3, StringComparison.InvariantCultureIgnoreCase));
        }
Пример #2
0
        public void HillCipherTestNewEnc()
        {
            HillCipher algorithm = new HillCipher();
            string     cipher    = algorithm.Encrypt(newPlain, newKey);

            Assert.IsTrue(cipher.Equals(newCipher, StringComparison.InvariantCultureIgnoreCase));
        }
Пример #3
0
        private void HillEncrypt_Click(object sender, EventArgs e)
        {
            HillCipher hillcipher = new HillCipher();

            string res = hillcipher.Encrypt(HillPlain.Text, HILLKey.Text);

            HillCipher.Text = res;
        }
Пример #4
0
        private void BtnAuto_Click(object sender, EventArgs e)
        {
            Stopwatch stopwatch;
            string    fileheader = new StringBuilder().AppendLine("Key length;Hmax;H;Encryption time").ToString();

            File.WriteAllText("hill.csv", fileheader);
            File.WriteAllText("gamma.csv", fileheader);
            File.WriteAllText("1permutation.csv", fileheader);
            for (int k = 2; k <= 11; k++)
            {
                StringBuilder hcs = new StringBuilder();
                StringBuilder gcs = new StringBuilder();
                StringBuilder pcs = new StringBuilder();
                for (int j = 0; j < 10; j++)
                {
                    Gamma <int> gc = new Gamma <int>(bmp, random.Next(0, 256), k);
                    SinglePermutation <int[]> pc = new SinglePermutation <int[]>(bmp, SinglePermutation <int> .GenKey(k));
                    if (k > 100 & k % 100 == 0)
                    {
                        HillCipher <Matrix> hc = new HillCipher <Matrix>(
                            bmp,
                            HillCipher <Matrix> .GenNewKey(k / 100)[KeyType.ENCRYPT],
                            HillCipher <Matrix> .GenNewKey(k / 100)[KeyType.ENCRYPT]);
                        stopwatch = new Stopwatch();
                        stopwatch.Start();
                        hc = hc.Encrypt();
                        stopwatch.Stop();
                        hcs.AppendLine(String.Format("{0};{1};{2};{3}",
                                                     k,
                                                     hc.ImageInfo.Hmax,
                                                     hc.ImageInfo.H,
                                                     stopwatch.ElapsedMilliseconds));
                    }
                    stopwatch = new Stopwatch();
                    stopwatch.Start();
                    gc = gc.Encrypt();
                    stopwatch.Stop();
                    gcs.AppendLine(String.Format("{0};{1};{2};{3}",
                                                 k,
                                                 gc.ImageInfo.Hmax,
                                                 gc.ImageInfo.H,
                                                 stopwatch.ElapsedMilliseconds));
                    stopwatch = new Stopwatch();
                    stopwatch.Start();
                    pc = pc.Encrypt();
                    stopwatch.Stop();
                    pcs.AppendLine(String.Format("{0};{1};{2};{3}",
                                                 k,
                                                 pc.ImageInfo.Hmax,
                                                 pc.ImageInfo.H,
                                                 stopwatch.ElapsedMilliseconds));
                }
                File.AppendAllText("hill.csv", hcs.ToString());
                File.AppendAllText("gamma.csv", gcs.ToString());
                File.AppendAllText("1permutation.csv", pcs.ToString());
            }
            MessageBox.Show("Done");
        }
        public void HillCipherTestEnc2()
        {
            HillCipher algorithm = new HillCipher();

            List <int> cipher2 = algorithm.Encrypt(plain, key);

            for (int i = 0; i < cipher.Count; i++)
            {
                Assert.AreEqual(cipher[i], cipher2[i]);
            }
        }
Пример #6
0
        private void Btne_Click(object sender, EventArgs e)
        {
            if (radioHill.Checked) //hill
            {
                lastRB = radioHill;
                int rnd = random.Next(0, 10);
                if (k < 11)
                {
                    hillCipher = new HillCipher <Matrix>(cypheredImage.Image,
                                                         HillCipher <Matrix> .GetKeys(k, rnd)[KeyType.ENCRYPT],
                                                         HillCipher <Matrix> .GetKeys(k, rnd)[KeyType.DECRYPT]);
                    btnd.Enabled = true;
                }
                else
                {
                    hillCipher = new HillCipher <Matrix>(cypheredImage.Image,
                                                         HillCipher <Matrix> .GenNewKey(k)[KeyType.ENCRYPT],
                                                         HillCipher <Matrix> .GenNewKey(k)[KeyType.DECRYPT]);
                    btnd.Enabled = false;
                }
                var tmp = hillCipher.Message;
                hillCipher         = hillCipher.Encrypt();
                hillCipher.Message = tmp;

                cypheredImage = hillCipher.ImageInfo;
            }
            else if (radioPermut.Checked) //1perutation
            {
                lastRB = radioPermut;
                perm1  = new SinglePermutation <int[]>(
                    cypheredImage.Image, SinglePermutation <int> .GenKey(k));
                perm1         = perm1.Encrypt();
                cypheredImage = perm1.ImageInfo;
            }
            else if (radioGamm.Checked) //gamma
            {
                lastRB = radioGamm;
                gamma  = new Gamma <int>(cypheredImage.Image,
                                         random.Next(0, 256), k);
                var tmp = gamma.Message;
                gamma         = gamma.Encrypt();
                gamma.Message = tmp;
                cypheredImage = gamma.ImageInfo;
            }
            else
            {
                SystemSounds.Exclamation.Play();
            }
            Hcypher.Text      = "H = " + cypheredImage.H;
            pictureBox2.Image = cypheredImage.Image;
        }
Пример #7
0
        public void HillCipherTest1()
        {
            // 进行随机测试,测试加密密文和解密密文是否相等
            int        time   = 100;     // 测试次数
            Random     random = new Random();
            HillCipher cipher = new HillCipher(RandomHelper.GetHillMatrix());

            // 进行time次测试
            for (int i = 0; i < time; i++)
            {
                // 随机生成测试明文
                var plain = RandomHelper.GetCharList();
                var re    = cipher.Decrypt(cipher.Encrypt(plain));
                Assert.AreEqual(plain, re);
            }
        }
Пример #8
0
 private void button1_Click(object sender, EventArgs e)
 {
     if (comboBox1.Text.Contains("Ceaser"))
     {
         Ceaser c   = new Ceaser();
         string Res = c.Encrypt(textBox1.Text.ToString(), int.Parse(textBox3.Text.ToString()));
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("Monoalphabetic"))
     {
         Monoalphabetic c   = new Monoalphabetic();
         string         Res = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("Columnar"))
     {
         Columnar   c   = new Columnar();
         List <int> key = new List <int>();
         for (int i = 0; i < textBox3.Text.Length; i++)
         {
             key.Add(int.Parse(textBox3.Text[i].ToString()));
         }
         string Res = c.Encrypt(textBox1.Text.ToString(), key);
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("HillCipher"))
     {
         HillCipher c          = new HillCipher();
         List <int> key1       = new List <int>();
         List <int> Plaintext1 = new List <int>();
         string     Res        = "";
         List <int> ResDig     = new List <int>();
         if (char.IsDigit(textBox3.Text[0]) && char.IsDigit(textBox1.Text[0]))
         {
             for (int i = 0; i < textBox1.Text.Length; i++)
             {
                 Plaintext1.Add(int.Parse(textBox1.Text[i].ToString()));
             }
             for (int i = 0; i < textBox3.Text.Length; i++)
             {
                 key1.Add(int.Parse(textBox3.Text[i].ToString()));
             }
             ResDig        = c.Encrypt(Plaintext1, key1);
             textBox4.Text = ResDig.ToString();
         }
         else
         {
             Res           = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
             textBox4.Text = Res;
         }
     }
     else if (comboBox1.Text.Contains("PlayFair"))
     {
         PlayFair c   = new PlayFair();
         string   Res = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("RailFence"))
     {
         RailFence c   = new RailFence();
         string    Res = c.Encrypt(textBox1.Text.ToString(), int.Parse(textBox3.Text.ToString()));
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("RepeatingKeyVigenere"))
     {
         RepeatingkeyVigenere c = new RepeatingkeyVigenere();
         string Res             = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("AutokeyVigenere"))
     {
         AutokeyVigenere c   = new AutokeyVigenere();
         string          Res = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("RSA"))
     {
         RSA      c   = new RSA();
         string   s   = textBox1.Text.ToString();
         string[] str = s.Split(' ');
         int      p   = int.Parse(str[0]);
         int      q   = int.Parse(str[1]);
         int      M   = int.Parse(str[2]);
         int      ee  = int.Parse(str[3]);
         int      Res = c.Encrypt(p, q, M, ee);
         textBox4.Text = Res.ToString();
     }
     else if (comboBox1.Text.Contains("AES"))
     {
         AES    c   = new AES();
         string Res = c.Encrypt(textBox1.Text.ToString(), textBox3.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("MD5"))
     {
         MD5    c   = new MD5();
         string Res = c.GetHash(textBox1.Text.ToString());
         textBox4.Text = Res;
     }
 }