吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1273|回复: 5
收起左侧

[求助] c# AES加解密问题

[复制链接]
WolfAvenue 发表于 2022-3-21 16:05
贴上代码,AES-CBC- PKCS7加密解密, 之前可以的,不知道什么时候就不行了,报错( padding is invalid and cannot be removed.),不知道哪里有bug,各位大佬请看看


[C#] 纯文本查看 复制代码
 public static class CryptoHelper
    {
        /// <summary>
        /// AES 加密
        /// </summary>
        /// <param name="plainText">密文</param>
        /// <param name="Key">加密key</param>
        /// <param name="IV">初始化向量 (字节数组,16字节长度)</param>
        /// <returns></returns>
        public static string AesEncrypt(string plainText, byte[] Key, byte[] IV)
        {

            byte[] encrypted;

            // Create an Aes object
            // with the specified key and IV.
            using (Aes aesAlg = Aes.Create())
            {
                aesAlg.KeySize = 256;
                aesAlg.Key = Key;
                aesAlg.IV = IV;
                aesAlg.Mode = CipherMode.CBC;
                aesAlg.Padding = PaddingMode.PKCS7;
                aesAlg.BlockSize = 128;

                using (ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV))
                {

                    // Create the streams used for encryption.
                    using (MemoryStream msEncrypt = new MemoryStream())
                    {
                        using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                        {
                            using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                            {
                                //Write all data to the stream.
                                swEncrypt.Write(plainText);
                            }
                            encrypted = msEncrypt.ToArray();
                        }
                    }
                }

            }

           
            return ToHex(encrypted);
        }
        /// <summary>
        /// AES 解密
        /// </summary>
        /// <param name="cipherText">密文(字节数组)</param>
        /// <param name="key">加密key(字节数组)</param>
        /// <param name="iv">初始化向量 (字节数组,16字节长度)</param>
        /// <returns></returns>
        public static string AesDecrypt(byte[] cipherText, byte[] key, byte[] iv)
        {
            string plainText = "";
            using (Aes _aes = Aes.Create())
            {
                _aes.KeySize = 256;
                _aes.Key = key;
                _aes.IV = iv;
                _aes.Mode = CipherMode.CBC;
                _aes.Padding = PaddingMode.PKCS7;
                _aes.BlockSize = 128;


                using (ICryptoTransform transform = _aes.CreateDecryptor(_aes.Key, _aes.IV))
                {
                    using (MemoryStream ms = new MemoryStream(cipherText))
                    {
                        using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Read))
                        {
                            using (StreamReader sr = new StreamReader(cs))
                            {
                                plainText = sr.ReadToEnd();
                            }
                        }
                    }
                }

            }

            return plainText;
        }


        public static string ToHex(byte[] _bytes)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < _bytes.Length; i++)
            {
                sb.Append(_bytes[i].ToString("X2"));
            }
            return sb.ToString();
        }


        
        /// <summary>
        /// 字符串转16进制字节数组
        /// </summary>
        /// <param name="hexString"></param>
        /// <returns></returns>
        public static byte[] HexStrToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
            return returnBytes;
        }
    }

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

icysky0605 发表于 2022-3-21 16:42
移除BlockSize这一行就行了

免费评分

参与人数 1吾爱币 +3 热心值 +1 收起 理由
WolfAvenue + 3 + 1 我很赞同!

查看全部评分

 楼主| WolfAvenue 发表于 2022-3-21 17:00
icysky0605 发表于 2022-3-21 16:42
移除BlockSize这一行就行了

这是为什么那,不能设置,我原来就行啊
icysky0605 发表于 2022-3-21 17:11
WolfAvenue 发表于 2022-3-21 17:00
这是为什么那,不能设置,我原来就行啊

我也不清楚,因为我没深入研究过,我以前的代码都没有这一行的
落红护花 发表于 2022-3-21 19:40
这个aes加密出来的字符串是可破解的吧,弱弱的问一句大佬,怎样写死码加密的字符串啊
 楼主| WolfAvenue 发表于 2022-3-22 09:49
落红护花 发表于 2022-3-21 19:40
这个aes加密出来的字符串是可破解的吧,弱弱的问一句大佬,怎样写死码加密的字符串啊

万物皆可破解 ,死码是什么意思?没看懂
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 16:04

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表