-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathCryptAES.cs
More file actions
98 lines (93 loc) · 3.59 KB
/
CryptAES.cs
File metadata and controls
98 lines (93 loc) · 3.59 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
using System;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApplication1
{
class CryptAES
{
static void Main(string[] args)
{
string key = "cly123zhang58s@d";
Console.WriteLine(Encrypt("我真的是一个好人", key));
Console.WriteLine(Decrypt("nsbOEGPPo6x0P/0kDNZ6ScBfl2a2zcNdwFPkJ2thYFU=", key));
Console.ReadLine();
}
/// <summary>
/// AES加密
/// </summary>
/// <param name="text">要加密的文本</param>
/// <param name="key">密钥</param>
/// <returns>返回加密后的密文</returns>
public static string Encrypt(string text, string key)
{
byte[] textBytes = Encoding.UTF8.GetBytes(text);
string iv = "";
RijndaelManaged rijndael = SetRijndaelManaged(key, iv);
ICryptoTransform transform = rijndael.CreateEncryptor();
byte[] encryptBytes = transform.TransformFinalBlock(textBytes, 0, textBytes.Length);
string newText = Convert.ToBase64String(encryptBytes);
return newText;
}
/// <summary>
/// 初始化RijndaelManaged
/// </summary>
/// <param name="key">密钥</param>
/// <param name="iv">向量</param>
/// <returns></returns>
private static RijndaelManaged SetRijndaelManaged(string key, string iv)
{
RijndaelManaged rijndaelManaged = new RijndaelManaged
{
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128
};
byte[] keyBytes = new byte[16];
byte[] oldKey = Encoding.UTF8.GetBytes(key);
int len = oldKey.Length;
if (len > keyBytes.Length)
{
len = keyBytes.Length;
}
Array.Copy(oldKey, keyBytes, len);
rijndaelManaged.Key = keyBytes;
byte[] ivBytes = new byte[16];
byte[] oldiv = Encoding.UTF8.GetBytes(iv);
int ivlen = oldiv.Length;
if (ivlen > ivBytes.Length)
{
ivlen = ivBytes.Length;
}
Array.Copy(oldiv, ivBytes, ivlen);
rijndaelManaged.IV = ivBytes;
return rijndaelManaged;
}
/// <summary>
/// AES解密
/// </summary>
/// <param name="text"></param>
/// <param name="password"></param>
/// <returns></returns>
public static string Decrypt(string text, string password)
{
RijndaelManaged rijndaelCipher = new RijndaelManaged();
rijndaelCipher.Mode = CipherMode.ECB;
rijndaelCipher.Padding = PaddingMode.PKCS7;
rijndaelCipher.KeySize = 128;
rijndaelCipher.BlockSize = 128;
byte[] encryptedData = Convert.FromBase64String(text);
byte[] pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
byte[] keyBytes = new byte[16];
int len = pwdBytes.Length;
if (len > keyBytes.Length) len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
//byte[] ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
//rijndaelCipher.IV = ivBytes;
ICryptoTransform transform = rijndaelCipher.CreateDecryptor();
byte[] plainText = transform.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
return Encoding.UTF8.GetString(plainText);
}
}
}