예제 #1
0
        static void Main(string[] args)
        {
            var des = new DESEncryption();
            // Create a 56 bit key for the DES to use to encrypt and decrypt
            var key = des.GenerateRandomNumbers(8);
            // Create a initialization vector to prevent repetition of block chipers
            var iv = des.GenerateRandomNumbers(8);

            const string message = "String to encrypt";

            Console.WriteLine("DES Encryption / Decryption Demonstartion");
            Console.WriteLine("-----------------------------------------");

            var encryptedMessage = des.Encrypt(Encoding.UTF8.GetBytes(message), key, iv);
            var decryptedMessage = des.Decrypt(encryptedMessage, key, iv);

            Console.WriteLine();
            Console.WriteLine($" original message  : {message}");
            Console.WriteLine($" encrypted message : {Convert.ToBase64String(encryptedMessage)}");
            Console.WriteLine($" decrypted message : {Encoding.UTF8.GetString(decryptedMessage)}");

            Console.WriteLine();
        }
예제 #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            //byte[] iv = new byte[] { 0xCC, 0x6F, 0x4F, 0xA9, 0x7E, 0xD7, 0xB0, 0x62 };
            byte[] iv      = StringFromHex(textBox1.Text.Replace(" ", ""));
            byte[] key     = StringFromHex(textBox1.Text.Replace(" ", ""));
            byte[] Setdata = StringFromHex(richTextBox1.Text.Replace(" ", ""));

            try
            {
                if (Setdata.Length > 0)
                {
                    try
                    {
                        if (iv.Length > 0)
                        {
                            byte[] data      = new byte[0];
                            byte[] Decpacket = new byte[0];
                            byte[] Header    = new byte[16];
                            int    size      = 0;

                            Array.Resize(ref Decpacket, Setdata.Length - 10);//Chave de Autentitaçao
                            Array.Copy(Setdata, 0, Decpacket, 0, Setdata.Length - 10);

                            size = Decpacket.Length;
                            byte[] SetDecpacket = new byte[0];

                            Array.Resize(ref SetDecpacket, Decpacket.Length + 8);//Conteudo
                            Array.Copy(Decpacket, 0, SetDecpacket, 8, size);

                            byte[] descrypt = DESEncryption.DecryptData(SetDecpacket, iv, key);

                            string packetDescrypt  = BitConverter.ToString(descrypt).Replace("-", " ");
                            byte[] finalize        = StringFromHex(packetDescrypt.Substring(72));
                            string packetDescrypt2 = BitConverter.ToString(finalize);

                            richTextBox2.Text = BitConverter.ToString(finalize).Replace("-", " ");
                            richTextBox3.Text = Encoding.ASCII.GetString(finalize);

                            if (isVetor == true)
                            {
                                byte[] readVetor = new byte[StringFromHex(packetDescrypt.Substring(72)).Length];
                                byte[] newVetor  = new byte[8];
                                readVetor = StringFromHex(packetDescrypt.Substring(72));
                                Array.Copy(readVetor, 25, newVetor, 0, 8);
                                textBox1.Text = BitConverter.ToString(newVetor).Replace("-", " ");
                            }

                            getId(finalize);
                            getCompress(finalize[6]);
                            getLen(finalize);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }
                else
                {
                    Clear();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, ex.Source);
            }
            //richTextBox2.Text = BitConverter.ToString(DESEncryption.EncryptData(data,iv,key)).Replace("-"," ");
        }