大白话理解:将明眼能看到的字符给用另一种读不懂的语言给翻译,就像是摩斯密码……就像base64加密,都有异曲同工之妙。
建一个新的类(这里放了aes加密解密的方法)
public static class AesPassword{/// <summary>/// Aes加解密钥必须32位/// </summary>public static string AesKey = "mima"; // 密钥/// <summary>/// 获取Aes32位密钥/// </summary>/// <param name="key">Aes密钥字符串</param>/// <returns>Aes32位密钥</returns>public static byte[] GetAesKey(string key){if (string.IsNullOrEmpty(key)){throw new ArgumentNullException("key", "Aes密钥不能为空");}if (key.Length < 32){// 不足32补全key = key.PadRight(32, '0');}if (key.Length > 32){key = key.Substring(0, 32);}return Encoding.UTF8.GetBytes(key);}/// AES 加密/// </summary>/// <param name="str">明文(待加密)</param>/// <param name="key">密文</param>/// <returns></returns>public static string AesEncrypt(string str, string key){if (string.IsNullOrEmpty(str))return null;Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);RijndaelManaged rm = new RijndaelManaged{Key = GetAesKey(key),Mode = CipherMode.ECB,Padding = PaddingMode.PKCS7};ICryptoTransform cTransform = rm.CreateEncryptor();Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);return Convert.ToBase64String(resultArray, 0, resultArray.Length);}/// <summary>/// AES 解密/// </summary>/// <param name="str">明文(待解密)</param>/// <param name="key">密文</param>/// <returns></returns>public static string AesDecrypt(string str, string key){if (string.IsNullOrEmpty(str))return null;Byte[] toEncryptArray = Convert.FromBase64String(str);RijndaelManaged rm = new RijndaelManaged{Key = GetAesKey(key),Mode = CipherMode.ECB,Padding = PaddingMode.PKCS7};ICryptoTransform cTransform = rm.CreateDecryptor();Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);return Encoding.UTF8.GetString(resultArray);}
/// <summary>
/// Aes加密
/// </summary>
/// <param name="source">源字符串</param>
/// <param name="key">aes密钥,长度必须32位</param>
/// <returns>加密后的字符串</returns>
public static string EncryptAes(string source, string key){using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider()){aesProvider.Key = GetAesKey(key);aesProvider.Mode = CipherMode.ECB;aesProvider.Padding = PaddingMode.PKCS7;using (ICryptoTransform cryptoTransform = aesProvider.CreateEncryptor()){byte[] inputBuffers = Encoding.UTF8.GetBytes(source);byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);aesProvider.Clear();aesProvider.Dispose();return Convert.ToBase64String(results, 0, results.Length);}}}/// <summary>/// Aes解密/// </summary>/// <param name="source">源字符串</param>/// <param name="key">aes密钥,长度必须32位</param>/// <returns>解密后的字符串</returns>public static string DecryptAes(string source, string key){using (AesCryptoServiceProvider aesProvider = new AesCryptoServiceProvider()){aesProvider.Key = GetAesKey(key);aesProvider.Mode = CipherMode.ECB;aesProvider.Padding = PaddingMode.PKCS7;using (ICryptoTransform cryptoTransform = aesProvider.CreateDecryptor()){byte[] inputBuffers = Convert.FromBase64String(source);byte[] results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);aesProvider.Clear();return Encoding.UTF8.GetString(results);}}}}
需要调用的文件
public static void SwitchPassword() {string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $@"config/config.json");//加密//string apn = AesPassword.AesEncrypt("LTAI****LyZ7q9", AesPassword.AesKey);var patternJson = JsonConvert.DeserializeObject<ConfigModel>(File.ReadAllText(path));//解密var _accesskey = AesPassword.AesDecrypt(patternJson.accesskey, AesPassword.AesKey);var _secretkey = AesPassword.AesDecrypt(patternJson.secretkey, AesPassword.AesKey);ConfigIni.AccessKey = _accesskey;ConfigIni.SecretKey = _secretkey;} public class ConfigModel{public string accesskey { get; set; }public string secretkey { get; set; }}