Exemplo n.º 1
0
        public void AllAroundHexTest()
        {
            string M   = "";
            string key = "";

            char[] possible = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
            Random random   = new Random();

            int posicion;

            for (var j = 0; j < 16; j++)
            {
                posicion = random.Next(16);
                M       += possible[posicion];
                posicion = random.Next(16);
                key     += possible[posicion];
            }
            var cipher = new DESClass();

            cipher.Plaintext = M;
            cipher.Key       = key;
            cipher.CipherHex(16);

            var decipher = new DESClass();

            decipher.Ciphertext = cipher.Ciphertext;
            decipher.Key        = key;
            decipher.DecipherHex(16);

            Assert.AreEqual(M, decipher.Plaintext, true);
        }
Exemplo n.º 2
0
        public void DecipherHexTest()
        {
            string M    = "85E813540F0AB405";
            string key  = "133457799BBCDFF1";
            var    test = new DESClass();

            test.Ciphertext = M;
            test.Key        = key;
            test.DecipherHex(16);

            Assert.AreEqual("0123456789ABCDEF", test.Plaintext, true);
        }
Exemplo n.º 3
0
        private void decipherButton_Click(object sender, RoutedEventArgs e)
        {
            string ciphertext = messagebox.Text;
            string key        = keybox.Text;
            string plaintext  = string.Empty;
            string works      = string.Empty;
            string send       = string.Empty;
            bool   hex        = (bool)hexRadio.IsChecked;
            int    round      = roundBox.SelectedIndex + 1;

            if (hex)
            {
                for (int i = 0; i < ciphertext.Length; i += 16)
                {
                    if (ciphertext.Length - i > 16)
                    {
                        send = ciphertext.Substring(i, 16);
                    }
                    else
                    {
                        send = ciphertext.Substring(i, plaintext.Length - i);
                    }
                    if (send.Length != 16)
                    {
                        int missing = 16 - send.Length;
                        for (int j = 0; j < missing; j++)
                        {
                            send += "0";
                        }
                    }
                    works += string.Format("Part {0}{1}", i, Environment.NewLine);
                    var des = new DESClass()
                    {
                        Ciphertext = send, Key = key
                    };
                    des.DecipherHex(round);
                    works     += string.Format("{0}{1}{1}", des.Work, Environment.NewLine);
                    plaintext += des.Plaintext;
                }
            }
            else
            {
                for (int i = 0; i < ciphertext.Length; i += 16)
                {
                    if (ciphertext.Length - i > 16)
                    {
                        send = ciphertext.Substring(i, 16);
                    }
                    else
                    {
                        send = ciphertext.Substring(i, ciphertext.Length - i);
                    }
                    if (send.Length != 16)
                    {
                        int missing = 16 - send.Length;
                        for (int j = 0; j < missing; j++)
                        {
                            send += "0";
                        }
                    }
                    works += string.Format("Part {0}{1}", i, Environment.NewLine);
                    var des = new DESClass()
                    {
                        Ciphertext = send, Key = key
                    };
                    des.DecipherString(round);
                    works     += string.Format("{0}{1}{1}", des.Work, Environment.NewLine);
                    plaintext += des.Plaintext;
                }
            }
            workText.Text = string.Format("{0}{1} PlainBytes: {2}", works, Environment.NewLine, plaintext);
        }