示例#1
0
 private void RandomIVButton_Click(object sender, EventArgs e)
 {
     Log("生成随机初始向量:IV(64 bit)");
     IV          = Bitset.RandomGenerate(64);
     IVText.Text = IV.ToString();
 }
示例#2
0
        private void EncryptDecryptUserGuide(bool isDecrypt)
        {
            if (busy)
            {
                return;
            }
            string sourceType    = isDecrypt ? "密文" : "明文";
            string targetType    = isDecrypt ? "明文" : "密文";
            string operationType = isDecrypt ? "解密" : "加密";

            try
            {
                MessageBox.Show("请选择" + sourceType + "文本。");
                if (encryptFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                textBoxInputFile.Text = encryptFileDialog.FileName;
                MessageBox.Show("请选择" + targetType + "存储位置。");
                if (saveFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
                textBoxOutputFile.Text = saveFileDialog.FileName;
                if (Key == null)
                {
                    MessageBox.Show("密钥尚未加载,请选择密钥。");
                    button2_Click(null, null);
                    if (Key == null)
                    {
                        return;
                    }
                }
                using (BinaryReader br = new BinaryReader(new FileStream(encryptFileDialog.FileName, FileMode.Open, FileAccess.Read)))
                {
                    using (BinaryWriter bw = new BinaryWriter(new FileStream(saveFileDialog.FileName, FileMode.Create, FileAccess.Write)))
                    {
                        DESStream encryptStream = new DESStream(br, Key, bw, IV);
                        busy = true;
                        if (isDecrypt)
                        {
                            Log("开始解密");
                            encryptStream.Decrypt();
                            IV          = encryptStream.IV;
                            IVText.Text = IV.ToString();
                            Log("解密结束");
                        }
                        else
                        {
                            Log("开始加密");
                            if (IV == null)
                            {
                                RandomIVButton_Click(null, null);
                            }
                            encryptStream.Encrypt();
                            Log("加密结束");
                        }
                        busy = false;
                    }
                }
                MessageBox.Show(operationType + "成功。");
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("错误:文件未找到");
                MessageBox.Show(operationType + "失败。");
            }
            catch (IOException)
            {
                MessageBox.Show("错误:文件读写发生错误");
                MessageBox.Show(operationType + "失败。");
            }
        }