永发信息网

C#的 AES 属于哪个类库

答案:4  悬赏:0  手机版
解决时间 2021-11-22 22:24
C#的 AES 属于哪个类库
最佳答案
AES也叫Rijndael加密法。


在C#中是没有AES这个类,所以会出现你所说的错误!


请参考下面的code:
///   
        /// AES加密  
        /// 
  
        /// 被加密的明文  
        /// 密钥  
        /// 向量  
        /// 密文  
        public static String AESEncrypt(String Data, String Key, String Vector)  
        {  
            Byte[] plainBytes = Encoding.UTF8.GetBytes(Data);  
  
            Byte[] bKey = new Byte[32];  
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);  
            Byte[] bVector = new Byte[16];  
            Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);  
  
            Byte[] Cryptograph = null; // 加密后的密文  
  
            Rijndael Aes = Rijndael.Create();  
            try  
            {  
                // 开辟一块内存流  
                using (MemoryStream Memory = new MemoryStream())  
                {  
                    // 把内存流对象包装成加密流对象  
                    using (CryptoStream Encryptor = new CryptoStream(Memory,  
                     Aes.CreateEncryptor(bKey, bVector),  
                     CryptoStreamMode.Write))  
                    {  
                        // 明文数据写入加密流  
                        Encryptor.Write(plainBytes, 0, plainBytes.Length);  
                        Encryptor.FlushFinalBlock();  
  
                        Cryptograph = Memory.ToArray();  
                    }  
                }  
            }  
            catch  
            {  
                Cryptograph = null;  
            }  
  
            return Convert.ToBase64String(Cryptograph);  
        }  
  
        ///   
        /// AES解密  
        /// 
  
        /// 被解密的密文  
        /// 密钥  
        /// 向量  
        /// 明文  
        public static String AESDecrypt(String Data, String Key, String Vector)  
        {  
            Byte[] encryptedBytes = Convert.FromBase64String(Data);  
            Byte[] bKey = new Byte[32];  
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);  
            Byte[] bVector = new Byte[16];  
            Array.Copy(Encoding.UTF8.GetBytes(Vector.PadRight(bVector.Length)), bVector, bVector.Length);  
  
            Byte[] original = null; // 解密后的明文  
  
            Rijndael Aes = Rijndael.Create();  
            try  
            {  
                // 开辟一块内存流,存储密文  
                using (MemoryStream Memory = new MemoryStream(encryptedBytes))  
                {  
                    // 把内存流对象包装成加密流对象  
                    using (CryptoStream Decryptor = new CryptoStream(Memory,  
                    Aes.CreateDecryptor(bKey, bVector),  
                    CryptoStreamMode.Read))  
                    {  
                        // 明文存储区  
                        using (MemoryStream originalMemory = new MemoryStream())  
                        {  
                            Byte[] Buffer = new Byte[1024];  
                            Int32 readBytes = 0;  
                            while ((readBytes = Decryptor.Read(Buffer, 0, Buffer.Length)) > 0)  
                            {  
                                originalMemory.Write(Buffer, 0, readBytes);  
                            }  
  
                            original = originalMemory.ToArray();  
                        }  
                    }  
                }  
            }  
            catch  
            {  
                original = null;  
            }  
            return Encoding.UTF8.GetString(original);  
        }  
  
  
  
        ///   
        /// AES加密(无向量)  
        /// 
  
        /// 被加密的明文  
        /// 密钥  
        /// 密文  
        public static string AESEncrypt(String Data, String Key)  
        {  
            MemoryStream mStream = new MemoryStream();  
            RijndaelManaged aes = new RijndaelManaged();  
  
            byte[] plainBytes = Encoding.UTF8.GetBytes(Data);  
            Byte[] bKey = new Byte[32];  
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);  
  
            aes.Mode = CipherMode.ECB;  
            aes.Padding = PaddingMode.PKCS7;  
            aes.KeySize = 128;  
            //aes.Key = _key;  
            aes.Key = bKey;  
            //aes.IV = _iV;  
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateEncryptor(), CryptoStreamMode.Write);  
            try  
            {  
                cryptoStream.Write(plainBytes, 0, plainBytes.Length);  
                cryptoStream.FlushFinalBlock();  
                return Convert.ToBase64String(mStream.ToArray());  
            }  
            finally  
            {  
                cryptoStream.Close();  
                mStream.Close();  
                aes.Clear();  
            }  
        }  
  
  
        ///   
        /// AES解密(无向量)  
        /// 
  
        /// 被加密的明文  
        /// 密钥  
        /// 明文  
        public static string AESDecrypt(String Data, String Key)  
        {  
            Byte[] encryptedBytes = Convert.FromBase64String(Data);  
            Byte[] bKey = new Byte[32];  
            Array.Copy(Encoding.UTF8.GetBytes(Key.PadRight(bKey.Length)), bKey, bKey.Length);  
              
            MemoryStream mStream = new MemoryStream(encryptedBytes);  
            //mStream.Write( encryptedBytes, 0, encryptedBytes.Length );  
            //mStream.Seek( 0, SeekOrigin.Begin );  
            RijndaelManaged aes = new RijndaelManaged();  
            aes.Mode = CipherMode.ECB;  
            aes.Padding = PaddingMode.PKCS7;  
            aes.KeySize = 128;  
            aes.Key = bKey;  
            //aes.IV = _iV;  
            CryptoStream cryptoStream = new CryptoStream(mStream, aes.CreateDecryptor(), CryptoStreamMode.Read);  
            try  
            {  
                byte[] tmp = new byte[encryptedBytes.Length + 32];  
                int len = cryptoStream.Read(tmp, 0, encryptedBytes.Length + 32);  
                byte[] ret = new byte[len];  
                Array.Copy(tmp, 0, ret, 0, len);  
                return Encoding.UTF8.GetString(ret);  
            }  
            finally  
            {  
                cryptoStream.Close();  
                mStream.Close();  
                aes.Clear();  
            }  
        }
