微信AES-128-CBC加密解密

[TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var key = "cheayeon";
            var iv = "encoding";
            var text = "sb sb ha ha ha";
            //对text,key iv 进行base64加密
            var enKey = Convert.ToBase64String(Encoding.UTF8.GetBytes(key));
            var enIv = Convert.ToBase64String(Encoding.UTF8.GetBytes(iv));
            var enText = Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
            //加密
            var encryp = AesEncryp(enText, enKey,enIv);
            //解密
            var decryp = AesDecryp(encryp, enKey, enIv);


                
        }
        /// 
        /// 加密密text
        /// 王剑锋 2018年7月7日09:58:34
        /// 
        /// 明文
        /// 秘钥,Base64串
        /// 向量,Base64串
        /// 
        public string AesEncryp(string text,string key,string iv)
        {
            var aes = Aes.Create();
            //AES-128-CBC PKCS#7
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = 128;
            aes.BlockSize = 128;
            //Base64解密text,KEY,IV,不够16位填充0
            aes.Key = Padding16(Convert.FromBase64String(key),0);
            aes.IV = Padding16(Convert.FromBase64String(iv),0);
            //获取该算法规则下的加密器
            var encryp = aes.CreateEncryptor();
            //加密之前加一个Base64解密 ,要加密的数据
            var dataBytes = Convert.FromBase64String(text);
            //获取AES-128-CBC PKCS#7 密文
            var enData = encryp.TransformFinalBlock(dataBytes,0, dataBytes.Length);
            return Convert.ToBase64String(enData);
        }
        /// 
        /// 解密text
        /// 王剑锋 2018年7月7日09:58:16
        /// 
        /// 密文
        /// 秘钥,Base64串
        /// 向量,Base64串
        /// 
        public string AesDecryp(string text, string key, string iv)
        {
            var aes = Aes.Create();
            //AES-128-CBC PKCS#7
            aes.Mode = CipherMode.CBC;
            aes.Padding = PaddingMode.PKCS7;
            aes.KeySize = 128;
            aes.BlockSize = 128;
            //Base64解密text,KEY,IV
            aes.Key = Padding16(Convert.FromBase64String(key),0);
            aes.IV =  Padding16(Convert.FromBase64String(iv),0);
            //获取该算法规则下的解密器
            var encryp = aes.CreateDecryptor();
            //加密之前加一个Base64解密 ,要加密的数据
            var dataBytes = Convert.FromBase64String(text);
            //获取AES-128-CBC PKCS#7 明文
            var enData = encryp.TransformFinalBlock(dataBytes, 0, dataBytes.Length);
            return Encoding.UTF8.GetString(enData);
        }
        /// 
        /// 不够16位的整数倍,已指定字符填充
        /// 
        /// 
        /// 
        /// 
        public byte[] Padding16(byte [] array,byte padding)
        {
            int group = (array.Length + 15) / 16;
            var newArray = new byte[group*16];
            for (int i = 0; i < newArray.Length; i++)
            {
                newArray[i] = (i < array.Length ? array[i] : padding);
            }
            return newArray;
        }

  

转载于:https://www.cnblogs.com/chaeyeon/p/9276897.html

你可能感兴趣的:(测试)