using System;
using System.IO;
using System.Linq;
using System.Net.NetworkInformation;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;
public class AESManager : MonoBehaviour
{
private static byte[] Key = new byte[16];
private static readonly byte[] IV = new byte[16];
public static bool isPasswordValid;
void Start()
{
Key = GenerateKey("123456qwer");
Debug.LogError(Encrypt("20241231", "1A-2B-3C-4D-5E-6F", 01)) ;
Main();
}
private byte[] GenerateKey(string keyString)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(keyString);
// 如果keyBytes长度大于16,截取前16位
if (keyBytes.Length > 16)
{
return keyBytes.Take(16).ToArray();
}
// 如果keyBytes长度小于或等于16,循环填充到16位
else
{
byte[] paddedKeyBytes = new byte[16];
Array.Copy(keyBytes, paddedKeyBytes, keyBytes.Length);
int paddingLength = 16 - keyBytes.Length;
for (int i = 0; i < paddingLength; i++)
{
paddedKeyBytes[keyBytes.Length + i] = keyBytes[i];
}
return paddedKeyBytes;
}
}
private string ReadPasswordFromFile(string filePath)
{
string password = "";
try
{
using (StreamReader reader = new StreamReader(filePath))
{
password = reader.ReadLine();
}
}
catch (Exception ex)
{
}
return password;
}
private void Main()
{
// 读取txt文件中的密码文本
string passwordText = ReadPasswordFromFile(Application.streamingAssetsPath+ "/passwords.txt");
// 获取本机系统时间
DateTime currentTime = DateTime.Now;
// 获取本机MAC地址
string macAddress = GetMacAddress();
// 调用Verify方法进行比对
isPasswordValid = Verify(passwordText, macAddress, currentTime);
}
private string GetMacAddress()
{
string macAddresses = "";
foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
{
if (ni.OperationalStatus == OperationalStatus.Up)
{
PhysicalAddress physAddr = ni.GetPhysicalAddress();
if (physAddr != null)
{
byte[] macBytes = physAddr.GetAddressBytes();
macAddresses = string.Join("-", macBytes.Select(b => b.ToString("x2")).ToArray()).ToLower();
break;
}
}
}
return macAddresses;
}
public string Encrypt(string expirationDate, string macAddress, int passwordVersion)
{
// 将到期时间和MAC地址组合成一个字符串
string combinedText = $"{expirationDate}-{macAddress}";
byte[] array;
using (Aes aes = Aes.Create())
{
aes.Key = Key;
aes.IV = IV;
ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
{
streamWriter.Write(combinedText);
}
array = memoryStream.ToArray();
}
}
}
return $"{passwordVersion.ToString().PadLeft(2, '0')}{Convert.ToBase64String(array)}";
}
public bool Verify(string encryptedText, string macAddress, DateTime currentTime)
{
// 直接从encryptedText字符串中切片去掉前两位
string base64CipherText = encryptedText.Substring(2);
// 解密字符串
byte[] decryptedBytes = Convert.FromBase64String(base64CipherText);
string decryptedText = Decrypt(decryptedBytes);
// 提取日期和MAC地址
string parsedExpirationDate = decryptedText.Substring(0, 8);
string parsedMacAddress = decryptedText.Substring(9);
// 检查到期时间
if (int.Parse(parsedExpirationDate) < currentTime.Year * 10000 + currentTime.Month * 100 + currentTime.Day)
{
return false;
}
// 检查MAC地址
if (parsedMacAddress.ToLower().Replace("-", "") != macAddress.ToLower().Replace("-", ""))
{
return false;
}
return true;
}
private string Decrypt(byte[] cipherText)
{
using (Aes aes = Aes.Create())
{
aes.Key = Key;
aes.IV = IV;
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
using (MemoryStream memoryStream = new MemoryStream(cipherText))
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
using (StreamReader streamReader = new StreamReader(cryptoStream))
{
return streamReader.ReadToEnd();
}
}
}
}
}
}
unity AES加密
于 2024-05-10 09:41:49 首次发布