C#数据加密与解密 (2)

    技术2022-05-12  15

    实例465 利用图片加密文件 实例说明 本实例中,利用图片生成密钥,然后对文本文件进行加密和解密操作。运行程序,单击【打开图片】按钮,选择密钥图片,然后单击【打开文本文件】按钮,选择要加密或解密的文件,单击【加密】或【解密】按钮完成文本文件的加密或解密操作。解密时的密钥图片要与加密时的密钥图片相同,否则解密不能成功。实例运行结果如图16.3所示。 技术要点 实现本实例功能主要用到了System.Security.Cryptography命名空间下的RC2CryptoServiceProvider类的CreateDecryptor( )方法、CreateEncryptor( )方法、CryptoStream类的Write( )方法、FlushFinalBlock( )方法、Close( )方法、System.IO命名空间下的FileStream类、BinaryReader类的ReadBytes( )方法、BinaryWriter类的Write( )方法、File类的Delete( )方法和Copy( )方法。以上大部分知识在第16章实例464中已经做过详细介绍,这里不再详细讲解。下面主要对RC2CryptoServiceProvider类、BinaryWriter类的Write( )方法、File类的Delete( )方法和Copy( )方法进行介绍。 (1)RC2CryptoServiceProvider类 此类定义访问RC2算法的加密服务提供程序(CSP)实现的包装对象。 (2)BinaryWriter类 此类以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。 语法格式为: public BinaryWriter (Streamoutput) 参数说明如下。 l     output:输出流。 (3)BinaryWriter类的Write( )方法 此方法将一个无符号字节写入当前流,并将流的位置提升1个字节。 语法格式为: public virtual void Write (byte value) 参数说明如下。 l     value:要写入的无符号字节。 (4)File类 此类提供用于创建、复制、删除、移动和打开文件的静态方法,并协助创建FileStream对象。 (5)Delete( )方法 此方法删除指定的文件。如果指定的文件不存在,则引发异常。 语法格式为: public static void Delete (string path) 参数说明如下。 l     path:要删除的文件的名称。 (6)Copy( )方法 此方法将现有文件复制到新文件,不允许改写同名的文件。 语法格式为: public static void Copy (string sourceFileName,string destFileName) 参数说明如下。 l     sourceFileName:要复制的文件。 l     destFileName:目标文件的名称,不能是一个目录或现有文件。 实现过程 (1)新建一个Windows应用程序,将其命名为Ex16_03,默认窗体为Form1。 (2)在Form1窗体中,主要添加一个TextBox控件,用来显示加密或解密文件的路径;添加一个OpenFileDialog控件,用来选择要加密或解密的文件和打开密钥的图片;添加4个Button控件,用来执行加密、解密、打开文件和打开图片操作;添加一个PictureBox控件,用于显示密钥图片。 (3)主要程序代码。 利用图片加密文本文件的实现代码如下: private void button3_Click(object sender, EventArgs e)         {             try             {                 if (pictureBox1.ImageLocation==null)                 { MessageBox.Show("请选择一幅图片用于加密"); return; }                 if (textBox1.Text == "")                 { MessageBox.Show("请选择加密文件路径"); return; }                 //图片流                 FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read);                 //加密文件流                 FileStream fsText = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);                 //初始化Key IV                 byte[] bykey = new byte[16];                 byte[] byIv = new byte[8];                 fsPic.Read(bykey, 0, 16);                 fsPic.Read(byIv, 0, 8);                 //临时加密文件                 string strPath = textBox1.Text;//加密文件的路径                 int intLent = strPath.LastIndexOf("//") + 1;                 int intLong = strPath.Length;                 string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称                 string strLinPath = "C://" + strName;//临时加密文件路径                 FileStream fsOut = File.Open(strLinPath, FileMode.Create, FileAccess.Write);                 //开始加密             RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//desc进行加密                  BinaryReader br = new BinaryReader(fsText);//从要加密的文件中读出文件内容                 CryptoStream cs = new CryptoStream(fsOut, desc.CreateEncryptor(bykey, byIv), CryptoStreamMode.Write);//写入临时加密文件                 cs.Write(br.ReadBytes((int)fsText.Length), 0, (int)fsText.Length);//写入加密流                 cs.FlushFinalBlock();                 cs.Flush();                 cs.Close();                 fsPic.Close();                 fsText.Close();                 fsOut.Close();                 File.Delete(textBox1.Text.TrimEnd());//删除原文件                 File.Copy(strLinPath, textBox1.Text);//复制加密文件                 File.Delete(strLinPath);//删除临时文件                 MessageBox.Show("加密成功");                 pictureBox1.ImageLocation = null;                 textBox1.Text = "";             }             catch (Exception ee)             {                 MessageBox.Show(ee.Message);             }         } 利用图片解密文本文件的实现代码如下: private void button4_Click(object sender, EventArgs e)         {             try             {                  //图片流         FileStream fsPic = new FileStream(pictureBox1.ImageLocation, FileMode.Open, FileAccess.Read);                 //解密文件流             FileStream fsOut = File.Open(textBox1.Text, FileMode.Open, FileAccess.Read);                 //初始化Key IV                 byte[] bykey = new byte[16];                 byte[] byIv = new byte[8];                 fsPic.Read(bykey, 0, 16);                 fsPic.Read(byIv, 0, 8);                 //临时解密文件                 string strPath = textBox1.Text;//加密文件的路径                 int intLent = strPath.LastIndexOf("//") + 1;                 int intLong = strPath.Length;       string strName = strPath.Substring(intLent, intLong - intLent);//要加密的文件名称                 string strLinPath = "C://" + strName;//临时解密文件路径                 FileStream fs = new FileStream(strLinPath, FileMode.Create, FileAccess.Write);                 //开始解密           RC2CryptoServiceProvider desc = new RC2CryptoServiceProvider();//desc进行解密            CryptoStream csDecrypt = new CryptoStream(fsOut, desc.CreateDecryptor(bykey, byIv), CryptoStreamMode.Read);//读出加密文件                 BinaryReader sr = new BinaryReader(csDecrypt);//从要加密流中读出文件内容                 BinaryWriter sw = new BinaryWriter(fs);//写入解密流                 sw.Write(sr.ReadBytes(Convert.ToInt32(fsOut.Length)));//                 sw.Flush();                 sw.Close();                 sr.Close();                 fs.Close();                 fsOut.Close();                 fsPic.Close();                 csDecrypt.Flush();                 File.Delete(textBox1.Text.TrimEnd());//删除原文件                 File.Copy(strLinPath, textBox1.Text);//复制加密文件                 File.Delete(strLinPath);//删除临时文件                 MessageBox.Show("解密成功");                 pictureBox1.ImageLocation = null;                 textBox1.Text = "";             }             catch (Exception ee)             {                 MessageBox.Show(ee.Message);             }         } 举一反三 根据本实例,读者可以实现以下功能。 利用各种图片加密文件。 使用图片批量解密文件。 

    最新回复(0)