|
- using System;
- using System.Security.Cryptography;
- using System.Text;
- using System.IO;
- namespace ConsoleApp111
- { /**//// <summary>
- /// 三重DES
- /// </summary>
- public class TripleDES_
- {
- private TripleDES mydes;
- public string Key;
- public string IV;
- /**//// <summary>
- /// 对称加密类的构造函数
- /// </summary>
- public TripleDES_(string key)
- {
- mydes = new TripleDESCryptoServiceProvider();
- Key = key;
- IV = "#$^%&&*Yisifhsfjsljfslhgosdshf26382837sdfjskhf97(*&(*";
- }
- /**//// <summary>
- /// 对称加密类的构造函数
- /// </summary>
- public TripleDES_(string key, string iv)
- {
- mydes = new TripleDESCryptoServiceProvider();
- Key = key;
- IV = iv;
- }
- /**//// <summary>
- /// 获得密钥
- /// </summary>
- /// <returns>密钥</returns>
- private byte[] GetLegalKey()
- {
- string sTemp = Key;
- mydes.GenerateKey();
- byte[] bytTemp = mydes.Key;
- int KeyLength = bytTemp.Length;
- if (sTemp.Length > KeyLength)
- sTemp = sTemp.Substring(0, KeyLength);
- else if (sTemp.Length < KeyLength)
- sTemp = sTemp.PadRight(KeyLength, ' ');
- return ASCIIEncoding.ASCII.GetBytes(sTemp);
- }
- /**//// <summary>
- /// 获得初始向量IV
- /// </summary>
- /// <returns>初试向量IV</returns>
- private byte[] GetLegalIV()
- {
- string sTemp = IV;
- mydes.GenerateIV();
- byte[] bytTemp = mydes.IV;
- int IVLength = bytTemp.Length;
- if (sTemp.Length > IVLength)
- sTemp = sTemp.Substring(0, IVLength);
- else if (sTemp.Length < IVLength)
- sTemp = sTemp.PadRight(IVLength, ' ');
- return ASCIIEncoding.ASCII.GetBytes(sTemp);
- }
- /**//// <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);
- }
- }
- static void Main(string[] args)
- {
- string inFileName1 = @"D:\密码学第一次作业\1.txt";
- //待加密文件的路径
- string outFileName1 = @"D:\密码学第一次作业\2.txt";
- //加密后文件的输出路径
- string inFileName2 = @"D:\密码学第一次作业\2.txt";
- //待解密文件的路径
- string outFileName2 = @"D:\密码学第一次作业\3.txt";
- //解密后文件的输出路径
- string key = "564765756466";
- //密钥key
- //加解密方法File to File
- TripleDES_ my3des = new TripleDES_(key);
- my3des.Encrypt(inFileName1, outFileName1);
- my3des.Decrypt(inFileName2, outFileName2);
- }
- }
- }
复制代码
1.建立三个文件,分别为1,2,3.
2.调用方法Encrypt,对待加密的文件进行加密. 3.调用方法Decrypt,对待解密的文件进行解密。
第一次
第二次
第三次
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|