c# AES128 加解密算法
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; using System.IO; using System.Security.Cryptography; namespace AESEncoder { class Program { static void Main(string[] args) { var encoder = new AESEncoder("@111>111@222>222@333>333"); var a = encoder.EncryptString("abc"); var b = encoder.DecryptString(a); } /// <summary> /// DESEncrypt加密解密算法。 /// </summary> public class AESEncoder { public AESEncoder(string key) { this.Key = key; } /// <summary> /// 对称加密解密的密钥 /// </summary> public string Key { get; private set; } /// <summary> /// DES加密 /// </summary> /// <param name="encryptString"></param> /// <returns></returns> public string EncryptString(string data) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(this.Key); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(data); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return Convert.ToBase64String(resultArray, 0, resultArray.Length); } /// <summary> /// DES解密 /// </summary> /// <param name="decryptString"></param> /// <returns></returns> public string DecryptString(string data) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(this.Key); byte[] toEncryptArray = Convert.FromBase64String(data); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length); return UTF8Encoding.UTF8.GetString(resultArray); } public bool EncryptFile(string filepath) { try { var bytes = File.ReadAllBytes(filepath); File.WriteAllBytes(filepath, Encoding.UTF8.GetBytes(this.EncryptString(Convert.ToBase64String(bytes)))); return true; } catch (Exception ex) { Debug.Print(ex.ToString()); return false; } } public bool DecryptFile(string filepath) { try { var bytes = File.ReadAllBytes(filepath); File.WriteAllBytes(filepath, Convert.FromBase64String(this.DecryptString(Encoding.UTF8.GetString(bytes)))); return true; } catch (Exception ex) { Debug.Print(ex.ToString()); return false; } } } } }
/// <summary> /// AES256加密解密算法。 /// </summary> public class AESEncoder { public AESEncoder(string key) { this.Key = key; } /// <summary> /// 对称加密解密的密钥 /// </summary> public string Key { get; private set; } /// <summary> /// DES加密 /// </summary> /// <param name="encryptString"></param> /// <returns></returns> public byte[] EncryptBytes(byte[] data) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(this.Key); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateEncryptor(); byte[] resultArray = cTransform.TransformFinalBlock(data, 0, data.Length); return resultArray; } /// <summary> /// DES解密 /// </summary> /// <param name="decryptString"></param> /// <returns></returns> public byte[] DecryptBytes(byte[] data) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(this.Key); RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(data, 0, data.Length); return resultArray; } }
桂棹兮兰桨,击空明兮溯流光。