Ejemplo 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);
        }
Ejemplo n.º 2
0
        public void CipherHexTest()
        {
            string M    = "0123456789ABCDEF";
            string key  = "133457799BBCDFF1";
            var    test = new DESClass();

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

            Assert.AreEqual("85E813540F0AB405", test.Ciphertext, true);
        }
Ejemplo n.º 3
0
        private void cipherButton_Click(object sender, RoutedEventArgs e)
        {
            string plaintext  = messagebox.Text;
            string key        = keybox.Text;
            string ciphertext = 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 < plaintext.Length; i += 16)
                {
                    if (plaintext.Length - i > 16)
                    {
                        send = plaintext.Substring(i, 16);
                    }
                    else
                    {
                        send = plaintext.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()
                    {
                        Plaintext = send, Key = key
                    };
                    des.CipherHex(round);
                    works      += string.Format("{0}{1}{1}", des.Work, Environment.NewLine);
                    ciphertext += des.Ciphertext;
                }
            }
            else
            {
                for (int i = 0; i < plaintext.Length; i += 8)
                {
                    if (plaintext.Length - i > 8)
                    {
                        send = plaintext.Substring(i, 8);
                    }
                    else
                    {
                        send = plaintext.Substring(i, plaintext.Length - i);
                    }
                    if (send.Length != 8)
                    {
                        int missing = 8 - send.Length;
                        for (int j = 0; j < missing; j++)
                        {
                            send += "0";
                        }
                    }
                    works += string.Format("Part {0}{1}", i, Environment.NewLine);
                    var des = new DESClass()
                    {
                        Plaintext = send, Key = key
                    };
                    des.CipherString(round);
                    works      += string.Format("{0}{1}{1}", des.Work, Environment.NewLine);
                    ciphertext += des.Ciphertext;
                }
            }
            workText.Text = string.Format("{0}{1} CipherBytes: {2}", works, Environment.NewLine, ciphertext);
        }