C#:AES的加密解密,用于明文加密

大白话理解:将明眼能看到的字符给用另一种读不懂的语言给翻译,就像是摩斯密码……就像base64加密,都有异曲同工之妙。

建一个新的类(这里放了aes加密解密的方法)

public static class AesPassword
    {

        /// 
        /// Aes加解密钥必须32位
        /// 
        public static string AesKey = "mima"; // 密钥
        /// 
        /// 获取Aes32位密钥
        /// 
        /// Aes密钥字符串
        /// Aes32位密钥
        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 加密
 /// 
 /// 明文(待加密)
 /// 密文
 /// 
        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);
        }
        /// 
        ///  AES 解密
        /// 
        /// 明文(待解密)
        /// 密文
        /// 
        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);
        }
/// 
/// Aes加密
/// 
/// 源字符串
/// aes密钥,长度必须32位
/// 加密后的字符串
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);
                }
            }
        }

        /// 
        /// Aes解密
        /// 
        /// 源字符串
        /// aes密钥,长度必须32位
        /// 解密后的字符串
        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; }

        }

C#:AES的加密解密,用于明文加密_第1张图片
C#:AES的加密解密,用于明文加密_第2张图片

验证

C#:AES的加密解密,用于明文加密_第3张图片

你可能感兴趣的:(C#,c#,密码加密,AES)