From ca3d1b8a023453c7dd3430f2f0677a90ad0645a2 Mon Sep 17 00:00:00 2001 From: codestation Date: Sun, 24 Jul 2011 17:55:20 -0430 Subject: [PATCH] Make --extract-quests use the correct quest name. Some refactoring. --- .gitignore | 1 + src/base/MHUtils.java | 13 +++++++++++++ src/crypt/DecryptUtils.java | 13 +++++++++++++ src/crypt/KirkCypher.java | 21 ++++++--------------- src/crypt/QuestCypher.java | 9 ++++++--- src/crypt/SavedataCypher.java | 3 ++- 6 files changed, 41 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index 97402ea..3b32e6b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /index.bin /.settings +/bcprov-jdk16-146.jar diff --git a/src/base/MHUtils.java b/src/base/MHUtils.java index 3914da5..3136550 100644 --- a/src/base/MHUtils.java +++ b/src/base/MHUtils.java @@ -113,4 +113,17 @@ public abstract class MHUtils { } return res; } + + static final String HEXES = "0123456789ABCDEF"; + + public static String getHex(byte[] raw) { + if (raw == null) { + return null; + } + final StringBuilder hex = new StringBuilder(2 * raw.length); + for (final byte b : raw) { + hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))).append(' '); + } + return hex.toString(); + } } diff --git a/src/crypt/DecryptUtils.java b/src/crypt/DecryptUtils.java index 89a8ca4..7b21f1c 100644 --- a/src/crypt/DecryptUtils.java +++ b/src/crypt/DecryptUtils.java @@ -87,4 +87,17 @@ public abstract class DecryptUtils { table[i + 2] = base_table[table[i + 2] & 0xFF]; table[i + 3] = base_table[table[i + 3] & 0xFF]; } + + static final String HEXES = "0123456789ABCDEF"; + + public static String getHex(byte[] raw) { + if (raw == null) { + return null; + } + final StringBuilder hex = new StringBuilder(2 * raw.length); + for (final byte b : raw) { + hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))).append(' '); + } + return hex.toString(); + } } diff --git a/src/crypt/KirkCypher.java b/src/crypt/KirkCypher.java index bcbe708..4056c92 100644 --- a/src/crypt/KirkCypher.java +++ b/src/crypt/KirkCypher.java @@ -21,23 +21,12 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; +import base.MHUtils; + import jpcsp.crypto.CryptoEngine; import keys.GameKeys; -public class KirkCypher implements GameKeys { - - static final String HEXES = "0123456789ABCDEF"; - - private static String getHex(byte[] raw) { - if (raw == null) { - return null; - } - final StringBuilder hex = new StringBuilder(2 * raw.length); - for (final byte b : raw) { - hex.append(HEXES.charAt((b & 0xF0) >> 4)).append(HEXES.charAt((b & 0x0F))).append(' '); - } - return hex.toString(); - } +public class KirkCypher extends MHUtils implements GameKeys { public void decrypt(String file) { try { @@ -47,11 +36,13 @@ public class KirkCypher implements GameKeys { fd.seek(0); System.out.println("Decrypting savedata (KIRK engine): " + byte_bt.length + " bytes"); System.out.println("Gamekey: " + getHex(gamekey)); - byte out[] = new CryptoEngine().DecryptSavedata(byte_bt, byte_bt.length, gamekey, 0); + byte hash[] = new byte[0x10]; + byte out[] = new CryptoEngine().DecryptSavedata(byte_bt, byte_bt.length, gamekey, 0, hash); fd.write(out); fd.setLength(out.length); fd.close(); System.out.println("Finished (" + out.length + " bytes)"); + System.out.println("Hash: " + getHex(hash)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { diff --git a/src/crypt/QuestCypher.java b/src/crypt/QuestCypher.java index 7d5c98d..a1d0805 100644 --- a/src/crypt/QuestCypher.java +++ b/src/crypt/QuestCypher.java @@ -30,9 +30,11 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; +import base.MHUtils; + import keys.QuestKeys; -public class QuestCypher implements QuestKeys { +public class QuestCypher extends MHUtils implements QuestKeys { private short key_var_table[] = {0, 0, 0, 0}; @@ -114,17 +116,18 @@ public class QuestCypher implements QuestKeys { in.read(byte_bt); in.close(); for(int i = 0; i < 100; i++) { + String mib = new String(byte_bt, (i + 1) * QUEST_SIZE - 16, 16); ByteBuffer bt = ByteBuffer.wrap(byte_bt,i * QUEST_SIZE, QUEST_SIZE); bt.order(ByteOrder.LITTLE_ENDIAN); ShortBuffer sb = bt.asShortBuffer(); short short_bt[] = new short[QUEST_SIZE/2]; sb.get(short_bt); - System.out.println("Decrypting quest file # " + i); int len = decrypt_quest(short_bt); if(len > 0) { + System.out.println("Decrypted quest # " + i + ": " + mib); sb.rewind(); sb.put(short_bt); - String outname = String.format("%s/%02d_quest.bin", directory, i); + String outname = String.format("%s/%s", directory, mib); FileOutputStream out = new FileOutputStream(outname); out.write(byte_bt, i * QUEST_SIZE, len); out.close(); diff --git a/src/crypt/SavedataCypher.java b/src/crypt/SavedataCypher.java index 2b5f522..0d76949 100644 --- a/src/crypt/SavedataCypher.java +++ b/src/crypt/SavedataCypher.java @@ -95,7 +95,7 @@ public class SavedataCypher extends DecryptUtils implements SavedataKeys { byte byte_bt[] = new byte[(int)fd.length()]; fd.read(byte_bt); fd.seek(0); - System.out.println("Updating sha1 hash"); + System.out.print("Updating "); update_sha1(byte_bt); System.out.println("Encrypting savedata"); encrypt_buffer(byte_bt); @@ -120,6 +120,7 @@ public class SavedataCypher extends DecryptUtils implements SavedataKeys { MessageDigest md = MessageDigest.getInstance("sha-1"); byte digest[] = md.digest(buffer); System.arraycopy(replace, 0, buf, buf.length - 36, 20); + System.out.println("SHA-1: " + getHex(digest)); System.arraycopy(digest, 0, buf, buf.length - 24, digest.length); } catch (NoSuchAlgorithmException e) { e.printStackTrace();