1,对文件进行加密与解密(Windows窗体应用程序中做一个简单的加密与解密功能)
步骤:
(一):创建一个Windows窗体应用程序
(二):在该窗体中添加一个TextBox控件,用来显示文件路径。一个OpenFileDialog控件,用来选择要加密或解密的文件。添加三个Button控件。用来执行加密、解密和选择文件操作
(三):代码如下
1 //获取文件路径 2 private void button1_Click(object sender, EventArgs e) 3 { 4 OpenFileDialog openFile = new OpenFileDialog();//创建打开文件对话对话框对象 5 if (openFile.ShowDialog() == DialogResult.OK)//判断是否选中文件 6 { 7 textBox1.Text = openFile.FileName; 8 } 9 } 10 11 //单击“加密”按钮实现对选择的文本进行加密,“加密”按钮的Click事件代码如下; 12 //加密 13 private void button3_Click(object sender, EventArgs e) 14 { 15 if (textBox1.Text == "")//若没选择要加密的文本文件 16 { 17 MessageBox.Show("1请选择要加密的文件");//如果没有选择则弹出提示 18 } 19 else 20 { 21 try 22 { 23 string strPath = textBox1.Text;//加密文件的路径 24 int intLent = strPath.LastIndexOf("\\") + 1;//设置截取的起始位置 25 int intLong = strPath.Length;//设置截取的长度 26 string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称 27 int intTxt = strName.LastIndexOf(".");//设置截取的起始位置 28 int intTxtLeng = strName.Length;//设置截取的长度 29 string strTxt = strName.Substring(intTxt, intTxtLeng - intTxt);//取出文件的拓展名 30 strName = strName.Substring(0, intTxt); 31 string strOutName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName + "Out" + strTxt;//加密后的文件名及路径 32 byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };//加密文件密钥 33 byte[] Iv = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 }; 34 RijndaelManaged myRijndael = new RijndaelManaged(); 35 FileStream fsOut = File.Open(strOutName, FileMode.Create, FileAccess.Write); 36 FileStream fsIn = File.Open(strPath, FileMode.Open, FileAccess.Read); 37 CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key, Iv), CryptoStreamMode.Write);//写入加密文本文件 38 BinaryReader br = new BinaryReader(fsIn);//创建阅读器来加密文件 39 csDecrypt.Write(br.ReadBytes((int)fsIn.Length), 0, (int)fsIn.Length);//将数据写入加密文件 40 csDecrypt.Close();//关闭CryptoStream对象 41 fsIn.Close();//关闭FileStream对象 42 fsOut.Close();//关闭FileStream对象 43 if (MessageBox.Show("加密成功!加密后的文件名及路径为:\n" + strOutName + ",是否删除文件", "提示信息", MessageBoxButtons.YesNo) == DialogResult.Yes) 44 { 45 File.Delete(strPath);//清除指定文件 46 textBox1.Text = "";//请空文本文件 47 } 48 else 49 { 50 textBox1.Text = ""; 51 } 52 } 53 catch (Exception ex) 54 { 55 56 MessageBox.Show(ex.Message); 57 } 58 } 59 } 60 61 //单击“解密”按钮实现对选择的文本进行加密,“解密”按钮的Click事件代码如下; 62 //解密 63 private void button2_Click(object sender, EventArgs e) 64 { 65 if (textBox1.Text == "")//若没选择要解密的文本文件 66 { 67 MessageBox.Show("请选择要解密的文件");//如果没有选择则弹出提示 68 } 69 else 70 { 71 try 72 { 73 string strPath = textBox1.Text;//加密文件的路径 74 int intLent = strPath.LastIndexOf("\\") + 1;//设置截取的起始位置 75 int intLong = strPath.Length;//设置截取的长度 76 string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称 77 int intTxt = strName.LastIndexOf(".");//设置截取的起始位置 78 int intTxtLeng = strName.Length;//设置截取的长度 79 strName = strName.Substring(0, intTxt);//获取文件拓展名 80 if (strName.LastIndexOf("Out") != -1) 81 { 82 strName = strName.Substring(0, strName.LastIndexOf("Out")); 83 } 84 else 85 { 86 strName = strName + "In"; 87 } 88 string strInName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName+ ".txt";//解密后的文件名及路径 89 //解密文件密钥 90 byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 24, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 }; 91 byte[] Iv = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 }; 92 RijndaelManaged myRijndael = new RijndaelManaged();//创建RijndaelManaged对象 93 FileStream fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read);//创建FileStream对象 94 CryptoStream csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key, Iv), CryptoStreamMode.Read); 95 StreamReader sr = new StreamReader(csDecrypt);//把文件读出来 96 StreamWriter sw = new StreamWriter(strInName);//解密后写一个新文件 97 sw.Write(sr.ReadToEnd()); 98 sw.Flush(); 99 sw.Close();//关闭FileStream对象 100 fsOut.Close();//关闭FileStream对象 101 if (MessageBox.Show("解密成功!解密后的文件名及路径为:\n" + strInName + ",是否删除文件", "提示信息", MessageBoxButtons.YesNo) == DialogResult.Yes) 102 { 103 File.Delete(strPath);//清除指定文件 104 textBox1.Text = "";//请空文本文件 105 } 106 else 107 { 108 textBox1.Text = ""; 109 } 110 } 111 catch (Exception ex) 112 { 113 114 MessageBox.Show(ex.Message); 115 } 116 } 117 }
(四):运行结果:
本例主要1用了System.Security.Cryptograhy命名空间子下的RijndaelManaged类的CreateDecryptor和CreateEncryptor方法以及CryptoStream类的Write方法。
(1):RijndaelManaged类
该类是访问System.Security.Cryptography.Rijndal对称加密算法的托管版本。其语法格式如下:
public sealed class RijndaelManaged:Rijndael
注意:此算法支持128、192或256位的密钥长度
(2):CreateDecryptor方法
该方法位于RijndaelManaged类中,使用指定的Key和初始化向量(IV)创建对称的Rijidael解密器对象。其语法格式如下:
public override ICryptoTransform CreateDecryptor(byte[] rgbKey,byte[] rgbIV)
参数说明:
rgbKey:用于对称算法的机密密钥
rgbIV:同于对称算法的IV
返回值:对称的Rijndael加密器对象
(3):CreateEncryptor方法
该方法位于RijndaelManaged类中,使用指定的Key和初始化向量(IV)创建对称的Rijidael解密器对象。其语法格式如下:
public override ICryptoTransform CreateEncryptor(byte[] rgbKey,byte[] rgbIV)
参数说明:
rgbKey:用于对称算法的机密密钥
rgbIV:同于对称算法的IV
返回值:对称的Rijndael加密器对象
(4):CryptoStream类
该类定义了将数据流连接到加密转换的流,它的构造器语法格式如下:
public CryptoStream(Stream, ICryptoTransform Transform,CryptoStreamMode mode);
参数说明:
stream:对其执行加密转换的流
Transform:要对流执行的加密转换
mode:CryptoStreamMode的枚举值一。CryptoStreamMode的枚举值及说明如下:
枚举 | 说明 |
Read | 对加密流的读访问 |
Write | 对加密流的写访问 |
(5):CryptoStream类的Write方法
该方法将一个字节序列写入当前的CryptoStream类中,并从当前位置写入指定的字节数。其语法格式如下:
public override void Write(byte[] budder,int offset,in count)
参数说明:
buffer:字节数组。该方法将count个字节从buffer复制到当前流
offset:buffer中的字节偏移量,从此偏移量开始将字节复制到当前流
count:要写入的当前流的字节数