|
思路:使用所给的TripleDES_类的Encrypt方法和Decrypt方法分别进行明文文件的加密和密文文件的解密,将所转换的内容传入到对应的文件中。
具体操作如下:
1、创建三个txt文本,分别为ji1,ji2,ji3。
2、引用类TripleDES_;
3、传入待加密文件的路径inFileName1,加密后文件的输出路径outFileName1,待解密文件的路径outFileName1,解密后文件的输出路径outFileName2;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TriDES
- {
- class Program
- {
- static void Main(string[] args)
- {
- string key = "jijijijijiji";
- TripleDES_ tripleDES_ = new TripleDES_(key);
- Console.WriteLine("需要加密的文件:");
- string inFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
- Console.WriteLine("加密后的文件:");
- string miFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
- Console.WriteLine("解密后的文件:");
- string outFile = @"C:\Users\name\Desktop" + Console.ReadLine() + ".txt";
- tripleDES_.Encrypt(inFile,miFile);
- tripleDES_.Decrypt(miFile, outFile);
- }
- }
- }
- 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);
- }
- }
- /// 解密方法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);
- }
- }
- }
- }
复制代码 运行截图
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|