|
本帖最后由 王庆海 于 2022-4-15 20:22 编辑
原理:
3des算法主要采用了下列两个类的算法
- 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> 创建文件夹存放需要加密的原文
<2> 创建主函数实现对加密算法的调用
- using System;
- namespace ConsoleApp2
- {
- class des1
- {
- static void Main(string[] args)
- {
- string cokey = "tkGGRmBErvc=";
- TripleDES_ tri = new TripleDES_(cokey);
- Console.Write("请输入此桌面上需要加密的文件名:\n");
- string inFileName = @"C:\Users\86176\Desktop" + Console.ReadLine() + ".txt";
- Console.Write("请输入加密后存放的文件名:\n");
- string outFileName = @"C:\Users\86176\Desktop" + Console.ReadLine() + ".txt";
- Console.Write("请输入解密后存放的文件名:\n");
- string finalFileName = @"C:\Users\86176\Desktop" + Console.ReadLine() + ".txt";
- tri.Encrypt(inFileName, outFileName);
- tri.Decrypt(outFileName, finalFileName);
- }
- }
- }
复制代码 运行界面:
原文:
加密文件夹:
解密文件夹:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|