Encryption.cs
5.96 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
using System;
using System.Security.Cryptography;
using System.Text;
using Hh.Mes.Common.log;
namespace Hh.Mes.Common
{
public class Encryption
{
private static string encryptKey = "4h!@w$rng,i#$@x1%)5^3(7*5P31/Ee0";
//默认密钥向量
private static byte[] Keys = { 0x41, 0x72, 0x65, 0x79, 0x6F, 0x75, 0x6D, 0x79, 0x53, 0x6E, 0x6F, 0x77, 0x6D, 0x61, 0x6E, 0x3F };
/// <summary>
/// 加密
/// </summary>
/// <param name="encryptString"></param>
/// <returns></returns>
public static string Encrypt(string encryptString)
{
if (string.IsNullOrEmpty(encryptString))
return string.Empty;
RijndaelManaged rijndaelProvider = new RijndaelManaged();
rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
rijndaelProvider.IV = Keys;
ICryptoTransform rijndaelEncrypt = rijndaelProvider.CreateEncryptor();
byte[] inputData = Encoding.UTF8.GetBytes(encryptString);
byte[] encryptedData = rijndaelEncrypt.TransformFinalBlock(inputData, 0, inputData.Length);
return Convert.ToBase64String(encryptedData);
}
/// <summary>
/// 解密
/// </summary>
/// <param name="decryptString"></param>
/// <returns></returns>
public static string Decrypt(string decryptString)
{
if (string.IsNullOrEmpty(decryptString))
return string.Empty;
try
{
RijndaelManaged rijndaelProvider = new RijndaelManaged();
rijndaelProvider.Key = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 32));
rijndaelProvider.IV = Keys;
ICryptoTransform rijndaelDecrypt = rijndaelProvider.CreateDecryptor();
byte[] inputData = Convert.FromBase64String(decryptString);
byte[] decryptedData = rijndaelDecrypt.TransformFinalBlock(inputData, 0, inputData.Length);
return Encoding.UTF8.GetString(decryptedData);
}
catch
{
return "";
}
}
/// <summary>
/// AES加密
/// </summary>
/// <param name="source"></param>
/// <param name="key">密钥</param>
/// <param name="iv">初始向量</param>
/// <param name="padding">填充模式</param>
/// <param name="mode">加密模式</param>
/// <returns></returns>
public static (bool isOk, string text) AESEncrypt(string source, string key, string iv = "", PaddingMode padding = PaddingMode.Zeros, CipherMode mode = CipherMode.CBC)
{
try
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] textBytes = Encoding.UTF8.GetBytes(source);
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
byte[] useKeyBytes = new byte[16];
byte[] useIvBytes = new byte[16];
Array.Copy(keyBytes, useKeyBytes, keyBytes.Length > useKeyBytes.Length ? useKeyBytes.Length : keyBytes.Length);
Array.Copy(ivBytes, useIvBytes, ivBytes.Length > useIvBytes.Length ? useIvBytes.Length : ivBytes.Length);
Aes aes = System.Security.Cryptography.Aes.Create();
aes.KeySize = 256;//秘钥的大小,以位为单位,128,256等
aes.BlockSize = 128;//支持的块大小
aes.Padding = padding;//填充模式
aes.Mode = mode;
aes.Key = useKeyBytes;
aes.IV = useIvBytes;//初始化向量,如果没有设置默认的16个0
ICryptoTransform cryptoTransform = aes.CreateEncryptor();
byte[] resultBytes = cryptoTransform.TransformFinalBlock(textBytes, 0, textBytes.Length);
return (true, Convert.ToBase64String(resultBytes));
}
catch (Exception ex)
{
return (false, ex.Message);
}
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="source"></param>
/// <param name="key">密钥</param>
/// <param name="iv">初始向量</param>
/// <param name="padding">填充模式</param>
/// <param name="mode">加密模式</param>
/// <returns></returns>
public static (bool isOk, string text) AESDecrypt(string source, string key, string iv = "", PaddingMode padding = PaddingMode.Zeros, CipherMode mode = CipherMode.CBC)
{
try
{
byte[] textBytes = Convert.FromBase64String(source);
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
byte[] ivBytes = Encoding.UTF8.GetBytes(iv);
byte[] useKeyBytes = new byte[16];
byte[] useIvBytes = new byte[16];
Array.Copy(keyBytes, useKeyBytes, keyBytes.Length > useKeyBytes.Length ? useKeyBytes.Length : keyBytes.Length);
Array.Copy(ivBytes, useIvBytes, ivBytes.Length > useIvBytes.Length ? useIvBytes.Length : ivBytes.Length);
Aes aes = Aes.Create();
aes.KeySize = 256;//秘钥的大小,以位为单位,128,256等
aes.BlockSize = 128;//支持的块大小
aes.Padding = padding;//填充模式
aes.Mode = mode;
aes.Key = useKeyBytes;
aes.IV = useIvBytes;//初始化向量,如果没有设置默认的16个0
ICryptoTransform decryptoTransform = aes.CreateDecryptor();
byte[] resultBytes = decryptoTransform.TransformFinalBlock(textBytes, 0, textBytes.Length);
return (true, Encoding.UTF8.GetString(resultBytes).Replace("\0", ""));
}
catch (Exception ex)
{
Log4NetHelper.Instance.Error("【AESDecrypt】" + ex.Message);
return (false, ex.Message);
}
}
}
}