C# 解密Java的密文报错 Padding is invalid and cannot be removed

2021-05-08 10:30

阅读:515

标签:ice   mba   base   string   hide   moved   manage   class   加密   

技术图片技术图片
#region AES



        /// 
        /// 加密
        /// 
        /// 
        /// 
        /// 
        protected virtual string EcryptHexString2String(string content, string password)
        {
            var ecrypt = EcryptAES(content, password);
            var result = Bytes2HexString(ecrypt);
            return result;
        }
        /// 
        /// 得到加密之后的16位进制的字符串
        /// 
        /// 
        /// 
        protected virtual string Bytes2HexString(string str)
        {
            var bytes = Convert.FromBase64String(str);
            var result = "";
            foreach (var i in bytes)
            {
                result = string.IsNullOrWhiteSpace(result) ? $"{i.ToString("X2")}" : $"{result}{i.ToString("X2")}";
            }

            return result;
        }

        /// 
        /// 加密
        /// 
        /// 
        /// 
        /// 
        protected virtual string EcryptAES(string content, string password)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged
            {
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
                KeySize = 128,
                BlockSize = 128
            };
            byte[] pwdBytes = getKey(password);
            byte[] keyBytes = new byte[16];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length)
                len = keyBytes.Length;
            Array.Copy(pwdBytes, keyBytes, len);
            rijndaelCipher.Key = keyBytes;
            ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
            byte[] plainText = Encoding.UTF8.GetBytes(content);
            byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
            return Convert.ToBase64String(cipherBytes);
        }
        /// 
        /// 解密
        /// 
        /// 
        /// 
        /// 
        protected virtual string DecryptHexString2String(string content, string password)
        {
            if (string.IsNullOrEmpty(content))
                return "";
            var bytes = HexString2Bytes(content);
            var result = DecryptAes(Convert.ToBase64String(bytes), password);
            return result;
        }
        /// 
        /// 解密
        /// 
        /// 
        /// 
        /// 
        protected virtual string DecryptAes(string input, string key)
        {
            var encryptedBytes = Convert.FromBase64String(input);
            byte[] pwdBytes = getKey(key);
            byte[] keyBytes = new byte[16];
            int len = pwdBytes.Length;
            if (len > keyBytes.Length) len = keyBytes.Length;
            Array.Copy(pwdBytes, keyBytes, len);
            RijndaelManaged rijndael = new RijndaelManaged
            {
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7,
                KeySize = 128,
                BlockSize = 128,
                Key = keyBytes
            };
            ICryptoTransform transform = rijndael.CreateDecryptor();
            byte[] plainText = transform.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
            return Encoding.UTF8.GetString(plainText);
        }
        /// 
        /// 得到密钥
        /// 
        /// 
        /// 
        public virtual byte[] getKey(string password)
        {
            try
            {
                byte[] key = Encoding.UTF8.GetBytes(password);
                using (var st = new SHA1CryptoServiceProvider())
                {
                    using (var nd = new SHA1CryptoServiceProvider())
                    {
                        var rd = nd.ComputeHash(st.ComputeHash(key));
                        byte[] keyArray = rd.Take(16).ToArray();
                        return keyArray;
                    }
                }
            }
            catch (Exception)
            {

            }
            throw new Exception("初始化密钥出现异常");
        }
        /// 
        /// 16进制转换2进制
        /// 
        /// 
        /// 
        protected virtual byte[] HexString2Bytes(string content)
        {
            if (string.IsNullOrEmpty(content))
                return null;

            var len = content.Length / 2;
            var result = new byte[len];
            for (var i = 0; i )
            {
                result[i] = Convert.ToByte(content.Substring(i * 2, 2), 16);
            }
            return result;
        }

        #endregion
View Code

我的业务中加了个把加密后的内容进行16进制转换,就不改了吧。

 

参考地址:https://blog.csdn.net/qiandeqiande/article/details/102808581?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-10.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-10.nonecase

C# 解密Java的密文报错 Padding is invalid and cannot be removed

标签:ice   mba   base   string   hide   moved   manage   class   加密   

原文地址:https://www.cnblogs.com/CnnBlog/p/13178189.html


评论


亲,登录后才可以留言!