Ejemplo n.º 1
0
        public byte[] Decode(byte[] input)
        {
            var output     = new List <byte>();
            var countBlock = Math.Ceiling((double)input.Length / _sizeBlock);

            for (var i = 0; i < countBlock; i++)
            {
                var cipherRegistr = _aes.Encrypt(_shiftRegistr.ToArray());

                for (var j = 0; j < _sizeBlock; j++)
                {
                    var index = i * _sizeBlock + j;
                    if (index >= input.Length)
                    {
                        break;
                    }

                    var currentByte = cipherRegistr[j];
                    output.Add((byte)(input[index] ^ currentByte));
                    _shiftRegistr.RemoveAt(0);
                    _shiftRegistr.Add(currentByte);
                }
            }
            return(output.ToArray());
        }
Ejemplo n.º 2
0
        private void ButtonExecute_Click(object sender, RoutedEventArgs e)
        {
            Encrypted.Text = "";
            Info.Text      = "";

            int keySize = 16;

            if (Key.Text == "")
            {
                Key.Text = "Key can`t be empty!!!";
                return;
            }

            if ((bool)Aes128.IsChecked)
            {
                Aes.setNk(4);
            }
            else if ((bool)Aes192.IsChecked)
            {
                Aes.setNk(6);
                keySize = 24;
            }
            else if ((bool)Aes256.IsChecked)
            {
                Aes.setNk(8);
                keySize = 32;
            }

            byte[] key = CreateKey(Key.Text, keySize);

            //-------------------------------------------------------------------

            byte[] buff = new byte[uncrypted.Length];

            char[] c = uncrypted.ToArray();

            for (int i = 0; i < c.Count(); i++)
            {
                buff[i] = Convert.ToByte(c[i]);
            }

            if ((bool)REncrypt.IsChecked)
            {
                if ((bool)Report.IsChecked)
                {
                    WaitWindow ww = new WaitWindow();
                    ww.Show();
                    encrypted = Aes.Encrypt(buff, key, ref res);
                    Info.Text = res;
                    ww.Close();
                }
                else
                {
                    encrypted = Aes.Encrypt(buff, key);
                }

                //Encrypted field
                char[] buff2  = new char[encrypted.Length];
                string bufEnc = "";

                for (int i = 0; i < encrypted.Count(); i++)
                {
                    char ch = Convert.ToChar(encrypted[i]);
                    bufEnc += Convert.ToString(ch);
                }

                Encrypted.Text += bufEnc;

                if ((bool)Save.IsChecked)
                {
                    String path = Path.Text;
                    path += ".enc";
                    //---------------------------------write in file--------------------------------
                    try
                    {
                        using (FileStream fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
                        {
                            fs.Write(encrypted, 0, encrypted.Count());
                            MessageBox.Show("Write success \n" + path);
                        }
                    }
                    catch (SystemException)
                    {
                        throw new SystemException();
                    }
                }
            }
            else if ((bool)RDecrypt.IsChecked)
            {
                try
                {
                    using (FileStream fs = File.Open(path, FileMode.Open))
                    {
                        int    snipped = 0;
                        byte[] data    = new byte[102400];
                        int    len     = fs.Read(data, 0, data.Length);


                        // ------copy needed info from buffer----
                        byte[] temp = new byte[len];
                        for (int i = 0; i < len; i++)
                        {
                            temp[i] = data[i];
                        }
                        //----------------------------------------


                        //decrypton
                        byte[] decrypted = Aes.Decrypt(temp, key);

                        // find last block
                        if (data[len - 16] == 0x1)
                        {
                            snipped = decrypted[decrypted.Count() - 17];
                            if (snipped > 16)
                            {
                                snipped = 16;
                            }
                        }
                        else
                        {
                            snipped = 0;
                        }
                        //---------------------------------------


                        char[] dataChar = new char[(decrypted.Count() - 32) + snipped];

                        for (int i = 0; i < dataChar.Count(); i++)
                        {
                            dataChar[i] = Convert.ToChar(decrypted[i]);
                        }

                        uncrypted = new String(dataChar);

                        Source.Text  = "File Size: " + uncrypted.Length.ToString() + "\n";
                        Source.Text += uncrypted;

                        //output encoded
                    }
                }
                catch (FileNotFoundException)
                {
                    Path.Text = "File not found!";
                }
                catch (ArgumentException)
                {
                    Path.Text = "Path is empty!";
                }
                catch (DirectoryNotFoundException)
                {
                    Path.Text = "Directory not found!";
                }
            }
        }