博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
加减密 DES
阅读量:4978 次
发布时间:2019-06-12

本文共 7598 字,大约阅读时间需要 25 分钟。

/**////     /// DES    ///     public class DES_    {        private DES mydes;        public string Key;        public string IV;        /**////         /// 对称加密类的构造函数        ///         public DES_(string key)        {            mydes = new DESCryptoServiceProvider();            Key = key;            IV = "728#$$%^TyguyshdsufhsfwofnhKJHJKHIYhfiusf98*(^%$^&&(*&()$##@%%$RHGJJHHJ";        }        /**////         /// 对称加密类的构造函数        ///         public DES_(string key, string iv)        {            mydes = new DESCryptoServiceProvider();            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); } /**//// /// 获得初始向量IV /// ///
初试向量IV
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); cs.FlushFinalBlock(); ms.Close(); byte[] bytOut = ms.ToArray(); return Convert.ToBase64String(bytOut); } catch (Exception ex) { throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message); } } /**//// /// 解密方法 /// /// 待解密的串 ///
经过解密的串
public string Decrypt(string Source) { try { byte[] bytIn = Convert.FromBase64String(Source); MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); mydes.Key = GetLegalKey(); mydes.IV = GetLegalIV(); ICryptoTransform encrypto = mydes.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return sr.ReadToEnd(); } catch (Exception ex) { throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message); } } /**//// /// 加密方法byte[] to byte[] /// /// 待加密的byte数组 ///
经过加密的byte数组
public byte[] Encrypt(byte[] Source) { try { byte[] bytIn = 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); cs.FlushFinalBlock(); ms.Close(); byte[] bytOut = ms.ToArray(); return bytOut; } catch (Exception ex) { throw new Exception("在文件加密的时候出现错误!错误提示: " + ex.Message); } } /**//// /// 解密方法byte[] to byte[] /// /// 待解密的byte数组 ///
经过解密的byte数组
public byte[] Decrypt(byte[] Source) { try { byte[] bytIn = Source; MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); mydes.Key = GetLegalKey(); mydes.IV = GetLegalIV(); ICryptoTransform encrypto = mydes.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return UTF8Encoding.UTF8.GetBytes(sr.ReadToEnd()); } catch (Exception ex) { throw new Exception("在文件解密的时候出现错误!错误提示: " + ex.Message); } } /**//// /// 加密方法File to File /// /// 待加密文件的路径 /// 待加密后文件的输出路径 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 /// /// 待解密文件的路径 /// 待解密后文件的输出路径 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); } } }

 

转载于:https://www.cnblogs.com/ToFlying/p/3610862.html

你可能感兴趣的文章