diff --git a/Misc/PKM.cs b/Misc/PKM.cs index 7b0f9fca7..441921e2a 100644 --- a/Misc/PKM.cs +++ b/Misc/PKM.cs @@ -64,7 +64,7 @@ internal static byte[] shuffleArray(byte[] data, uint sv) // Fill the Battle Stats back if (data.Length > 136) - Array.Copy(data, 136, sdata, 136, 100); + Array.Copy(data, 136, sdata, 136, data.Length - 136); return sdata; } @@ -120,6 +120,21 @@ internal static byte[] encryptArray(byte[] pkm) // Done return ekm; } + + /// + /// Checks to see if the PKM file is encrypted; if so, decrypts. + /// + /// Input byte array + internal static void checkEncrypted(ref byte[] pkm) + { + if (pkm.Length != PK4.SIZE_STORED && pkm.Length != PK4.SIZE_PARTY && pkm.Length != PK5.SIZE_PARTY) + return; // bad + + ushort chk = 0; + for (int i = 8; i < PK4.SIZE_STORED; i += 2) // Loop through the entire PKM + chk += BitConverter.ToUInt16(pkm, i); + pkm = chk != BitConverter.ToUInt16(pkm, 0x06) ? decryptArray(pkm) : pkm; + } /// Calculates the CRC16-CCITT checksum over an input byte array. /// Input byte array diff --git a/PKX/f1-Main.cs b/PKX/f1-Main.cs index 15914d4f1..3a4062b6c 100644 --- a/PKX/f1-Main.cs +++ b/PKX/f1-Main.cs @@ -617,8 +617,9 @@ private void openFile(byte[] input, string path, string ext) #region PK3/PK4/PK5 Conversion else if (new[] { PK3.SIZE_PARTY, PK3.SIZE_STORED, PK4.SIZE_PARTY, PK4.SIZE_STORED, PK5.SIZE_PARTY }.Contains(input.Length)) { + PKM.checkEncrypted(ref input); if (!PKX.verifychk(input)) Util.Error("Invalid File (Checksum Error)"); - try // to convert g5pkm + try // to convert pk6 { populateFields(Converter.ConvertPKMtoPK6(input)); }