首页 > 代码库 > C#使用AES加密
C#使用AES加密
public class AES { /// <summary> /// 获取密钥 必须是32字节 /// </summary> private static string Key { get { return @"ae125efkk4_54eeff444ferfkny6oxi8"; } } /// <summary> /// AES加密 /// </summary> /// <param name="plainStr">明文字符串</param> /// <returns>密文</returns> public static string AESEncrypt(string encryptStr) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(Key); byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(encryptStr); 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); } public static string AESDEncrypt(string encryptStr) { byte[] keyArray = UTF8Encoding.UTF8.GetBytes(Key); byte[] toEncryptArray = Convert.FromBase64String(encryptStr); 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); } }
调用代码:
static void Main(string[] args) { string s = AES.AESEncrypt("我1爱小23小张0-0"); Console.WriteLine(s); Console.WriteLine(AES.AESDEncrypt(s)); Console.Read(); }
C#使用AES加密
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。