|
本帖最后由 张欢 于 2022-4-15 22:51 编辑
3DES是DES加密算法的一种模式,它使用2条不同的56位的密钥对数据进行三次加密,而且每个密钥之间是独立的。
以下为主函数:- static void Main(String[] args){
- TripleDES_ TripleDES_=new TripleDES_("fhaskfsdfakfsdot");
- TripleDES_.Encrypt(@"D:\桌面\密码学1.txt", @"D:\桌面\密码学2.txt");
- TripleDES_.Decrypt(@"D:\桌面\密码学2.txt", @"D:\桌面\密码学3.txt");
- }
复制代码 读取密码学1.txt中的内容,进行3DES加密,再将密文存入密码学2.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 类的新实例。
- 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);
- }
- }
复制代码 其中,
- ICryptoTransform encrypto = mydes.CreateEncryptor();
复制代码 这是定义加密转换的基本操作,用的是mydes,前面用了private TripleDES mydes,就是将mydes定义成3des。
读取密码学2.txt中的内容,然后进行3DES解密。
- /**//// <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);
- }
- }
复制代码 其中,
- ICryptoTransform encrypto = mydes.CreateDecryptor();
复制代码 这是定义解密转换的基本操作,用的是3DES解密。
3DES加解密过程:
- private TripleDES mydes;
- public string Key;
- public string IV;
- public TripleDES_(string key)
- {
- mydes = new TripleDESCryptoServiceProvider();
- Key = key;
- IV = "#$^%&&*Yisifhsfjsljfslhgosdshf26382837sdfjskhf97(*&(*";
- }
- public TripleDES_(string key, string iv)
- {
- mydes = new TripleDESCryptoServiceProvider();
- Key = key;
- IV = iv;
- }
- 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);
- }
- 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);
- }
- public string Encrypt(string Source)
- {
- try
- {
- byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source);
- MemoryStream ms = new MemoryStream();
- mydes.Key = GetLegalKey();
- mydes.IV = GetLegalIV();
- ICryptoTransform encrypto = mydes.CreateEncryptor();// 创建对称加密器对象
- CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);//将数据流链接到加密转换的流
- cs.Write(bytIn, 0, bytIn.Length);//将一字节序列写入当前的 CryptoStream,并将通过写入的字节数提前该流的当前位置
- cs.FlushFinalBlock();//用缓冲区的当前状态更新基础数据源或存储库,随后清除缓冲区。
- ms.Close();
- byte[] bytOut = ms.ToArray();
- return Convert.ToBase64String(bytOut);
- }
- catch (Exception ex)
- {
- throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message);
- }
- }
复制代码 运行结果如下:
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|