chengkun
2025-06-05 4080b5997b38ca84b3b203c7101dcadb97b76925
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
77
78
79
80
81
82
83
84
85
86
87
88
using System;
using System.Text;
using System.Security.Cryptography;
 
namespace Alipay.EasySDK.Kernel.Util
{
    public class AES
    {
        /// <summary>
        /// 128位全0初始向量
        /// </summary>
        private static readonly byte[] AES_IV = InitIV(16);
 
        /// <summary>
        /// AES加密
        /// </summary>
        /// <param name="plainText">明文</param>
        /// <param name="key">对称密钥</param>
        /// <returns>密文</returns>
        public static string Encrypt(string plainText, string key)
        {
            try
            {
                byte[] keyBytes = Convert.FromBase64String(key);
                byte[] plainBytes = AlipayConstants.DEFAULT_CHARSET.GetBytes(plainText); ;
 
                RijndaelManaged rijndatel = new RijndaelManaged
                {
                    Key = keyBytes,
                    Mode = CipherMode.CBC,
                    Padding = PaddingMode.PKCS7,
                    IV = AES_IV
                };
 
                ICryptoTransform transform = rijndatel.CreateEncryptor(rijndatel.Key, rijndatel.IV);
                byte[] cipherBytes = transform.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
                return Convert.ToBase64String(cipherBytes);
            }
            catch (Exception e)
            {
                throw new Exception("AES加密失败,plainText=" + plainText +
                    ",keySize=" + key.Length + "。" + e.Message, e);
            }
        }
 
        /// <summary>
        /// AES解密
        /// </summary>
        /// <param name="cipherText">密文</param>
        /// <param name="key">对称密钥</param>
        /// <returns>明文</returns>
        public static string Decrypt(string cipherText, string key)
        {
            try
            {
                byte[] keyBytes = Convert.FromBase64String(key);
                byte[] cipherBytes = Convert.FromBase64String(cipherText);
 
                RijndaelManaged rijndatel = new RijndaelManaged
                {
                    Key = keyBytes,
                    Mode = CipherMode.CBC,
                    Padding = PaddingMode.PKCS7,
                    IV = AES_IV
                };
 
                ICryptoTransform transform = rijndatel.CreateDecryptor(rijndatel.Key, rijndatel.IV);
                byte[] plainBytes = transform.TransformFinalBlock(cipherBytes, 0, cipherBytes.Length);
                return AlipayConstants.DEFAULT_CHARSET.GetString(plainBytes);
            }
            catch (Exception e)
            {
                throw new Exception("AES解密失败,ciphertext=" + cipherText +
                    ",keySize=" + key.Length + "。" + e.Message, e);
            }
        }
 
        private static byte[] InitIV(int blockSize)
        {
            byte[] iv = new byte[blockSize];
            for (int i = 0; i < blockSize; ++i)
            {
                iv[i] = 0x0;
            }
            return iv;
        }
    }
}