private void buttonDecrypt_Click(object sender, EventArgs e) { if (busy) { return; } try { MessageHint("请选择密文文本。"); if (encryptFileDialog.ShowDialog() != DialogResult.OK) { return; } textBoxRecInputFile.Text = encryptFileDialog.FileName; MessageHint("请选择明文存储位置。"); if (saveFileDialog.ShowDialog() != DialogResult.OK) { return; } textBoxRecOutputFile.Text = saveFileDialog.FileName; if (RecKey == null) { buttonRecLoadRSAInput_Click(sender, e); if (RecEncryptedDESKey == 0) { return; } buttonRSADecrypt_Click(sender, e); if (RecKey == 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, RecKey, bw, IV); busy = true; Log("开始解密"); encryptStream.Decrypt(); Log("解密结束"); busy = false; } } MessageHint("DES解密成功。"); } catch (FileNotFoundException) { MessageError("错误:文件未找到"); MessageError("解密失败。"); } catch (IOException) { MessageError("错误:文件读写发生错误"); MessageError("解密失败。"); } }
private void buttonEncrypt_Click(object sender, EventArgs e) { if (busy) { return; } try { MessageHint("请选择明文文本。"); if (encryptFileDialog.ShowDialog() != DialogResult.OK) { return; } textBoxInputFile.Text = encryptFileDialog.FileName; MessageHint("请选择密文存储位置。"); if (saveFileDialog.ShowDialog() != DialogResult.OK) { return; } textBoxOutputFile.Text = saveFileDialog.FileName; if (Key == null) { MessageHint("密钥尚未加载,请选择密钥。"); LoadKeyButton_Click(sender, e); if (Key == null) { return; } } if (ReceiverPublicKey == null) { buttonGetRSAPublic_Click(sender, e); if (ReceiverPublicKey == null) { return; } } buttonRSAEncrypt_Click(sender, e); 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; Log("开始加密"); if (IV == null) { RandomIVButton_Click(null, null); } encryptStream.Encrypt(); Log("加密结束"); busy = false; } } MessageHint("加密成功。"); } catch (FileNotFoundException) { MessageError("错误:文件未找到"); MessageError("加密失败。"); } catch (IOException) { MessageError("错误:文件读写发生错误"); MessageError("加密失败。"); } }
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 + "失败。"); } }