项目中比较常用的加密手段
/// <summary> /// ASE_128_ECB_无填充_64Base_加密函数 /// </summary> /// <param name="content">要加密的内容</param> /// <param name="key">一定要16位的密钥</param> /// <returns>加密的字符串(字节)</returns> public static byte[] Func_AES128加密(string content, string key) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(key); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(content); 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); //返回字节数组,可用Convert.FromBase64String()转换为字节 //return Convert.ToBase64String(resultArray, 0, resultArray.Length); return resultArray; } /// <summary> /// ASE_128_ECB_无填充_64Base_解密函数 /// </summary> /// <param name="content">密串(字节)</param> /// <param name="keyArray">密钥(字节)</param> /// <returns>解密后的字符串</returns> public static byte[] Func_AES128解密(byte[] content, byte[] keyArray) #region { RijndaelManaged rDel = new RijndaelManaged(); rDel.Key = keyArray; rDel.Mode = CipherMode.ECB; rDel.Padding = PaddingMode.None; rDel.BlockSize = 128; ICryptoTransform cTransform = rDel.CreateDecryptor(); byte[] resultArray = cTransform.TransformFinalBlock(content, 0, content.Length); return resultArray; } #endregion 使用: byte[] _val1 = Func_AES128加密("Lee", "abcdef1234567890"); byte[] _key = Encoding.UTF8.GetBytes("abcdef1234567890"); byte[] _return1 = Func_AES128解密(_val1, _key); TextBox2.Text = Encoding.UTF8.GetString(_return1);