// with unity header public static void EncryptFile(String file) { var fileName = Path.GetFileNameWithoutExtension(file); var unityBlob = new List <Byte>(); unityBlob.AddRange(BitConverter.GetBytes(fileName.Length)); unityBlob.AddRange(Encoding.ASCII.GetBytes(fileName)); var pad = 8 - ((unityBlob.Count - 1) % 8) - 1; unityBlob.AddRange(Enumerable.Range(0, pad).Select(i => (Byte)0).ToList()); var bytes = File.ReadAllBytes(file).ToList(); var size = bytes.Count; pad = 8 - ((bytes.Count - 1) % 8) - 1; bytes.AddRange(Enumerable.Range(0, pad).Select(i => (Byte)0).ToList()); var cipher = new ROMDesCipher(ROMKey, 8); var encryptedBytes = cipher.Encrypt(bytes.ToArray()); var payload = new List <Byte>(); payload.AddRange(Encoding.ASCII.GetBytes(ROMSig)); payload.AddRange(BitConverter.GetBytes(size)); payload.AddRange(encryptedBytes); unityBlob.AddRange(BitConverter.GetBytes(payload.Count)); unityBlob.AddRange(payload); File.WriteAllBytes(file.replace(".lua", "") + ".bytes", unityBlob.ToArray()); }
// without unity header public static void DecryptFile(String file) { var fileBytes = File.ReadAllBytes(file); var header = fileBytes.Take(12); var signature = header.Take(8); if (Encoding.ASCII.GetString(signature.ToArray()) != ROMSig) { throw new Exception("not a valid ROM payload"); } var size = BitConverter.ToInt32(header.Skip(8).ToArray(), 0); var encryptedBytes = fileBytes.Skip(12).ToArray(); var cipher = new ROMDesCipher(ROMKey, 8); var decryptedBytes = cipher.Decrypt(encryptedBytes); File.WriteAllBytes(file.replace(".bytes", "") + ".lua", decryptedBytes.Take(size).ToArray()); }