// Encryption Routine: Zlib -> AES -> Base64 public static byte[] Encrypt(PackVersion version, byte[] src, Encryption flag, out uint size, out uint sizeCompressed, out uint sizeEncoded) { byte[] pEncrypted; if (flag.HasFlag(Encryption.Zlib)) { pEncrypted = ZlibStream.CompressBuffer(src); } else { pEncrypted = new byte[src.Length]; Buffer.BlockCopy(src, 0, pEncrypted, 0, src.Length); } size = (uint)src.Length; sizeCompressed = (uint)pEncrypted.Length; if (flag.HasFlag(Encryption.Aes)) { // Get the AES Key/IV for transformation CipherKeys.GetKeyAndIV(version, sizeCompressed, out byte[] key, out byte[] iv); // Perform AES block encryption var pCipher = new AESCipher(key, iv); pCipher.TransformBlock(pEncrypted, 0, size, pEncrypted, 0); // Encode the encrypted data into a base64 encoded string pEncrypted = Encoding.UTF8.GetBytes(Convert.ToBase64String(pEncrypted)); } else if (flag.HasFlag(Encryption.Xor)) { // Perform XOR block encryption pEncrypted = EncryptXor(version, pEncrypted, size, sizeCompressed); } sizeEncoded = (uint)pEncrypted.Length; return(pEncrypted); }
// Decryption Routine: Base64 -> AES -> Zlib private static byte[] Decrypt(PackVersion version, uint size, uint sizeCompressed, Encryption flag, byte[] src) { if (flag.HasFlag(Encryption.Aes)) { // Get the AES Key/IV for transformation CipherKeys.GetKeyAndIV(version, sizeCompressed, out byte[] key, out byte[] iv); // Decode the base64 encoded string src = Convert.FromBase64String(Encoding.UTF8.GetString(src)); // Decrypt the AES encrypted block var pCipher = new AESCipher(key, iv); pCipher.TransformBlock(src, 0, size, src, 0); } else if (flag.HasFlag(Encryption.Xor)) { // Decrypt the XOR encrypted block src = EncryptXor(version, src, size, sizeCompressed); } return(flag.HasFlag(Encryption.Zlib) ? ZlibStream.UncompressBuffer(src) : src); }