Moli
2022-04-15 22477f0eca904ec628ab59cd749f959af35c1439
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
 
namespace Helpers
{
    /// <summary>
    /// ASE加密解密工具
    /// </summary>
    public class ASEEncryption
    {
        /// <summary>
        /// ASE加密
        /// </summary>
        /// <param name="Plaintext">待加密的字符串</param>
        /// <param name="key">混淆参数</param>
        /// <param name="IV">偏移量</param>
        /// <returns></returns>
        public string Encrypt(String Plaintext, string key="abcd", String IV="dddd")
        {
            return EncrypText(GetPlainArray(Plaintext), GetKeyArray(key), GetIvByte(IV));
        }
 
        /// <summary>
        /// ASE解密
        /// </summary>
        /// <param name="Ciphertext">待解密的字符串</param>
        /// <param name="key">加密时设置的混淆参数</param>
        /// <param name="IV">加密时设置偏移量</param>
        /// <returns></returns>
        public string Decrypt(string Ciphertext, string key = "abcd", String IV = "dddd")
        {
            return Decryptext(GetCipherArray(Ciphertext), GetKeyArray(key), GetIvByte(IV));
        }
 
 
        //Iv从string类型转化为byte[]类型
        //初始化向量无疑就和口令加密过程中使用的掩值道理类同。
        private Byte[] GetIvByte(String iv)
        {
            // 16*8 = 128 
            byte[] IVArray = new byte[16];
            //建议不用default
            Array.Copy(Encoding.UTF32.GetBytes(iv.PadRight(IVArray.Length, 'a')), IVArray, IVArray.Length);
            return IVArray;
        }
 
        //K 从string类型转化为byte[]类型
        private Byte[] GetKeyArray(String key)
        {
            //24*8 = 192  16*8 = 128  32*8 = 256 
            byte[] KeyArray = new byte[32];
            Array.Copy(Encoding.UTF32.GetBytes(key.PadRight(KeyArray.Length, 'a')), KeyArray, KeyArray.Length);
            return KeyArray;
        }
 
        //把要加密的内容 转化为byte类型数据
        private byte[] GetPlainArray(String Plaintext)
        {
            Byte[] TextArray = Encoding.UTF32.GetBytes(Plaintext);
            return TextArray;
        }
        //把要解密的内容 转化为byte类型数据
        private byte[] GetCipherArray(String Ciphertext)
        {
            Byte[] Cipher = Convert.FromBase64String(Ciphertext);
            return Cipher;
        }
 
        //加密
        private string EncrypText(byte[] Plaintext, byte[] key, byte[] IV)
        {
            try
            {
                // RijndaelManaged rijndael = new RijndaelManaged(); 
                Aes aes = new AesManaged();
                //获取或设置对称算法的密钥
                aes.Key = key;
                //获取或设置对称算法的初始化向量 
                aes.IV = IV;
                aes.BlockSize = 128; //运算模式
                aes.Mode = CipherMode.ECB;
                //块补充:要填充的数量= 128 - (数据Plaintext长度 mod 块长度)
                aes.Padding = PaddingMode.ANSIX923;
                // 用当前的 Key 属性和初始化向量 (IV) 创建对称加密器对象。
                // iCryptoTransform 定义加密转换的基本操作。
                ICryptoTransform iCryptoTransform = aes.CreateEncryptor(key, IV);
                //转换指定的字节数组的指定的区域。
                byte[] resultArray = iCryptoTransform.TransformFinalBlock(Plaintext, 0, Plaintext.Length);
                return Convert.ToBase64String(resultArray);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
 
        //解密
        private string Decryptext(byte[] Ciphertext, byte[] key, byte[] IV)
        {
            try
            {
                Aes aes = new AesManaged();
                aes.Key = key;
                aes.IV = IV;
                aes.BlockSize = 128;
                aes.Mode = CipherMode.ECB;
                aes.Padding = PaddingMode.ANSIX923;
                ICryptoTransform cTransform = aes.CreateDecryptor(key, IV);
                byte[] resultArray = cTransform.TransformFinalBlock(Ciphertext, 0, Ciphertext.Length);
                return Encoding.UTF32.GetString(resultArray);
            }
            catch (Exception e)
            {
                return e.ToString();
            }
        }
    }
}