先附上源碼http://files.cnblogs.com/files/zhanqun/MY.Cipher.rar
包含RSA\AES\DES加解密算法
RSA
using MY.Cipher.Csv; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; namespace MY.Cipher { public class RSACode { /// <summary> /// 創建RSA公鑰和私鑰 /// </summary> /// <param name="publicKey"></param> /// <param name="privateKey"></param> /// <returns></returns> public static bool CreateKey(out string publicKey, out string privateKey) { publicKey = null; privateKey = null; try { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { #if RSAXML privateKey = rsa.ToXmlString(true); publicKey = rsa.ToXmlString(false); #else byte[] publicKeyBytes = rsa.ExportCspBlob(false); byte[] privateKeyBytes = rsa.ExportCspBlob(true); publicKey = Convert.ToBase64String(publicKeyBytes); privateKey = Convert.ToBase64String(privateKeyBytes); #endif return true; } } catch (Exception) { return false; } } /// <summary> /// RSA加密 /// </summary> /// <param name="publickey"></param> /// <param name="content"></param> /// <returns></returns> public static string Encrypt(string publickey, string content) { if (string.IsNullOrEmpty(content)) return null; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { byte[] cipherbytes; #if RSAXML rsa.FromXmlString(publickey); #else byte[] keyBytes = Convert.FromBase64String(publickey); rsa.ImportCspBlob(keyBytes); #endif cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); return Convert.ToBase64String(cipherbytes); } } /// <summary> /// RSA加密 /// </summary> /// <param name="publickey"></param> /// <param name="dt"></param> /// <param name="columnIndexs"></param> /// <returns></returns> public static DataTable Encrypt(string publickey, DataTable dt, int[] columnIndexs) { if (dt == null) return null; DataTable result = dt.Clone(); using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { #if RSAXML rsa.FromXmlString(publickey); #else byte[] keyBytes = Convert.FromBase64String(publickey); rsa.ImportCspBlob(keyBytes); #endif foreach (DataRow dr in dt.Rows) { object[] objs = dr.ItemArray; foreach (int index in columnIndexs) { if (objs[index] != null && objs[index] != DBNull.Value) { byte[] bytes = rsa.Encrypt(Encoding.UTF8.GetBytes(objs[index].ToString()), false); objs[index] = Convert.ToBase64String(bytes); } } result.Rows.Add(objs); } } return result; } /// <summary> /// RSA解密 /// </summary> /// <param name="privatekey"></param> /// <param name="content"></param> /// <returns></returns> public static string Decrypt(string privatekey, string content) { if (string.IsNullOrEmpty(content)) return null; using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { byte[] cipherbytes; #if RSAXML rsa.FromXmlString(privatekey); #else byte[] keyBytes = Convert.FromBase64String(privatekey); rsa.ImportCspBlob(keyBytes); #endif cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); return Encoding.UTF8.GetString(cipherbytes); } } /// <summary> /// RSA解密 /// </summary> /// <param name="privatekey"></param> /// <param name="dt"></param> /// <param name="columnIndexs"></param> /// <returns></returns> public static DataTable Decrypt(string privatekey, DataTable dt, int[] columnIndexs) { if (dt == null) return null; DataTable result = dt.Clone(); using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { #if RSAXML rsa.FromXmlString(privatekey); #else byte[] keyBytes = Convert.FromBase64String(privatekey); rsa.ImportCspBlob(keyBytes); #endif foreach (DataRow dr in dt.Rows) { object[] objs = dr.ItemArray; foreach (int index in columnIndexs) { if (objs[index] != null && objs[index] != DBNull.Value) { byte[] bytes = rsa.Decrypt(Convert.FromBase64String(objs[index].ToString()), false); objs[index] = Encoding.UTF8.GetString(bytes); } } result.Rows.Add(objs); } } return result; } public static int Encrypt(string publickey, string src, string dest, int[] columns, Predicate<string> action) { return Encrypt(publickey, src, dest, true, columns, action); } public static int Encrypt(string publickey, string src, string dest, bool hasHeaders, int[] columns, Predicate<string> action) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { #if RSAXML rsa.FromXmlString(publickey); #else byte[] keyBytes = Convert.FromBase64String(publickey); rsa.ImportCspBlob(keyBytes); #endif using (TextReader reader = new StreamReader(src, Encoding.Default)) { using (TextWriter writer = new StreamWriter(dest, false, Encoding.Default)) { CsvReader _reader = new CsvReader(reader, hasHeaders); if (hasHeaders) writer.WriteLine(string.Join(",", _reader.GetFieldHeaders())); int rowIndex = 0; while (_reader.ReadNextRecord()) { if (rowIndex > 0 && rowIndex % 100 == 0 && action != null) { if (!action(string.Format("正在處理第{0}行...", rowIndex))) break; } string[] objs = new string[_reader.FieldCount]; for (int index = 0; index < objs.Length; index++) { if (_reader[index] != null && Array.Exists(columns, (column) => { return Convert.ToInt32(column) == index; })) { byte[] bytes = rsa.Encrypt(Encoding.UTF8.GetBytes(_reader[index].ToString()), false); objs[index] = Convert.ToBase64String(bytes); } else objs[index] = _reader[index]; } writer.WriteLine(string.Join(",", objs)); rowIndex++; } reader.Close(); writer.Close(); return rowIndex; } } } } public static void Encrypt(string publickey, string src, string dest, int[] columns) { Encrypt(publickey, src, dest, columns, null); } public static int Decrypt(string privatekey, string src, string dest, int[] columns, Predicate<string> action) { return Decrypt(privatekey, src, dest, true, columns, action); } public static int Decrypt(string privatekey, string src, string dest, bool hasHeaders, int[] columns, Predicate<string> action) { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { #if RSAXML rsa.FromXmlString(privatekey); #else byte[] keyBytes = Convert.FromBase64String(privatekey); rsa.ImportCspBlob(keyBytes); #endif using (TextReader reader = new StreamReader(src, Encoding.Default)) { using (TextWriter writer = new StreamWriter(dest, false, Encoding.Default)) { CsvReader _reader = new CsvReader(reader, hasHeaders); if (hasHeaders) writer.WriteLine(string.Join(",", _reader.GetFieldHeaders())); int rowIndex = 0; while (_reader.ReadNextRecord()) { if (rowIndex > 0 && rowIndex % 100 == 0 && action != null) { if (!action(string.Format("正在處理第{0}行...", rowIndex))) break; } string[] objs = new string[_reader.FieldCount]; for (int index = 0; index < objs.Length; index++) { if (_reader[index] != null && Array.Exists(columns, (column) => { return Convert.ToInt32(column) == index; })) { byte[] bytes = rsa.Decrypt(Convert.FromBase64String(_reader[index].ToString()), false); objs[index] = Encoding.UTF8.GetString(bytes); } else objs[index] = _reader[index]; } writer.WriteLine(string.Join(",", objs)); rowIndex++; } reader.Close(); writer.Close(); return rowIndex; } } } } public static void Decrypt(string privatekey, string src, string dest, int[] columns) { Decrypt(privatekey, src, dest, columns, null); } } }
using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; namespace MY.Cipher { interface ICipher { //string Key { get; set; } string Encrypt(string val); string Decrypt(string val); DataTable Encrypt(DataTable dt, int[] columnIndexs); DataTable Decrypt(DataTable dt, int[] columnIndexs); int Encrypt(string src, string dest, int[] columns, Predicate<string> action); int Decrypt(string src, string dest, int[] columns, Predicate<string> action); int Encrypt(string src, string dest, bool hasHeaders, int[] columns, Predicate<string> action); int Decrypt(string src, string dest, bool hasHeaders, int[] columns, Predicate<string> action); //void GeneralKeyIV(string keyStr, out byte[] key, out byte[] iv); } }
AES
using MY.Cipher.Csv; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; namespace MY.Cipher { public class AESCode : ICipher { public string Key { get; set; } public string Encrypt(string val) { if (string.IsNullOrEmpty(val)) return null; #if CSP using (AesCryptoServiceProvider des = new AesCryptoServiceProvider()) #else using (AesManaged des = new AesManaged()) #endif { byte[] inputByteArray = Encoding.UTF8.GetBytes(val); byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); byte[] bytes = (byte[])ms.ToArray(); return Convert.ToBase64String(bytes); } } } } public string Decrypt(string val) { if (string.IsNullOrEmpty(val)) return null; #if CSP using (AesCryptoServiceProvider des = new AesCryptoServiceProvider()) #else using (AesManaged des = new AesManaged()) #endif { byte[] inputByteArray = Convert.FromBase64String(val); byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.UTF8.GetString(ms.ToArray()); } } } } public DataTable Encrypt(DataTable dt, int[] columnIndexs) { if (dt == null) return null; DataTable result = dt.Clone(); #if CSP using (AesCryptoServiceProvider des = new AesCryptoServiceProvider()) #else using (AesManaged des = new AesManaged()) #endif { byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; ICryptoTransform transform = des.CreateEncryptor(); foreach (DataRow dr in dt.Rows) { object[] objs = dr.ItemArray; foreach (int index in columnIndexs) { if (objs[index] != null && objs[index] != DBNull.Value) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) { byte[] src = Encoding.UTF8.GetBytes(objs[index].ToString()); if (src.Length == 0) continue; cs.Write(src, 0, src.Length); cs.FlushFinalBlock(); byte[] bytes = (byte[])ms.ToArray(); objs[index] = Convert.ToBase64String(bytes); } } } } result.Rows.Add(objs); } } return result; } public DataTable Decrypt(DataTable dt, int[] columnIndexs) { if (dt == null) return null; DataTable result = dt.Clone(); #if CSP using (AesCryptoServiceProvider des = new AesCryptoServiceProvider()) #else using (AesManaged des = new AesManaged()) #endif { byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; ICryptoTransform transform = des.CreateDecryptor(); foreach (DataRow dr in dt.Rows) { object[] objs = dr.ItemArray; foreach (int index in columnIndexs) { if (objs[index] != null && objs[index] != DBNull.Value) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) { byte[] src = Convert.FromBase64String(objs[index].ToString()); if (src.Length == 0) continue; cs.Write(src, 0, src.Length); cs.FlushFinalBlock(); objs[index] = Encoding.UTF8.GetString(ms.ToArray()); } } } } result.Rows.Add(objs); } } return result; } public int Encrypt(string src, string dest, int[] columns, Predicate<string> action) { return Encrypt(src, dest, true, columns, action); } public int Decrypt(string src, string dest, int[] columns, Predicate<string> action) { return Decrypt(src, dest, true, columns, action); } public int Encrypt(string src, string dest, bool hasHeaders, int[] columns, Predicate<string> action) { #if CSP using (AesCryptoServiceProvider des = new AesCryptoServiceProvider()) #else using (AesManaged des = new AesManaged()) #endif { byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; ICryptoTransform transform = des.CreateEncryptor(); using (TextReader reader = new StreamReader(src, Encoding.Default)) { using (TextWriter writer = new StreamWriter(dest, false, Encoding.Default)) { CsvReader _reader = new CsvReader(reader, hasHeaders); if (hasHeaders) writer.WriteLine(string.Join(",", _reader.GetFieldHeaders())); int rowIndex = 0; while (_reader.ReadNextRecord()) { if (rowIndex > 0 && rowIndex % 100 == 0 && action != null) { if (!action(string.Format("正在處理第{0}行...", rowIndex))) break; } string[] objs = new string[_reader.FieldCount]; for (int index = 0; index < objs.Length; index++) { if (_reader[index] != null && Array.Exists(columns, (column) => { return Convert.ToInt32(column) == index; })) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) { byte[] _bytes = Encoding.UTF8.GetBytes(_reader[index].ToString()); if (_bytes.Length == 0) continue; cs.Write(_bytes, 0, _bytes.Length); cs.FlushFinalBlock(); byte[] bytes = (byte[])ms.ToArray(); objs[index] = Convert.ToBase64String(bytes); } } } else objs[index] = _reader[index]; } writer.WriteLine(string.Join(",", objs)); rowIndex++; } reader.Close(); writer.Close(); return rowIndex; } } } } public int Decrypt(string src, string dest, bool hasHeaders, int[] columns, Predicate<string> action) { #if CSP using (AesCryptoServiceProvider des = new AesCryptoServiceProvider()) #else using (AesManaged des = new AesManaged()) #endif { byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; ICryptoTransform transform = des.CreateDecryptor(); using (TextReader reader = new StreamReader(src, Encoding.Default)) { using (TextWriter writer = new StreamWriter(dest, false, Encoding.Default)) { CsvReader _reader = new CsvReader(reader, hasHeaders); if (hasHeaders) writer.WriteLine(string.Join(",", _reader.GetFieldHeaders())); int rowIndex = 0; while (_reader.ReadNextRecord()) { if (rowIndex > 0 && rowIndex % 100 == 0 && action != null) { if (!action(string.Format("正在處理第{0}行...", rowIndex))) break; } string[] objs = new string[_reader.FieldCount]; for (int index = 0; index < objs.Length; index++) { if (_reader[index] != null && Array.Exists(columns, (column) => { return Convert.ToInt32(column) == index; })) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) { byte[] _bytes = Convert.FromBase64String(_reader[index].ToString()); if (_bytes.Length == 0) continue; cs.Write(_bytes, 0, _bytes.Length); cs.FlushFinalBlock(); objs[index] = Encoding.UTF8.GetString(ms.ToArray()); } } } else objs[index] = _reader[index]; } writer.WriteLine(string.Join(",", objs)); rowIndex++; } reader.Close(); writer.Close(); return rowIndex; } } } } public void GeneralKeyIV(string keyStr, out byte[] key, out byte[] iv) { byte[] bytes = Encoding.UTF8.GetBytes(keyStr); key = SHA256Managed.Create().ComputeHash(bytes); iv = MD5.Create().ComputeHash(bytes); } } }
DES
using MY.Cipher.Csv; using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; namespace MY.Cipher { public class DESCode : ICipher { public string Key { get; set; } public string Encrypt(string val) { if (string.IsNullOrEmpty(val)) return null; using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] inputByteArray = Encoding.UTF8.GetBytes(val); byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); byte[] bytes = (byte[])ms.ToArray(); return Convert.ToBase64String(bytes); } } } } public string Decrypt(string val) { if (string.IsNullOrEmpty(val)) return null; using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] inputByteArray = Convert.FromBase64String(val); byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write)) { cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); return Encoding.UTF8.GetString(ms.ToArray()); } } } } public DataTable Encrypt(DataTable dt, int[] columnIndexs) { if (dt == null) return null; DataTable result = dt.Clone(); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; ICryptoTransform transform = des.CreateEncryptor(); foreach (DataRow dr in dt.Rows) { object[] objs = dr.ItemArray; foreach (int index in columnIndexs) { if (objs[index] != null && objs[index] != DBNull.Value) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) { byte[] src = Encoding.UTF8.GetBytes(objs[index].ToString()); if (src.Length == 0) continue; cs.Write(src, 0, src.Length); cs.FlushFinalBlock(); byte[] bytes = (byte[])ms.ToArray(); objs[index] = Convert.ToBase64String(bytes); } } } } result.Rows.Add(objs); } } return result; } public DataTable Decrypt(DataTable dt, int[] columnIndexs) { if (dt == null) return null; DataTable result = dt.Clone(); using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; ICryptoTransform transform = des.CreateDecryptor(); foreach (DataRow dr in dt.Rows) { object[] objs = dr.ItemArray; foreach (int index in columnIndexs) { if (objs[index] != null && objs[index] != DBNull.Value) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) { byte[] src = Convert.FromBase64String(objs[index].ToString()); if (src.Length == 0) continue; cs.Write(src, 0, src.Length); cs.FlushFinalBlock(); objs[index] = Encoding.UTF8.GetString(ms.ToArray()); } } } } result.Rows.Add(objs); } } return result; } public int Encrypt(string src, string dest, int[] columns, Predicate<string> action) { return Encrypt(src, dest, true, columns, action); } public int Decrypt(string src, string dest, int[] columns, Predicate<string> action) { return Decrypt(src, dest, true, columns, action); } public int Encrypt(string src, string dest, bool hasHeaders, int[] columns, Predicate<string> action) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; ICryptoTransform transform = des.CreateEncryptor(); using (TextReader reader = new StreamReader(src, Encoding.Default)) { using (TextWriter writer = new StreamWriter(dest, false, Encoding.Default)) { CsvReader _reader = new CsvReader(reader, hasHeaders); if (hasHeaders) writer.WriteLine(string.Join(",", _reader.GetFieldHeaders())); int rowIndex = 0; while (_reader.ReadNextRecord()) { if (rowIndex > 0 && rowIndex % 100 == 0 && action != null) { if (!action(string.Format("正在處理第{0}行...", rowIndex))) break; } string[] objs = new string[_reader.FieldCount]; for (int index = 0; index < objs.Length; index++) { if (_reader[index] != null && Array.Exists(columns, (column) => { return Convert.ToInt32(column) == index; })) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) { byte[] _bytes = Encoding.UTF8.GetBytes(_reader[index].ToString()); if (_bytes.Length == 0) continue; cs.Write(_bytes, 0, _bytes.Length); cs.FlushFinalBlock(); byte[] bytes = (byte[])ms.ToArray(); objs[index] = Convert.ToBase64String(bytes); } } } else objs[index] = _reader[index]; } writer.WriteLine(string.Join(",", objs)); rowIndex++; } reader.Close(); writer.Close(); return rowIndex; } } } } public int Decrypt(string src, string dest, bool hasHeaders, int[] columns, Predicate<string> action) { using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) { byte[] _key; byte[] _iv; GeneralKeyIV(this.Key, out _key, out _iv); des.Key = _key; des.IV = _iv; ICryptoTransform transform = des.CreateDecryptor(); using (TextReader reader = new StreamReader(src, Encoding.Default)) { using (TextWriter writer = new StreamWriter(dest, false, Encoding.Default)) { CsvReader _reader = new CsvReader(reader, hasHeaders); if (hasHeaders) writer.WriteLine(string.Join(",", _reader.GetFieldHeaders())); int rowIndex = 0; while (_reader.ReadNextRecord()) { if (rowIndex > 0 && rowIndex % 100 == 0 && action != null) { if (!action(string.Format("正在處理第{0}行...", rowIndex))) break; } string[] objs = new string[_reader.FieldCount]; for (int index = 0; index < objs.Length; index++) { if (_reader[index] != null && Array.Exists(columns, (column) => { return Convert.ToInt32(column) == index; })) { using (MemoryStream ms = new MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write)) { byte[] _bytes = Convert.FromBase64String(_reader[index].ToString()); if (_bytes.Length == 0) continue; cs.Write(_bytes, 0, _bytes.Length); cs.FlushFinalBlock(); objs[index] = Encoding.UTF8.GetString(ms.ToArray()); } } } else objs[index] = _reader[index]; } writer.WriteLine(string.Join(",", objs)); rowIndex++; } reader.Close(); writer.Close(); return rowIndex; } } } } public void GeneralKeyIV(string keyStr, out byte[] key, out byte[] iv) { byte[] bytes = Encoding.UTF8.GetBytes(keyStr); byte[] _key = SHA1.Create().ComputeHash(bytes); key = new byte[8]; iv = new byte[8]; for (int i = 0; i < 8; i++) { iv[i] = _key[i]; key[i] = _key[i]; } } } }