using System; using System.Collections; using System.Collections.Generic; using System.Security.Cryptography; using System.Text; using UnityEngine; namespace WZ { public static class EncryptionUtils { public static string BytesToHexString(byte[] bytes, bool isUpperCase) { if (bytes == null) return ""; char[] hexDigits = isUpperCase ? HEX_DIGITS_UPPER : HEX_DIGITS_LOWER; int len = bytes.Length; if (len == 0) return ""; char[] ret = new char[len << 1]; for (int i = 0, j = 0; i < len; i++) { ret[j++] = hexDigits[bytes[i] >> 4 & 0x0f]; ret[j++] = hexDigits[bytes[i] & 0x0f]; } return new string(ret); } public static byte[] HexStringToBytes(string hexString) { if (IsSpace(hexString)) return new byte[0]; int len = hexString.Length; if (len % 2 != 0) { hexString = "0" + hexString; len = len + 1; } char[] hexBytes = hexString.ToUpper().ToCharArray(); byte[] ret = new byte[len >> 1]; for (int i = 0; i < len; i += 2) { ret[i >> 1] = (byte)(HexToDec(hexBytes[i]) << 4 | HexToDec(hexBytes[i + 1])); } return ret; } public static bool IsSpace(string s) { if (s == null) return true; for (int i = 0, len = s.Length; i < len; ++i) { if (!char.IsWhiteSpace(s[i])) { return false; } } return true; } private static int HexToDec(char hexChar) { if (hexChar >= '0' && hexChar <= '9') { return hexChar - '0'; } else if (hexChar >= 'A' && hexChar <= 'F') { return hexChar - 'A' + 10; } else { throw new ArgumentException(); } } private static readonly char[] HEX_DIGITS_UPPER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; private static readonly char[] HEX_DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; private static string GetKey(string packageName) { var keyBytes = Encoding.UTF8.GetBytes(packageName); byte[] keyHash; using (var md5 = MD5.Create()) { keyHash = md5.ComputeHash(keyBytes); } return BitConverter.ToString(keyHash).Replace("-", "").ToUpper(); } // Aes 加密 public static string AesEncrypt(string packageName, string content) { string key = GetKey(packageName); var contentBytes = Encoding.UTF8.GetBytes(content); var keyBytes = Encoding.UTF8.GetBytes(key); byte[] keyHash; using (var md5 = MD5.Create()) { keyHash = md5.ComputeHash(keyBytes); } using var aesAlg = Aes.Create(); aesAlg.Key = keyHash; aesAlg.Mode = CipherMode.ECB; aesAlg.Padding = PaddingMode.PKCS7; var encryptor = aesAlg.CreateEncryptor(); var encryptedBytes = encryptor.TransformFinalBlock(contentBytes, 0, contentBytes.Length); return BitConverter.ToString(encryptedBytes).Replace("-", "").ToLower(); } // Aes 解密 public static string AesDecrypt(string packageName, string encrypted) { string key = GetKey(packageName); var keyBytes = Encoding.UTF8.GetBytes(key); var encryptedBytes = new byte[encrypted.Length / 2]; for (var i = 0; i < encryptedBytes.Length; i++) { encryptedBytes[i] = Convert.ToByte(encrypted.Substring(i * 2, 2), 16); } // 使用传统的MD5创建方式 byte[] keyHash; using (var md5 = MD5.Create()) { keyHash = md5.ComputeHash(keyBytes); } using var aesAlg = Aes.Create(); aesAlg.Key = keyHash; aesAlg.Mode = CipherMode.ECB; aesAlg.Padding = PaddingMode.PKCS7; var decryptor = aesAlg.CreateDecryptor(); var decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length); return Encoding.UTF8.GetString(decryptedBytes); } } }