Beispiel #1
0
        public void HillCipherError4()
        {
            HillCipher algorithm = new HillCipher();
            string     key       = algorithm.Analyse3By3Key(mainPlain, cipherS3);

            Assert.IsTrue(key.Equals(keyS3, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #2
0
        public void HillCipherTestNew2By2Analysis()
        {
            HillCipher algorithm = new HillCipher();
            string     key       = algorithm.Analyse(newPlain, newCipher);

            Assert.IsTrue(key.Equals(newKey, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #3
0
        public void HillCipherTestNewEnc()
        {
            HillCipher algorithm = new HillCipher();
            string     cipher    = algorithm.Encrypt(newPlain, newKey);

            Assert.IsTrue(cipher.Equals(newCipher, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #4
0
        public void HillCipherTestDec5()
        {
            HillCipher algorithm = new HillCipher();
            string     plain     = algorithm.Decrypt(mainCipher3, mainKey3);

            Assert.IsTrue(plain.Equals(mainPlain3, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #5
0
        public void HillCipherTest3By3Analysis1()
        {
            HillCipher algorithm = new HillCipher();
            string     key       = algorithm.Analyse3By3Key(mainPlain3, mainCipher3);

            Assert.IsTrue(key.Equals(mainKey3, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #6
0
        public void HillCipherTestNewDec()
        {
            HillCipher algorithm = new HillCipher();
            string     plain     = algorithm.Decrypt(newCipher, newKey);

            Assert.IsTrue(plain.Equals(newPlain, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #7
0
        public void HillCipherTestEnc3()
        {
            HillCipher algorithm = new HillCipher();
            string     cipher    = algorithm.Encrypt(mainPlain, keyS3);

            Assert.IsTrue(cipher.Equals(cipherS3, StringComparison.InvariantCultureIgnoreCase));
        }
Beispiel #8
0
        private void HillAnalays_Click(object sender, EventArgs e)
        {
            HillCipher hillcipher = new HillCipher();

            string res = hillcipher.Analyse(HillPlain.Text, HillCipher.Text);

            HILLKey.Text = res;
        }
Beispiel #9
0
        private void HillDecrypt_Click(object sender, EventArgs e)
        {
            HillCipher hillcipher = new HillCipher();

            string res = hillcipher.Decrypt(HillCipher.Text, HILLKey.Text);

            HillPlain.Text = res;
        }
Beispiel #10
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 HillCipherTest2By2Analysis2()
        {
            HillCipher algorithm = new HillCipher();

            List <int> key2 = algorithm.Analyse(plain, cipher);

            for (int i = 0; i < key.Count; i++)
            {
                Assert.AreEqual(key[i], key2[i]);
            }
        }
        public void HillCipherTest3By3Analysis2()
        {
            HillCipher algorithm = new HillCipher();

            List <int> key2 = algorithm.Analyse3By3Key(plain4, cipher4);

            for (int i = 0; i < key4.Count; i++)
            {
                Assert.AreEqual(key4[i], key2[i]);
            }
        }
        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]);
            }
        }
        public void HillCipherTestDec2()
        {
            HillCipher algorithm = new HillCipher();

            List <int> plain2 = algorithm.Decrypt(cipher, key);

            for (int i = 0; i < plain.Count; i++)
            {
                Assert.AreEqual(plain[i], plain2[i]);
            }
        }
Beispiel #15
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;
        }
Beispiel #16
0
        static void Main(string[] args)
        {
            HillCipher hill = new HillCipher(26);

            Console.WriteLine("Write message, please");
            var encodedMessage = hill.Encode(Console.ReadLine());

            Console.WriteLine($"Key: {hill.GetKey}");
            Console.WriteLine($"Encoded message:\n{encodedMessage}");
            var decodedMessage = hill.Decode(encodedMessage);

            Console.WriteLine($"Decoded message:\n{decodedMessage}");
            Console.ReadKey();
        }
Beispiel #17
0
        public void Encode_MessageInUkrainianAndKey_ReturnsMessageEncodedForUkrainianAlphabetAndProvidedKey(string message, string expectedEncodedMessage, object[] key)
        {
            var flattenedKey = (int[])key[0];

            int[,] keyTransformed = new int[2, 2] {
                { flattenedKey[0], flattenedKey[1] },
                { flattenedKey[2], flattenedKey[3] }
            };
            HillCipher encoder =
                new HillCipher(keyTransformed, new UkrainianAlphabet());

            string encodedMessage = encoder.Encode(message);

            Assert.Equal(expectedEncodedMessage, encodedMessage);
        }
Beispiel #18
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);
            }
        }
Beispiel #19
0
 private void Btnd_Click(object sender, EventArgs e)
 {
     if (lastRB == radioHill) //hill
     {
         hillCipher    = hillCipher.Decrypt();
         cypheredImage = hillCipher.ImageInfo;
     }
     else if (lastRB == radioPermut) //1perutation
     {
         perm1         = perm1.Decrypt();
         cypheredImage = perm1.ImageInfo;
     }
     else if (lastRB == radioGamm)//gamma
     {
         gamma         = gamma.Decrypt();
         cypheredImage = gamma.ImageInfo;
     }
     else
     {
         SystemSounds.Exclamation.Play();
     }
     Hcypher.Text      = "H = " + cypheredImage.H;
     pictureBox2.Image = cypheredImage.Image;
 }
Beispiel #20
0
 private void button2_Click(object sender, EventArgs e)
 {
     if (comboBox1.Text.Contains("Ceaser"))
     {
         Ceaser c   = new Ceaser();
         int    Res = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res.ToString();
     }
     else if (comboBox1.Text.Contains("Monoalphabetic"))
     {
         Monoalphabetic c   = new Monoalphabetic();
         string         Res = c.Analyse(textBox1.Text.ToString(), textBox2.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()));
         }
         List <int> Res = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res.ToString();
     }
     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 (textBox5.Text == "2")
         {
             if (char.IsDigit(textBox1.Text[0]) && char.IsDigit(textBox2.Text[0]))
             {
                 for (int i = 0; i < textBox1.Text.Length; i++)
                 {
                     Plaintext1.Add(int.Parse(textBox1.Text[i].ToString()));
                 }
                 for (int i = 0; i < textBox2.Text.Length; i++)
                 {
                     key1.Add(int.Parse(textBox2.Text[i].ToString()));
                 }
                 ResDig        = c.Analyse(Plaintext1, key1);
                 textBox4.Text = ResDig.ToString();
             }
             else
             {
                 Res           = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
                 textBox4.Text = Res;
             }
         }
         else if (textBox5.Text == "3")
         {
             if (char.IsDigit(textBox1.Text[0]) && char.IsDigit(textBox2.Text[0]))
             {
                 for (int i = 0; i < textBox1.Text.Length; i++)
                 {
                     Plaintext1.Add(int.Parse(textBox1.Text[i].ToString()));
                 }
                 for (int i = 0; i < textBox2.Text.Length; i++)
                 {
                     key1.Add(int.Parse(textBox2.Text[i].ToString()));
                 }
                 ResDig        = c.Analyse3By3Key(Plaintext1, key1);
                 textBox4.Text = ResDig.ToString();
             }
             else
             {
                 Res           = c.Analyse3By3Key(textBox1.Text.ToString(), textBox2.Text.ToString());
                 textBox4.Text = Res;
             }
         }
     }
     else if (comboBox1.Text.Contains("RailFence"))
     {
         RailFence c   = new RailFence();
         int       Res = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res.ToString();
     }
     else if (comboBox1.Text.Contains("RepeatingKeyVigenere"))
     {
         RepeatingkeyVigenere c = new RepeatingkeyVigenere();
         string Res             = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res;
     }
     else if (comboBox1.Text.Contains("AutokeyVigenere"))
     {
         AutokeyVigenere c   = new AutokeyVigenere();
         string          Res = c.Analyse(textBox1.Text.ToString(), textBox2.Text.ToString());
         textBox4.Text = Res;
     }
 }
        public void HillCipherError3()
        {
            HillCipher algorithm = new HillCipher();

            List <int> key2 = algorithm.Decrypt(plain, keyError);
        }
        public void HillCipherError2()
        {
            HillCipher algorithm = new HillCipher();

            List <int> key2 = algorithm.Analyse(mainPlainError2, mainCipherError2);
        }
Beispiel #23
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;
     }
 }
Beispiel #24
0
        public void HillCipherError1()
        {
            HillCipher algorithm = new HillCipher();

            string key2 = algorithm.Analyse(mainPlainError, mainCipherError);
        }