全部回答
System.Security.Cryptography.Aes.Create()
需要引用dll

System.Security.Cryptography.Aes.Create()
需要引用dll

没有AES加解密类,需要自己写:

using System;
using System.Security.Cryptography;
using System.Text;
using System.IO;
public class Test
{
 public static void Main()
 {
 //密码
 string password="1234567890123456";
 //加密初始化向量
 string iv="  ";
 string message=AESEncrypt("abcdefghigklmnopqrstuvwxyz0123456789",password,iv);
 Console.WriteLine(message);
 message=AESDecrypt("8Z3dZzqn05FmiuBLowExK0CAbs4TY2GorC2dDPVlsn/tP+VuJGePqIMv1uSaVErr",password,iv);
 Console.WriteLine(message);
 }
 /// 
 /// AES加密
 /// 

 /// 加密字符
 /// 加密的密码
 /// 密钥
 /// 
 public static string AESEncrypt(string text, string password, string iv)
 {
 RijndaelManaged rijndaelCipher = new RijndaelManaged();
 rijndaelCipher.Mode = CipherMode.CBC;
 rijndaelCipher.Padding = PaddingMode.PKCS7;
 rijndaelCipher.KeySize = 128;
 rijndaelCipher.BlockSize = 128;
 byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
 byte[] keyBytes = new byte[16];
 int len = pwdBytes.Length;
 if (len > keyBytes.Length) len = keyBytes.Length;
 System.Array.Copy(pwdBytes, keyBytes, len);
 rijndaelCipher.Key = keyBytes;
 byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
 rijndaelCipher.IV = new byte[16];
 ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
 byte[] plainText = Encoding.UTF8.GetBytes(text);
 byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
 return Convert.ToBase64String(cipherBytes);
 }
 /// 
 /// AES解密
 /// 

 /// 
 /// 
 /// 
 /// 
 public static string AESDecrypt(string text, string password, string iv)
 {
 RijndaelManaged rijndaelCipher = new RijndaelManaged();
 rijndaelCipher.Mode = CipherMode.CBC;
 rijndaelCipher.Padding = PaddingMode.PKCS7;
 rijndaelCipher.KeySize = 128;
 rijndaelCipher.BlockSize = 128;
 byte[] encryptedData = Convert.FromBase64String(text);
 byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
 byte[] keyBytes = new byte[16];
 int len = pwdBytes.Length;
 if (len > keyBytes.Length) len = keyBytes.Length;
 System.Array.Copy(pwdBytes, keyBytes, len);
 rijndaelCipher.Key = keyBytes;
 byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
 rijndaelCipher.IV = ivBytes;
 ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
 byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
 return Encoding.UTF8.GetString(plainText);
 }
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
裕兴游戏厅地址有知道的么?有点事想过去!
全国叫江明的有多少人
天锐绿盾,风奥软件,亿赛通,哪个加密软件好
家庭基本情况,包括父母工作情况,家庭人口,经
“呈”字的右边加上一个鲍尔(耳)旁怎么念
7.8.9只需算式
路边商场的停车场交警可以贴罚单吗
格力空调kfr-40w有风就是不制冷
下面的两张五块,有一张是1999年的,1999年的
家族诞生84、85期的中字什么时候会有啊
手机笔画"怕"字怎么打
根据分子大小分离蛋白质的方法有哪些
移门推销用语
男女性的骨盆差别
金智科技地址在什么地方,想过去办事,
推荐资讯
什么时候昼短夜长
如何应对"一带一路"安全风险 王义桅
在两河流域的范围之内,哪一时期的文明具有最
lost和dog的音一样吗
求关于旋涡鸣人的个性签名!如果有好的网名也
潮州绿榕湖畔的小区监控录像查询
2015年农历九月十八适宜进宅吗
获嘉县谁在卖赛克自行车
华硕x399主板有几款
y=4的函数图像有吗
网卡属性中的唤醒魔包开启后,重启电脑又恢复
7000平方厘米等于多少平方分米
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?