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

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

using System;using System.IO;using System.Security.Cryptography;using System.Text;public class CCryptoHandle    {        //=============================== 构造函数 ===============================        #region 对称加密类的构造函数        ///         /// 对称加密类的构造函数        ///         /// 密钥        public CCryptoHandle(string key)        {            myRijndael = new RijndaelManaged();            Key = key;            IV = "12^%*(&(_)*&7!rNIfb&95GUYEDF4ghUb#er57HBh(u%g6HJ($GE3r7&!hg4ui%$hV$";        }        #endregion        #region 对称加密类的构造函数        ///         /// 对称加密类的构造函数        ///         /// 密钥        /// 初始向量IV        public CCryptoHandle(string key, string iv)        {            myRijndael = new RijndaelManaged();            Key = key;            IV = iv;        }        #endregion        //=============================== 私有变量 ===============================        #region 私有变量        private RijndaelManaged myRijndael;        ///         /// 密钥        ///         public string Key;        ///         /// 初始向量IV        ///         public string IV;        #endregion        //=============================== 共有方法 ===============================        #region 获得密钥        ///         /// 获得密钥        ///         /// 
密钥
private byte[] GetLegalKey() { string sTemp = Key; myRijndael.GenerateKey(); byte[] bytTemp = myRijndael.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); } #endregion #region 获得初始向量IV /// /// 获得初始向量IV /// ///
初试向量IV
private byte[] GetLegalIV() { string sTemp = IV; myRijndael.GenerateIV(); byte[] bytTemp = myRijndael.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); } #endregion #region 加密方法 /// /// 加密方法 /// /// 待加密的串 ///
经过加密的串
public string Encrypt(string Source) { try { byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source); MemoryStream ms = new MemoryStream(); myRijndael.Key = GetLegalKey(); myRijndael.IV = GetLegalIV(); ICryptoTransform encrypto = myRijndael.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("在文件加密的时候出现错误!错误提示: \n" + ex.Message); } } #endregion #region 解密方法 /// /// 解密方法 /// /// 待解密的串 ///
经过解密的串
public string Decrypt(string Source) { try { byte[] bytIn = Convert.FromBase64String(Source); MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); myRijndael.Key = GetLegalKey(); myRijndael.IV = GetLegalIV(); ICryptoTransform encrypto = myRijndael.CreateDecryptor(); CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Read); StreamReader sr = new StreamReader(cs); return sr.ReadToEnd(); } catch (Exception ex) { throw new Exception("在文件解密的时候出现错误!错误提示: \n" + ex.Message); } } #endregion #region 加密方法byte[] to byte[] /// /// 加密方法byte[] to byte[] /// /// 待加密的byte数组 ///
经过加密的byte数组
public byte[] Encrypt(byte[] Source) { try { byte[] bytIn = Source; MemoryStream ms = new MemoryStream(); myRijndael.Key = GetLegalKey(); myRijndael.IV = GetLegalIV(); ICryptoTransform encrypto = myRijndael.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("在文件加密的时候出现错误!错误提示: \n" + ex.Message); } } #endregion #region 解密方法byte[] to byte[] /// /// 解密方法byte[] to byte[] /// /// 待解密的byte数组 ///
经过解密的byte数组
public byte[] Decrypt(byte[] Source) { try { byte[] bytIn = Source; MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length); myRijndael.Key = GetLegalKey(); myRijndael.IV = GetLegalIV(); ICryptoTransform encrypto = myRijndael.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("在文件解密的时候出现错误!错误提示: \n" + ex.Message); } } #endregion #region 加密方法File to File /// /// 加密方法File to File /// /// 待加密文件的路径 /// 待加密后文件的输出路径 public void Encrypt(string inFileName, string outFileName) { try { //byte[] bytIn = UTF8Encoding.UTF8.GetBytes(Source); //MemoryStream ms = new MemoryStream(); FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read); FileStream fout = new FileStream(outFileName, FileMode.OpenOrCreate, FileAccess.Write); fout.SetLength(0); myRijndael.Key = GetLegalKey(); myRijndael.IV = GetLegalIV(); byte[] bin = new byte[100]; long rdlen = 0; long totlen = fin.Length; int len; ICryptoTransform encrypto = myRijndael.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("在文件加密的时候出现错误!错误提示: \n" + ex.Message); } } #endregion #region 解密方法File to File /// /// 解密方法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; myRijndael.Key = GetLegalKey(); myRijndael.IV = GetLegalIV(); ICryptoTransform encrypto = myRijndael.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("在文件解密的时候出现错误!错误提示: \n" + ex.Message); } } #endregion }

 

转载于:https://www.cnblogs.com/benhua/p/6060704.html

你可能感兴趣的文章
Makefile-2
查看>>
获取页面中出现次数最多的三个标签以及出现次数
查看>>
访问WEB-INF目录中的文件
查看>>
web接口开发与测试
查看>>
php -- php控制linux关机、重启、注销
查看>>
867. Transpose Matrix
查看>>
十.python面向对象(itme)
查看>>
Python下selenium的简单用法
查看>>
multiset的应用
查看>>
我的mysql的学习记录
查看>>
Codeforces Round #416 (Div. 2)(A,思维题,暴力,B,思维题,暴力)
查看>>
NYOJ 题目77 开灯问题(简单模拟)
查看>>
模式识别
查看>>
设置文本编辑器的按回车时触发的事件
查看>>
关于android手机不能打印Log日志
查看>>
hdu 2157 How many ways?? **
查看>>
xcode8 更新cocoapods
查看>>
习题:海盗船(广搜)
查看>>
auto_ptr
查看>>
Windows系统的消息机制
查看>>