Fix savedata decryption/encryption

This commit is contained in:
codestation 2011-07-23 01:31:56 -04:30
parent 4f0f67cabc
commit 39d2736f9b
4 changed files with 19 additions and 13 deletions

View file

@ -81,10 +81,10 @@ public abstract class DecryptUtils {
buffer[3] = table[buffer[3] & 0xFF];
}
protected void set_table_data(byte table[], int i) {
table[i] = decrypt_table[table[i] & 0xFF];
table[i + 1] = decrypt_table[table[i + 1] & 0xFF];
table[i + 2] = decrypt_table[table[i + 2] & 0xFF];
table[i + 3] = decrypt_table[table[i + 3] & 0xFF];
protected void set_table_data(byte table[], byte base_table[], int i) {
table[i] = base_table[table[i] & 0xFF];
table[i + 1] = base_table[table[i + 1] & 0xFF];
table[i + 2] = base_table[table[i + 2] & 0xFF];
table[i + 3] = base_table[table[i + 3] & 0xFF];
}
}

View file

@ -199,7 +199,7 @@ public class Decrypter extends DecryptUtils implements DataKeys {
int read = filein.read(buffer);
size -= read;
for (int i = 0; i < read; i += 4) {
set_table_data(buffer, i);
set_table_data(buffer, decrypt_table, i);
long alpha = get_table_value(buffer, i);
long beta = getBeta();
long gamma = alpha ^ beta;

View file

@ -66,6 +66,7 @@ public class KirkCypher implements GameKeys {
fd.read(byte_bt);
fd.seek(0);
System.out.println("Encrypting savedata (KIRK engine): " + byte_bt.length + " bytes");
System.out.println("Gamekey: " + getHex(gamekey));
byte out[] = new CryptoEngine().EncryptSavedata(byte_bt, byte_bt.length, gamekey, 0);
fd.write(out);
fd.setLength(out.length);

View file

@ -73,19 +73,20 @@ public class SavedataCypher extends DecryptUtils implements SavedataKeys {
private void decrypt_buffer(byte buffer[]) {
int len = buffer.length - 4;
byte seed[] = new byte[4];
System.arraycopy(buffer, len, seed, 0, 4);
System.arraycopy(buffer, len, seed, 0, 4);
get_table_value(decrypt_table, seed);
get_table_value(decrypt_table, seed);
get_table_value(decrypt_table, seed);
long alpha = get_table_value(seed, 0);
initSeed(alpha);
for (int i = 0; i < len; i += 4) {
set_table_data(buffer, i);
set_table_data(buffer, decrypt_table, i);
alpha = get_table_value(buffer, i);
long beta = getBeta();
long gamma = alpha ^ beta;
set_table_value(buffer, i, gamma);
set_table_data(buffer, i);
set_table_data(buffer, decrypt_table, i);
}
System.arraycopy(seed, 0, buffer, len, 4);
}
public void encrypt(String file) {
@ -130,15 +131,19 @@ public class SavedataCypher extends DecryptUtils implements SavedataKeys {
}
int len = buffer.length - 4;
byte seed[] = new byte[4];
System.arraycopy(buffer, len, seed, 0, 4);
System.arraycopy(buffer, len, seed, 0, 4);
long alpha = get_table_value(seed, 0);
initSeed(alpha);
for (int i = 0; i < len; i += 4) {
for (int i = 0; i < len; i += 4) {
set_table_data(buffer, encrypt_table, i);
long gamma = get_table_value(buffer, i);
long beta = getBeta();
alpha = beta ^ gamma;
set_table_value(buffer, i, alpha);
get_table_value(encrypt_table, buffer);
set_table_data(buffer, encrypt_table, i);
}
get_table_value(encrypt_table, seed);
get_table_value(encrypt_table, seed);
System.arraycopy(seed, 0, buffer, len, 4);
}
}