コード例 #1
0
        private void buttonEncodeToFile_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Trim() == "")
            {
                MessageBox.Show(new Form()
                {
                    TopMost = true
                },
                                "Введите парольную фразу для шифрования!",
                                "Ошибка!",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show(new Form()
                {
                    TopMost = true
                },
                                "Введите текст для кодирования!",
                                "Ошибка!",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            {
                int longtoEncode;

                if (radioButton1.Checked)
                {
                    longtoEncode = 128;
                }

                else
                if (radioButton2.Checked)
                {
                    longtoEncode = 192;
                }
                else
                {
                    longtoEncode = 256;
                }

                //String userKeyword = textBox2.Text.Length < maxLength ? textBox2.Text+ new string(' ', maxLength-textBox2.Text.Length): textBox2.Text;

                byte[] userKeyBytes = Encoding.Unicode.GetBytes(textBox2.Text);
                PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(userKeyBytes, null);
                byte[] passInByte = passwordDeriveBytes.GetBytes(longtoEncode);

                RC6 rc6 = new RC6(longtoEncode, passInByte);

                byte[] codedText = rc6.EncodeRc6(textBox1.Text);

                //textBox2.Text=Encoding.UTF8.GetString(rc6.DecodeRc6(codedText));


                #region codeToFile
                SaveFileDialog s = new SaveFileDialog();
                s.CheckPathExists = true;
                s.SupportMultiDottedExtensions = true;
                s.Title = "Выберите файл для сохранения кодированного текста";
                if (s.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }

                File.WriteAllBytes(s.FileName, codedText);
                #endregion
            }
        }
コード例 #2
0
        private void buttonDecode_Click(object sender, EventArgs e)
        {
            string inputPath  = textBoxInput.Text;
            string outputPath = textBoxOutput.Text;

            if (textBox1.Text.Trim() == "")
            {
                MessageBox.Show(new Form()
                {
                    TopMost = true
                },
                                "Введите парольную фразу для шифрования!",
                                "Ошибка!",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            if (!File.Exists(inputPath))
            {
                MessageBox.Show(new Form()
                {
                    TopMost = true
                },
                                "Данного входного файла не существует!",
                                "Ошибка!",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            if (outputPath == "")
            {
                MessageBox.Show(new Form()
                {
                    TopMost = true
                },
                                "Отсутствует файл для вывода!",
                                "Ошибка",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            {
                int longtoEncode;

                if (radioButton1.Checked)
                {
                    longtoEncode = 128;
                }

                else
                if (radioButton2.Checked)
                {
                    longtoEncode = 192;
                }
                else
                {
                    longtoEncode = 256;
                }

                byte[] userKeyBytes = Encoding.Unicode.GetBytes(textBox1.Text);
                PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(userKeyBytes, null);
                byte[] passInByte = passwordDeriveBytes.GetBytes(longtoEncode);

                #region decodeFile

                try {
                    RC6 rc6 = new RC6(longtoEncode, passInByte);

                    byte[] inputBytes  = File.ReadAllBytes(inputPath);
                    byte[] decodedText = rc6.DecodeRc6(inputBytes);

                    File.WriteAllBytes(outputPath, decodedText);
                }
                catch (Exception)
                {
                    MessageBox.Show(new Form()
                    {
                        TopMost = true
                    },
                                    "Был выбран некорректный файл для расшифровки!",
                                    "Ошибка",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }


                #endregion
            }
        }
コード例 #3
0
        private void buttonDecodeFromFile_Click(object sender, EventArgs e)
        {
            if (textBox2.Text.Trim() == "")
            {
                MessageBox.Show(new Form()
                {
                    TopMost = true
                },
                                "Введите парольную фразу для расшифрования!",
                                "Ошибка!",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            if (!radioButton1.Checked && !radioButton2.Checked && !radioButton3.Checked)
            {
                MessageBox.Show(new Form()
                {
                    TopMost = true
                },
                                "Выберите длину ключа!",
                                "Ошибка!",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
            }
            else
            {
                int longtoEncode;


                if (radioButton1.Checked)
                {
                    longtoEncode = 128;
                }

                else
                if (radioButton2.Checked)
                {
                    longtoEncode = 192;
                }
                else
                {
                    longtoEncode = 256;
                }

                #region FileToCode

                OpenFileDialog o = new OpenFileDialog();
                o.CheckFileExists = true;
                o.CheckPathExists = true;
                o.Multiselect     = false;
                o.SupportMultiDottedExtensions = true;
                o.Title = "Выберите файл с текстом для расшифрования";
                if (o.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }

                //String userKeyword = textBox2.Text.Length < maxLength ? textBox2.Text + new string(' ', maxLength - textBox2.Text.Length) : textBox2.Text;

                byte[] userKeyBytes = Encoding.Unicode.GetBytes(textBox2.Text);
                PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(userKeyBytes, null);
                byte[] passInByte = passwordDeriveBytes.GetBytes(longtoEncode);

                try
                {
                    RC6 rc6 = new RC6(longtoEncode, passInByte);


                    byte[] fromFile = File.ReadAllBytes(o.FileName);

                    textBox1.Text = Encoding.UTF8.GetString(rc6.DecodeRc6(fromFile));
                }
                catch (Exception)
                {
                    MessageBox.Show(new Form()
                    {
                        TopMost = true
                    },
                                    "Был выбран некорректный файл для расшифровки!",
                                    "Ошибка",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
                #endregion
            }
        }