Пример #1
0
        private void start_button_Click(object sender, RoutedEventArgs e)
        {
            DES    des    = new DES();
            Blocks blocks = new Blocks();

            bool encryptOrDecrypt = false;

            string filepath = Filepath.Text;
            string keyValue = keyword.Text;
            string currentDir = "";
            string currentName = "";
            string workGroup_1 = $@"{currentDir}\{currentName}";
            string workGroup_2 = "";
            string workGroup_3 = "";
            string endpointText = "";
            string message, title, defaultValue;
            object fileName;

            int mLength        = 0;
            int zeroBits       = 0;
            int length         = 0;
            int count          = 0;
            int ammoutOfRounds = 16;

            try
            {
                currentDir  = Path.GetDirectoryName(filepath);
                currentName = Path.GetFileName(filepath);
            }
            catch (Exception)
            {
                MessageBox.Show("No such file!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }

            byte[] binaryBits   = blocks.GetBinaryBits(filepath);
            byte[] key          = blocks.GetKey(keyValue);
            byte[] currentBlock = new byte[64];
            byte[,] blockText  = blocks.DoBlocks(binaryBits, out mLength, out zeroBits);
            byte[,] addedBlock = blocks.AddBlcok(blockText, zeroBits, mLength);
            byte[] endpointBlock;
            byte[] correctedMatrix;
            byte[] readyBytes;



            if (radiobutton_encrypt.IsChecked == true)
            {
                encryptOrDecrypt = true;
                length           = mLength + 1;
            }
            else if (radiobutton_decrypt.IsChecked == true)
            {
                encryptOrDecrypt = false;
                length           = mLength;
            }
            for (int i = 0; i < length; i++)
            {
                for (int j = 0; j < 64; j++)
                {
                    if (radiobutton_encrypt.IsChecked == true)
                    {
                        currentBlock[j] = addedBlock[i, count];
                    }
                    else if (radiobutton_decrypt.IsChecked == true)
                    {
                        currentBlock[j] = blockText[i, count];
                    }
                    count++;
                }
                count = 0;
                des.Rounds(ammoutOfRounds, key, currentBlock, encryptOrDecrypt, out endpointBlock);
                foreach (byte t in endpointBlock)
                {
                    endpointText += t.ToString();
                }
            }
            if (radiobutton_decrypt.IsChecked == true)
            {
                try
                {
                    correctedMatrix = blocks.DoCorrection(endpointText);
                    endpointText    = "";
                    foreach (byte t in correctedMatrix)
                    {
                        endpointText += t.ToString();
                    }
                }
                catch (Exception)
                {
                    Interaction.MsgBox("Wrong key word!!!", MsgBoxStyle.OkOnly | MsgBoxStyle.Information, "Error");
                    return;
                }
            }
            readyBytes = blocks.ReverseByte(endpointText);

            if (radiobutton_encrypt.IsChecked == true)
            {
                message      = "Please input filename:";
                title        = "DES encryption";
                defaultValue = "Encrypted message.txt";
                fileName     = Interaction.InputBox(message, title, defaultValue);
                workGroup_2  = $@"{currentDir}\{(string)fileName}";
                if ((string)fileName != "")
                {
                    File.WriteAllBytes(workGroup_2, readyBytes);
                }
                MessageBox.Show($"File has been encrypted successfully!\nFilepath:{workGroup_2}", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else if (radiobutton_decrypt.IsChecked == true)
            {
                message      = "Please input new filename:";
                title        = "DES encryption";
                defaultValue = "Decrypted message.txt";
                fileName     = Interaction.InputBox(message, title, defaultValue);
                workGroup_3  = $@"{currentDir}\{(string)fileName}";
                if ((string)fileName != "")
                {
                    File.WriteAllBytes(workGroup_3, readyBytes);
                }
                MessageBox.Show($"File has been decrypted successfully!\nFilepath:{workGroup_3}", "Success!", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }