CSharp中字符串加密和解密(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
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
123
124
125
126
127
128
129
130
131
namespace Z.Utils.Common
{
using System;
using System.Security.Cryptography;
using System.Text;

/// <summary>
/// AES加密
/// </summary>
public class AesHelper
{
public static string QrcodeLoginKey = "qazwsxedcrfvtgby";

/// <summary>
/// AES 加密
/// </summary>
/// <returns>
/// Encrypted 16 hex string
/// </returns>
public static string AesEncrypt(string data, string key = "")
{
if (string.IsNullOrWhiteSpace(key))
{
key = QrcodeLoginKey.PadRight(16, '0');
}

// 256-AES key
byte[] keyArray = Encoding.UTF8.GetBytes(key);
byte[] toEncryptArray = Encoding.UTF8.GetBytes(data);
RijndaelManaged rDel = new RijndaelManaged
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(
toEncryptArray,
0,
toEncryptArray.Length
);
return BytesToHexString(resultArray);
}

/// <summary>
/// AES 解密
/// </summary>
/// <param name="hexString">
/// Encrypted 16 hex string
/// </param>
/// <param name="key"></param>
/// <returns>
/// Decrypted string
/// </returns>
public static string AesDecrypt(string hexString, string key = "")
{
if (string.IsNullOrWhiteSpace(key))
{
key = QrcodeLoginKey.PadRight(16, '0');
}

// 256-AES key
byte[] keyArray = Encoding.UTF8.GetBytes(key);
byte[] toEncryptArray = HexStringToBytes(hexString);
RijndaelManaged rDel = new RijndaelManaged
{
Key = keyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
};
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(
toEncryptArray,
0,
toEncryptArray.Length
);
return Encoding.UTF8.GetString(resultArray);
}

/// <summary>
/// Byte array to convert 16 hex string
/// </summary>
/// <param name="bytes">
/// byte array
/// </param>
/// <returns>
/// 16 hex string
/// </returns>
public static string BytesToHexString(byte[] bytes)
{
StringBuilder returnStr = new StringBuilder();
if (bytes == null)
{
return returnStr.ToString();
}
foreach (byte t in bytes)
{
returnStr.Append(t.ToString("X2"));
}
return returnStr.ToString();
}

/// <summary>
/// 16 hex string converted to byte array
/// </summary>
/// <param name="hexString">
/// 16 hex string
/// </param>
/// <returns>
/// byte array
/// </returns>
public static byte[] HexStringToBytes(string hexString)
{
if (hexString == null || hexString.Equals(""))
{
return null;
}
int length = hexString.Length / 2;
if (hexString.Length % 2 != 0)
{
return null;
}
byte[] d = new byte[length];
for (int i = 0; i < length; i++)
{
d[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
}
return d;
}
}
}