📄 frmcrypto.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.IO;
namespace LingangTools
{
public partial class FrmCrypto : Form
{
byte[] Key, IV;
private bool isTextContent = true;
public FrmCrypto()
{
InitializeComponent();
}
private void txt_GetKeyAndIv_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txt_Password.Text))
{
MD5 md5 = MD5.Create();
Key = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(txt_Password.Text));
string textTransfered = txt_Password.Text + "lingang";
IV = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(textTransfered));
System.Text.StringBuilder sb = new StringBuilder();
for (int i = 0; i < Key.Length; i++)
{
sb.Append(Key[i]);
if (i < Key.Length - 1)
sb.Append(",");
}
txt_Key.Text = sb.ToString();
txt_KeyBase64.Text = System.Convert.ToBase64String(Key);
sb = new StringBuilder();
for (int i = 0; i < IV.Length; i++)
{
sb.Append(IV[i]);
if (i < IV.Length - 1)
sb.Append(",");
}
txt_Iv.Text = sb.ToString();
txt_IvBase64.Text = System.Convert.ToBase64String(IV);
btn_Crypto.Enabled = true;
}
else
{
MessageBox.Show("初始密码不能为空,请输入密钥后再进行加密或生成密钥操作", "不能为空");
}
}
private void btn_Crypto_Click(object sender, EventArgs e)
{
try
{
if (!string.IsNullOrEmpty(txt_PlainText.Text))
{
Rijndael sa = Rijndael.Create();
sa.Key = Key;
sa.IV = IV;
if (isTextContent)
{
byte[] plainBytes = System.Text.Encoding.Default.GetBytes(txt_PlainText.Text);
System.IO.MemoryStream msOutput = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(msOutput, sa.CreateEncryptor(), CryptoStreamMode.Write);
byte[] cryptoBytes;
try
{
cs.Write(plainBytes, 0, plainBytes.Length);
cs.Close();//必须关闭后才能读取
cryptoBytes = msOutput.ToArray();
System.Text.StringBuilder sb = new StringBuilder();
for (int i = 0; i < cryptoBytes.Length; i++)
{
sb.Append(cryptoBytes[i]);
if (i < cryptoBytes.Length - 1)
sb.Append(",");
}
txt_BinaryCryptoText.Text = sb.ToString();
txt_Base64CryptoText.Text = System.Convert.ToBase64String(cryptoBytes);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
msOutput.Close();
}
}
else
{
if (System.IO.File.Exists(txt_PlainText.Text))
{
FileStream fsInput = new FileStream(txt_PlainText.Text, FileMode.Open);
BinaryReader brInput = new BinaryReader(fsInput);
string cryptoFileName = System.IO.Path.GetFileNameWithoutExtension(txt_PlainText.Text) + ".enc";
string cryptoFullFileName = System.IO.Path.GetDirectoryName(txt_PlainText.Text) + "\\" + cryptoFileName;
FileStream fsOutput = new FileStream(cryptoFullFileName, FileMode.Create);
CryptoStream cs = new CryptoStream(fsOutput, sa.CreateEncryptor(), CryptoStreamMode.Write);
BinaryWriter brOuput = new BinaryWriter(cs);
try
{
if (fsInput.Length < int.MaxValue)
{
brOuput.Write(brInput.ReadBytes((int)fsInput.Length));
}
else
{
long segment = fsInput.Length % int.MaxValue;
long segmentLastLength = fsInput.Length - segment * int.MaxValue;
for (long i = 0; i < segment; i++)
{
brOuput.Write(brInput.ReadBytes(int.MaxValue));
}
brOuput.Write(brInput.ReadBytes((int)segmentLastLength));
}
MessageBox.Show(string.Format("文件已经保存为:{0}", cryptoFileName), "加密成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
brOuput.Close();
cs.Close();
fsOutput.Close();
brInput.Close();
fsInput.Close();
}
}
else
{
MessageBox.Show("所选的文件不存在!", "文件不存在");
}
}
}
else
{
MessageBox.Show("没有需要加密的内容,请输入内容或选择文件后,再进行加密操作!", "加密内容为空");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btn_CopyAsCode_Click(object sender, EventArgs e)
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append("byte [] Key ={").Append(txt_Key.Text).Append("};").Append(System.Environment.NewLine);
sb.Append("byte [] IV ={").Append(txt_Iv.Text).Append("};").Append(System.Environment.NewLine);
sb.Append("System.Security.Cryptography.Rijndael sa = System.Security.Cryptography.Rijndael.Create();").Append(System.Environment.NewLine);
sb.Append("sa.Key = Key;").Append(System.Environment.NewLine);
sb.Append("sa.IV = IV;").Append(System.Environment.NewLine);
sb.Append("System.IO.MemoryStream ms = new System.IO.MemoryStream();").Append(System.Environment.NewLine);
sb.Append("System.Security.Cryptography.CryptoStream cs = new System.Security.Cryptography.CryptoStream(ms, sa.CreateEncryptor(), CryptoStreamMode.Write);").Append(System.Environment.NewLine);
sb.Append("byte [] plainBytes = new byte[]{11,11,11};").Append(System.Environment.NewLine);
sb.Append("byte [] cryptoBytes;").Append(System.Environment.NewLine);
sb.Append("try").Append(System.Environment.NewLine); ;
sb.Append("{").Append(System.Environment.NewLine);
sb.Append(" cs.Write(plainBytes, 0, plainBytes.Length);").Append(System.Environment.NewLine);
sb.Append(" cs.Close();//必须关闭后才能读取").Append(System.Environment.NewLine);
sb.Append(" cryptoBytes = ms.ToArray();").Append(System.Environment.NewLine);
sb.Append("}").Append(System.Environment.NewLine);
sb.Append("catch (Exception ex)").Append(System.Environment.NewLine);
sb.Append("{").Append(System.Environment.NewLine);
sb.Append(" throw ex;").Append(System.Environment.NewLine);
sb.Append("}").Append(System.Environment.NewLine);
sb.Append("finally").Append(System.Environment.NewLine);
sb.Append("{").Append(System.Environment.NewLine);
sb.Append(" ms.Close();").Append(System.Environment.NewLine);
sb.Append("}");
FrmTextBox frm = new FrmTextBox();
frm.MyText = sb.ToString();
frm.ShowDialog();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void txt_Base64CryptoText_DoubleClick(object sender, EventArgs e)
{
Clipboard.SetText(txt_Base64CryptoText.Text);
}
private void txt_BinaryCryptoText_DoubleClick(object sender, EventArgs e)
{
Clipboard.SetText(txt_BinaryCryptoText.Text);
}
private void radioButton_Text_CheckedChanged(object sender, EventArgs e)
{
isTextContent = radioButton_Text.Checked;
btn_OpenFile.Enabled = !isTextContent;
}
private void btn_OpenFile_Click(object sender, EventArgs e)
{
if (ofd_OpenFile.ShowDialog() == DialogResult.OK)
{
txt_PlainText.Text = ofd_OpenFile.FileName;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -