C#实现的AES加密解密完整实例

时间:2022-02-02 15:24:31

本文实例讲述了C#实现的AES加密解密。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/******************************************************************
 * 创建人:HTL
 * 说明:C# 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);
 }
 /// <summary>
 /// AES加密
 /// </summary>
 /// <param name="text">加密字符</param>
 /// <param name="password">加密的密码</param>
 /// <param name="iv">密钥</param>
 /// <returns></returns>
 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);
 }
 /// <summary>
 /// AES解密
 /// </summary>
 /// <param name="text"></param>
 /// <param name="password"></param>
 /// <param name="iv"></param>
 /// <returns></returns>
 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);
 }
}

希望本文所述对大家C#程序设计有所帮助。