|
一.大体思路:
使用老师给的TripleDES_类的Encrypt方法和Decrypt方法进行文件的加密解密,最后将所转换的内容传入到对应的文件中。
Encrypt方法:此方法主要通过传入一个原文的文件地址,方法的作用则是用来对原文加密,并且将加密后的数据放入传入的第二个参数指向的文件,这里我们指的是加密文件(Encrypt.txt)。
Decrypt方法:此方法通过将传入的密文文件中的内容进行解密,并且将解密后的内容传入第二个参数所指向的文件,这里指的是解密文件(Decrypt.txt)。
- /**//// <summary>
- /// 加密方法File to File
- /// </summary>
- /// <param name="inFileName">待加密文件的路径</param>
- /// <param name="outFileName">待加密后文件的输出路径</param>
- public void Encrypt(string inFileName, string outFileName)
- {
- try
- {
- FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
- FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
- fout.SetLength(0);
- mydes.Key = GetLegalKey();
- mydes.IV = GetLegalIV();
- byte[] bin = new byte[100];
- long rdlen = 0;
- long totlen = fin.Length;
- int len;
- ICryptoTransform encrypto = mydes.CreateEncryptor();
- CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
- while (rdlen < totlen)
- {
- len = fin.Read(bin, 0, 100);
- cs.Write(bin, 0, len);
- rdlen = rdlen + len;
- }
- cs.Close();
- fout.Close();
- fin.Close();
- }
- catch (Exception ex)
- {
- throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
- }
- }
- /**//// <summary>
- /// 解密方法File to File
- /// </summary>
- /// <param name="inFileName">待解密文件的路径</param>
- /// <param name="outFileName">待解密后文件的输出路径</param>
- public void Decrypt(string inFileName, string outFileName)
- {
- try
- {
- FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
- FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write);
- fout.SetLength(0);
- byte[] bin = new byte[100];
- long rdlen = 0;
- long totlen = fin.Length;
- int len;
- mydes.Key = GetLegalKey();
- mydes.IV = GetLegalIV();
- ICryptoTransform encrypto = mydes.CreateDecryptor();
- CryptoStream cs = new CryptoStream(fout, encrypto, CryptoStreamMode.Write);
- while (rdlen < totlen)
- {
- len = fin.Read(bin, 0, 100);
- cs.Write(bin, 0, len);
- rdlen = rdlen + len;
- }
- cs.Close();
- fout.Close();
- fin.Close();
- }
- catch (Exception ex)
- {
- throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message);
- }
- }
复制代码
二.运行截图1.创建的三个txt文件
从左到右分别是原文文件、加密文件、解密文件
2.不同的例子的运行截图
(1)例一:
(2)例二:
(3)例三:
三.源代码:
(1)主程序代码:
- using System;
- namespace ldd_3DES
- {
- class Program
- {
- static void Main(string[] args)
- {
- TripleDES_ trides = new TripleDES_("jdsafueaofjwoifoiweoif");
- string primetxt = "C:\\Users\\HP\\Desktop\\Prime.txt";
- string Encrypttxt = "C:\\Users\\HP\\Desktop\\Encrypt.txt";
- string Decrypttxt = "C:\\Users\\HP\\Desktop\\Decrypt.txt";
- trides.Encrypt(primetxt, Encrypttxt);
- trides.Decrypt(Encrypttxt, Decrypttxt);
- }
- }
- }
复制代码
(2)启动程序截图:
在更改原文文件中的内容后,启动程序,则对应加密解密文件中的内容相应变化
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|