AES加密和解密

来源1 来源2

 1  {

JsonSerializer serializer = new JsonSerializer();
string[] strArray = { "111","222","333","444","555","666"};
Boy boy = new Boy();
boy.Age = 15;
boy.bir = Convert.ToDateTime("1999-9-9");
boy.Name = "消";
boy.Price = 10.26;
string o = new JavaScriptSerializer().Serialize(boy);//{"Name":"消","Age":15,"Price":10.26,"bir":"\/Date(936806400000)\/"}
string ss = JsonConvert.SerializeObject(boy);//这个的时间的格式与上一个不同:{"Name":"消","Age":15,"Price":10.26,"bir":"1999-09-09T00:00:00"}
string ArrayStr = JsonConvert.SerializeObject(strArray);//["111","222","333","444","555","666"]
string text = o; //明文

 2             //密码
 3             string keys = "1234567890123456";//类似密钥吧要满足16位
 4             string text = "Prozkb";
 5             //加密初始化向量
 6             Console.WriteLine("明文:" + text);
 7             string message = AesEncrypt(text, keys);
 8            
 9             Console.WriteLine("加密结果"+message);
10             string Jiemi = AesDecrypt(message, keys);
11 
12            Console.WriteLine("解密结果" + Jiemi);
13           
14             Console.ReadKey();
15 
16             //string text = "要加密的数据"; //明文
17             //string keys = "1234567891234567";//密钥,要满足16位   
18             //byte[] bytes = Encoding.ASCII.GetBytes(keys);
19             //byte[] encryptBytes = AESEncrypt(text, keys);
20             ////将加密后的密文转换为Base64编码,以便显示,可以查看下结果
21             //Console.WriteLine("明文:" + text);
22             //Console.WriteLine("密文:" + Convert.ToBase64String(encryptBytes));
23             ////解密
24             //byte[] decryptBytes = AESDecrypt(encryptBytes, keys);
25             ////将解密后的结果转换为字符串,也可以将该步骤封装在解密算法中
26             //string result = Encoding.UTF8.GetString(decryptBytes);
27             //Console.WriteLine("解密结果:" + result);
28             //Console.Read(); 
29 
30         }
 1  /// <summary>
 2         /// AES加密
 3         /// </summary>
 4         /// <param name="text">加密字符</param>
 5         /// <param name="password">加密的密码</param>
 6         /// <param name="iv">密钥</param>
 7         /// <returns></returns>
 8         public static string AESENcrypt(string text, string password)
 9         {
10             RijndaelManaged rijndaelCipher = new RijndaelManaged();
11             rijndaelCipher.Mode = CipherMode.CBC;
12             rijndaelCipher.Padding = PaddingMode.PKCS7;
13             rijndaelCipher.KeySize = 128;
14             rijndaelCipher.BlockSize = 128;
15             byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
16             byte[] keyBytes = new byte[16];
17             int len = pwdBytes.Length;
18             if (len > keyBytes.Length) len = keyBytes.Length;
19             System.Array.Copy(pwdBytes, keyBytes, len);
20             rijndaelCipher.Key = keyBytes;
21             rijndaelCipher.IV = new byte[16];
22             ICryptoTransform transform = rijndaelCipher.CreateEncryptor();
23             byte[] plainText = Encoding.UTF8.GetBytes(text);
24             byte[] cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
25             return Convert.ToBase64String(cipherBytes);
26         }
27 
28 
29         /// <summary>
30         /// AES解密
31         /// </summary>
32         /// <param name="text"></param>
33         /// <param name="password"></param>
34         /// <param name="iv"></param>
35         /// <returns></returns>
36         public static string AESDecrypt(string text, string password)
37         {
38             RijndaelManaged rijndaelCipher = new RijndaelManaged();
39             rijndaelCipher.Mode = CipherMode.CBC;
40             rijndaelCipher.Padding = PaddingMode.PKCS7;
41             rijndaelCipher.KeySize = 128;
42             rijndaelCipher.BlockSize = 128;
43             byte[] encryptedData = Convert.FromBase64String(text);
44             byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
45             byte[] keyBytes = new byte[16];
46             int len = pwdBytes.Length;
47             if (len > keyBytes.Length) len = keyBytes.Length;
48             System.Array.Copy(pwdBytes, keyBytes, len);
49             rijndaelCipher.Key = keyBytes;
50             rijndaelCipher.IV = new byte[16];
51             ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
52             byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
53             return Encoding.UTF8.GetString(plainText);
54         }
55 
56        
57 
58         //加密
59         public static string AesEncrypt(string str, string key)
60         {
61             if (string.IsNullOrEmpty(str)) return null;
62             Byte[] toEncryptArray = Encoding.UTF8.GetBytes(str);
63 
64             System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
65             {
66                 Key = Encoding.UTF8.GetBytes(key),
67                 Mode = System.Security.Cryptography.CipherMode.ECB,
68                 Padding = System.Security.Cryptography.PaddingMode.PKCS7
69             };
70 
71             System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateEncryptor();
72             Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
73 
74             return Convert.ToBase64String(resultArray, 0, resultArray.Length);
75         }
76         /// <summary>
77         ///  AES 解密
78         /// </summary>
79         /// <param name="str">明文(待解密)</param>
80         /// <param name="key">密文</param>
81         /// <returns></returns>
82         public static string AesDecrypt(string str, string key)
83         {
84             if (string.IsNullOrEmpty(str)) return null;
85             Byte[] toEncryptArray = Convert.FromBase64String(str);
86 
87             System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged
88             {
89                 Key = Encoding.UTF8.GetBytes(key),
90                 Mode = System.Security.Cryptography.CipherMode.ECB,
91                 Padding = System.Security.Cryptography.PaddingMode.PKCS7
92             };
93 
94             System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
95             Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
96 
97             return Encoding.UTF8.GetString(resultArray);
98         }
 private static byte[] _key1 = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 
        /// <summary>
        /// AES加密算法
        /// </summary>
        /// <param name="plainText">明文字符串</param>
        /// <param name="strKey">密钥</param>
        /// <returns>返回加密后的密文字节数组</returns>
        public static byte[] AESEncrypt(string plainText, string strKey)
        {
            //分组加密算法
            SymmetricAlgorithm des = Rijndael.Create();
            byte[] inputByteArray = Encoding.UTF8.GetBytes(plainText);//得到需要加密的字节数组
            //设置密钥及密钥向量
            des.Key = Encoding.UTF8.GetBytes(strKey);
            des.IV = _key1;
            MemoryStream ms = new MemoryStream();
            CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(inputByteArray, 0, inputByteArray.Length);
            cs.FlushFinalBlock();
            byte[] cipherBytes = ms.ToArray();//得到加密后的字节数组
            cs.Close();
            ms.Close();
            return cipherBytes;
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="cipherText">密文字节数组</param>
        /// <param name="strKey">密钥</param>
        /// <returns>返回解密后的字符串</returns>
        public static byte[] AESDecrypt(byte[] cipherText, string strKey)
        {
            SymmetricAlgorithm des = Rijndael.Create();
            des.Key = Encoding.UTF8.GetBytes(strKey);
            des.IV = _key1;
            byte[] decryptBytes = new byte[cipherText.Length];
            MemoryStream ms = new MemoryStream(cipherText);
            CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Read);
            cs.Read(decryptBytes, 0, decryptBytes.Length);
            cs.Close();
            ms.Close();
            return decryptBytes;
        }

 

posted @ 2018-04-19 14:22  ProZkb  阅读(478)  评论(0编辑  收藏  举报