c#---AES加解密

这段代码展示了如何在C#中使用AES算法进行加密和解密操作。它定义了一个AES类,包含默认密钥和加密模式,提供了静态方法进行加密和解密,使用了RijndaelManaged类并设置了ECB模式和PKCS7填充。
摘要由CSDN通过智能技术生成
public class AES
    {
        /// <summary>
        /// 默认密钥-密钥的长度必须是32
        /// </summary>
        private const string PublicKey = "abcd123456789012abcd123456789012";

        /// <summary>
        /// 默认向量
        /// </summary>
        //private const string Iv = "abcdefghijklmnop";
        /// <summary>  
        /// AES加密  
        /// </summary>  
        /// <param name="str">需要加密字符串</param>  
        /// <returns>加密后字符串</returns>  
        public static String Encrypt(string str)
        {
            return Encrypt(str, PublicKey);
        }

        /// <summary>  
        /// AES解密  
        /// </summary>  
        /// <param name="str">需要解密字符串</param>  
        /// <returns>解密后字符串</returns>  
        public static String Decrypt(string str)
        {
            return Decrypt(str, PublicKey);
        }
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="str">需要加密的字符串</param>
        /// <param name="key">32位密钥</param>
        /// <returns>加密后的字符串</returns>
        public static string Encrypt(string str, string key)
        {
            Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key);
            Byte[] toEncryptArray = System.Text.Encoding.UTF8.GetBytes(str);
            var rijndael = new System.Security.Cryptography.RijndaelManaged();
            rijndael.Key = keyArray;
            rijndael.Mode = System.Security.Cryptography.CipherMode.ECB;
            rijndael.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
           // rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv);
            System.Security.Cryptography.ICryptoTransform cTransform = rijndael.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">32位密钥</param>
        /// <returns>解密后的字符串</returns>
        public static string Decrypt(string str, string key)
        {
            //NLogHelp.InfoLog("开始解密"+ str);
            //NLogHelp.InfoLog("开始解密" + key);
            Byte[] keyArray = System.Text.Encoding.UTF8.GetBytes(key);
           // NLogHelp.InfoLog("keyArray" + keyArray);
            Byte[] toEncryptArray = Convert.FromBase64String(str);
            //NLogHelp.InfoLog("toEncryptArray" + toEncryptArray);
            var rijndael = new System.Security.Cryptography.RijndaelManaged();
            rijndael.Key = keyArray;
            //NLogHelp.InfoLog("解密异常");
            rijndael.Mode = System.Security.Cryptography.CipherMode.ECB;
            //NLogHelp.InfoLog("解密异常"+ rijndael.Mode);
            rijndael.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
            //NLogHelp.InfoLog("解密异常"+ rijndael.Padding);
            //rijndael.IV = System.Text.Encoding.UTF8.GetBytes(Iv);
            
            System.Security.Cryptography.ICryptoTransform cTransform = rijndael.CreateDecryptor();
            Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return System.Text.Encoding.UTF8.GetString(resultArray);
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值