-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCryptAESCore.cs
More file actions
120 lines (109 loc) · 3.9 KB
/
CryptAESCore.cs
File metadata and controls
120 lines (109 loc) · 3.9 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace CoreConsole
{
public class CryptAESCore
{
public static void Main(string[] args)
{
string encryMsg = "123456789";
//获得加密字符串
string encryData = AesEncryptText(encryMsg);
Console.WriteLine(encryData);
//获得解密字符串
Console.WriteLine(AesDecryptText(encryData));
Console.ReadLine();
}
#region AES加解密
//key16 128加密,key24 192加密,key32 256加密
private static string key = "yunzhangcaijing$";
/// <summary>
/// AES加密
/// </summary>
/// <param name="input"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string AesEncryptText(string input)
{
byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(input);
byte[] passwordBytes = Encoding.UTF8.GetBytes(key);
byte[] bytesEncrypted = AESEncryptBytes(bytesToBeEncrypted, passwordBytes);
string result = Convert.ToBase64String(bytesEncrypted);
return result;
}
public static byte[] AESEncryptBytes(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
using (var ms = new MemoryStream())
{
using (var AES = Aes.Create())
{
AES.KeySize = 128;
AES.BlockSize = 128;
AES.Mode = CipherMode.ECB;
AES.Padding = PaddingMode.PKCS7;
AES.Key = passwordBytes;
AES.IV = new byte[16];
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(),
CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Dispose();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="input"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string AesDecryptText(string input)
{
try
{
byte[] bytesToBeDecrypted = Convert.FromBase64String(input);
byte[] passwordBytes = Encoding.UTF8.GetBytes(key);
byte[] bytesDecrypted = AESDecryptBytes(bytesToBeDecrypted, passwordBytes);
string result = Encoding.UTF8.GetString(bytesDecrypted);
return result;
}
catch (Exception ex)
{
return "-1";
}
}
public static byte[] AESDecryptBytes(byte[] bytesToBeDecrypted, byte[] passwordBytes)
{
byte[] decryptedBytes = null;
using (var ms = new MemoryStream())
{
using (var AES = Aes.Create())
{
AES.KeySize = 128;
AES.BlockSize = 128;
AES.Mode = CipherMode.ECB;
AES.Padding = PaddingMode.PKCS7;
AES.Key = passwordBytes;
AES.IV = new byte[16];
using (var cs = new CryptoStream(ms, AES.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cs.Dispose();
}
decryptedBytes = ms.ToArray();
}
}
return decryptedBytes;
}
#endregion
}
}