|
@@ -7,64 +7,14 @@ namespace GFGGame
|
|
|
{
|
|
|
public static class AesEncryption
|
|
|
{
|
|
|
- // !!!重要警告!!!
|
|
|
- // 硬编码在客户端的密钥并不安全,容易被反编译获取。
|
|
|
- // 这只能防止简单的窥探,无法抵御专业黑客。
|
|
|
- // 对于极度敏感的信息,请考虑其他方案(如服务端令牌验证)。
|
|
|
- private static readonly string Key = "45418100000000000000000000000000"; // 必须恰好是32字节
|
|
|
- private static readonly string IV = "1234567890123456"; // 必须恰好是16字节
|
|
|
-
|
|
|
public static string Encrypt(string plainText)
|
|
|
{
|
|
|
- using (Aes aesAlg = Aes.Create())
|
|
|
- {
|
|
|
- aesAlg.Key = Encoding.UTF8.GetBytes(Key);
|
|
|
- aesAlg.IV = Encoding.UTF8.GetBytes(IV);
|
|
|
-
|
|
|
- ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
|
|
|
- using (MemoryStream msEncrypt = new MemoryStream())
|
|
|
- {
|
|
|
- using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
|
|
|
- {
|
|
|
- using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
|
|
|
- {
|
|
|
- swEncrypt.Write(plainText);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return Convert.ToBase64String(msEncrypt.ToArray());
|
|
|
- }
|
|
|
- }
|
|
|
+ return plainText;
|
|
|
}
|
|
|
|
|
|
public static string Decrypt(string cipherText)
|
|
|
{
|
|
|
- try
|
|
|
- {
|
|
|
- byte[] buffer = Convert.FromBase64String(cipherText);
|
|
|
- using (Aes aesAlg = Aes.Create())
|
|
|
- {
|
|
|
- aesAlg.Key = Encoding.UTF8.GetBytes(Key);
|
|
|
- aesAlg.IV = Encoding.UTF8.GetBytes(IV);
|
|
|
-
|
|
|
- ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
|
|
|
- using (MemoryStream msDecrypt = new MemoryStream(buffer))
|
|
|
- {
|
|
|
- using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
|
|
|
- {
|
|
|
- using (StreamReader srDecrypt = new StreamReader(csDecrypt))
|
|
|
- {
|
|
|
- return srDecrypt.ReadToEnd();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- catch
|
|
|
- {
|
|
|
- // 解密失败(可能数据被篡改或密钥错误)
|
|
|
- return null;
|
|
|
- }
|
|
|
+ return cipherText;
|
|
|
}
|
|
|
}
|
|
|
}
|