diff --git a/src/main/java/konogonka/AppPreferences.java b/src/main/java/konogonka/AppPreferences.java index fade087..433cfc0 100644 --- a/src/main/java/konogonka/AppPreferences.java +++ b/src/main/java/konogonka/AppPreferences.java @@ -7,10 +7,21 @@ public class AppPreferences { public static AppPreferences getInstance() { return INSTANCE; } private Preferences preferences; - private int titleKeysCount; + + private int + kakAppCount, + kakOceanCount, + kakSysCount, + titleKeksCount, + titleKeysCount; private AppPreferences(){ preferences = Preferences.userRoot().node("konogonka"); + + kakAppCount = getKAKAppCount(); + kakOceanCount = getKAKOceanCount(); + kakSysCount = getKAKSysCount(); + titleKeksCount = getTitleKeksCount(); titleKeysCount = getTitleKeysCount(); } @@ -41,23 +52,65 @@ public class AppPreferences { public String getHeaderKey(){ return preferences.get("header_key", "");} public void setHeaderKey(String key){ preferences.put("header_key", key); } - public String getApplicationKey(int number){ return preferences.get("key_area_key_application_0"+number, "");} - public void setApplicationKey(int number, String key){ preferences.put("key_area_key_application_0"+number, key); } - - public String getOceanKey(int number){ return preferences.get("key_area_key_ocean_0"+number, "");} - public void setOceanKey(int number, String key){ preferences.put("key_area_key_ocean_0"+number, key); } - - public String getSystemKey(int number){ return preferences.get("key_area_key_system_0"+number, "");} - public void setSystemKey(int number, String key){ preferences.put("key_area_key_system_0"+number, key); } - - public String getTitleKek(int number){ return preferences.get("titlekek_"+number, "");} - public void setTitleKek(int number, String key){ preferences.put("titlekek_"+number, key); } - - - - public int getTitleKeysCount(){ // TODO: do the same for other multi-keys and single - return preferences.getInt("title_keys_count", 0); + // KAKs: Application/Ocean/System + public void setKAKAppCount(int number){ + if (this.kakAppCount > number){ + for (int i = number; i < this.kakAppCount; i++) { + preferences.remove(String.format("key_area_key_application_%02d", number)); + } + } + preferences.putInt("key_area_key_application_count", number); + this.kakAppCount = number; } + public void setKAKOceanCount(int number){ + if (this.kakOceanCount > number){ + for (int i = number; i < this.kakOceanCount; i++) { + preferences.remove(String.format("key_area_key_ocean_%02d", number)); + } + } + preferences.putInt("key_area_key_ocean_count", number); + this.kakOceanCount = number; + } + public void setKAKSysCount(int number){ + if (this.kakSysCount > number){ + for (int i = number; i < this.kakSysCount; i++) { + preferences.remove(String.format("key_area_key_system_%02d", number)); + } + } + preferences.putInt("key_area_key_system_count", number); + this.kakSysCount = number; + } + + public void setTitleKeksCount(int number){ + if (this.titleKeksCount > number){ + for (int i = number; i < this.titleKeksCount; i++) { + preferences.remove(String.format("titlekek_%02d", number)); + } + } + preferences.putInt("titlekek_count", number); + this.titleKeksCount = number; + } + + public int getKAKAppCount(){ return preferences.getInt("key_area_key_application_count", 0); } + public int getKAKOceanCount(){ return preferences.getInt("key_area_key_ocean_count", 0); } + public int getKAKSysCount(){ return preferences.getInt("key_area_key_system_count", 0); } + public int getTitleKeksCount(){ return preferences.getInt("titlekek_count", 0); } + + public String getApplicationKey(int number){ return preferences.get(String.format("key_area_key_application_%02d", number), "");} + public void setApplicationKey(int number, String key){ preferences.put(String.format("key_area_key_application_%02d", number), key); } + + public String getOceanKey(int number){ return preferences.get(String.format("key_area_key_ocean_%02d", number), "");} + public void setOceanKey(int number, String key){ preferences.put(String.format("key_area_key_ocean_%02d", number), key); } + + public String getSystemKey(int number){ return preferences.get(String.format("key_area_key_system_%02d", number), "");} + public void setSystemKey(int number, String key){ preferences.put(String.format("key_area_key_system_%02d", number), key); } + + public String getTitleKek(int number){ return preferences.get(String.format("titlekek_%02d", number), "");} + public void setTitleKek(int number, String key){ preferences.put(String.format("titlekek_%02d", number), key); } + + + // Title keys + public int getTitleKeysCount(){ return preferences.getInt("title_keys_count", 0); } // Since we don't want to store title keys that are no longer in use, we have to (try to) remove them. // This part of code works as a charm. Don't touch. public void setTitleKeysCount(int number){ @@ -74,4 +127,4 @@ public class AppPreferences { return preferences.get("title_key_"+number, "0 = 0").split(" = ", 2); } public void setTitleKey(int number, String pair){ preferences.put("title_key_"+number, pair); } -} +} \ No newline at end of file diff --git a/src/main/java/konogonka/Controllers/NCA/NCAController.java b/src/main/java/konogonka/Controllers/NCA/NCAController.java index 488edd3..da3d27b 100644 --- a/src/main/java/konogonka/Controllers/NCA/NCAController.java +++ b/src/main/java/konogonka/Controllers/NCA/NCAController.java @@ -76,12 +76,15 @@ public class NCAController implements TabController { this.selectedFile = file; HashMap keysMap = new HashMap<>(); keysMap.put("header_key", AppPreferences.getInstance().getHeaderKey()); - for (int i = 0; i < 8; i++){ // TODO: FIX!!!!!!!!! URGENT! - keysMap.put("key_area_key_application_0"+i, AppPreferences.getInstance().getApplicationKey(i)); - keysMap.put("key_area_key_ocean_0"+i, AppPreferences.getInstance().getOceanKey(i)); - keysMap.put("key_area_key_system_0"+i, AppPreferences.getInstance().getSystemKey(i)); - keysMap.put("titlekek_0"+i, AppPreferences.getInstance().getTitleKek(i)); - } + + for (int i = 0; i < AppPreferences.getInstance().getKAKAppCount(); i++) + keysMap.put(String.format("key_area_key_application_%02d", i), AppPreferences.getInstance().getApplicationKey(i)); + for (int i = 0; i < AppPreferences.getInstance().getKAKOceanCount(); i++) + keysMap.put(String.format("key_area_key_ocean_%02d", i), AppPreferences.getInstance().getOceanKey(i)); + for (int i = 0; i < AppPreferences.getInstance().getKAKSysCount(); i++) + keysMap.put(String.format("key_area_key_system_%02d", i), AppPreferences.getInstance().getSystemKey(i)); + for (int i = 0; i < AppPreferences.getInstance().getTitleKeksCount(); i++) + keysMap.put(String.format("titlekek_%02d", i), AppPreferences.getInstance().getTitleKek(i)); for (int i = 0; i < AppPreferences.getInstance().getTitleKeysCount(); i++){ String[] pair = AppPreferences.getInstance().getTitleKeyPair(i); if ( ! pair[0].equals("0") && ! pair[1].equals("0")) diff --git a/src/main/java/konogonka/Settings/SettingsController.java b/src/main/java/konogonka/Settings/SettingsController.java index baf05b1..c1988f0 100644 --- a/src/main/java/konogonka/Settings/SettingsController.java +++ b/src/main/java/konogonka/Settings/SettingsController.java @@ -42,40 +42,31 @@ public class SettingsController implements Initializable { ListSelectorTitleKeysController.initSelector(32, null); // 32 required LinkedHashMap preparedPairsMapInit = new LinkedHashMap<>(); - String kaekApp; - int cnt = 0; - while (!(kaekApp = AppPreferences.getInstance().getApplicationKey(cnt)).isEmpty()){ - preparedPairsMapInit.put("key_area_key_application_"+String.format("%02d", cnt), kaekApp); - cnt++; + for (int i = 0; i < AppPreferences.getInstance().getKAKAppCount(); i++){ + preparedPairsMapInit.put(String.format("key_area_key_application_%02d", i), AppPreferences.getInstance().getApplicationKey(i)); } ListSelectorKAEKAppController.setList(preparedPairsMapInit); - preparedPairsMapInit.clear(); - cnt = 0; - while (!(kaekApp = AppPreferences.getInstance().getOceanKey(cnt)).isEmpty()){ - preparedPairsMapInit.put("key_area_key_ocean_"+String.format("%02d", cnt), kaekApp); - cnt++; + + for (int i = 0; i < AppPreferences.getInstance().getKAKOceanCount(); i++){ + preparedPairsMapInit.put(String.format("key_area_key_ocean_%02d", i), AppPreferences.getInstance().getOceanKey(i)); } ListSelectorKAEKOceanController.setList(preparedPairsMapInit); - preparedPairsMapInit.clear(); - cnt = 0; - while (!(kaekApp = AppPreferences.getInstance().getTitleKek(cnt)).isEmpty()){ - preparedPairsMapInit.put("titlekek_"+String.format("%02d", cnt), kaekApp); - cnt++; - } - ListSelectorTitleKeksController.setList(preparedPairsMapInit); - preparedPairsMapInit.clear(); - cnt = 0; - while (!(kaekApp = AppPreferences.getInstance().getSystemKey(cnt)).isEmpty()){ - preparedPairsMapInit.put("key_area_key_system_"+String.format("%02d", cnt), kaekApp); - cnt++; + for (int i = 0; i < AppPreferences.getInstance().getKAKSysCount(); i++){ + preparedPairsMapInit.put(String.format("key_area_key_system_%02d", i), AppPreferences.getInstance().getSystemKey(i)); } ListSelectorKAEKSysController.setList(preparedPairsMapInit); - preparedPairsMapInit.clear(); + + for (int i = 0; i < AppPreferences.getInstance().getTitleKeksCount(); i++){ + preparedPairsMapInit.put(String.format("titlekek_%02d", i), AppPreferences.getInstance().getTitleKek(i)); + } + ListSelectorTitleKeksController.setList(preparedPairsMapInit); + preparedPairsMapInit.clear(); + for (int i = 0; i < AppPreferences.getInstance().getTitleKeysCount(); i++){ preparedPairsMapInit.put( AppPreferences.getInstance().getTitleKeyPair(i)[0], @@ -118,32 +109,32 @@ public class SettingsController implements Initializable { String keyParsed; int counter = 0; - while ((keyParsed = fileMap.get("key_area_key_application_"+String.format("%02d", counter))) != null){ - kaekSingle.put("key_area_key_application_"+String.format("%02d", counter), keyParsed); + while ((keyParsed = fileMap.get(String.format("key_area_key_application_%02d", counter))) != null){ + kaekSingle.put(String.format("key_area_key_application_%02d", counter), keyParsed); counter++; } ListSelectorKAEKAppController.setList(kaekSingle); kaekSingle.clear(); counter = 0; - while ((keyParsed = fileMap.get("key_area_key_ocean_"+String.format("%02d", counter))) != null){ - kaekSingle.put("key_area_key_ocean_"+String.format("%02d", counter), keyParsed); + while ((keyParsed = fileMap.get(String.format("key_area_key_ocean_%02d", counter))) != null){ + kaekSingle.put(String.format("key_area_key_ocean_%02d", counter), keyParsed); counter++; } ListSelectorKAEKOceanController.setList(kaekSingle); kaekSingle.clear(); counter = 0; - while ((keyParsed = fileMap.get("key_area_key_system_"+String.format("%02d", counter))) != null){ - kaekSingle.put("key_area_key_system_"+String.format("%02d", counter), keyParsed); + while ((keyParsed = fileMap.get(String.format("key_area_key_system_%02d", counter))) != null){ + kaekSingle.put(String.format("key_area_key_system_%02d", counter), keyParsed); counter++; } ListSelectorKAEKSysController.setList(kaekSingle); kaekSingle.clear(); counter = 0; - while ((keyParsed = fileMap.get("titlekek_"+String.format("%02d", counter))) != null){ - kaekSingle.put("titlekek_"+String.format("%02d", counter), keyParsed); + while ((keyParsed = fileMap.get(String.format("titlekek_%02d", counter))) != null){ + kaekSingle.put(String.format("titlekek_%02d", counter), keyParsed); counter++; } ListSelectorTitleKeksController.setList(kaekSingle); @@ -203,24 +194,32 @@ public class SettingsController implements Initializable { for (int i = 0; i < kaekAppKeySet.length; i++) AppPreferences.getInstance().setApplicationKey(i, kaekAppKeySet[i].split("\\s=\\s", 2)[1]); } + if (kaekAppKeySet != null) + AppPreferences.getInstance().setKAKAppCount(kaekAppKeySet.length); String[] kaekOceanKeySet = ListSelectorKAEKOceanController.getList(); if (kaekOceanKeySet != null){ for (int i = 0; i < kaekOceanKeySet.length; i++) AppPreferences.getInstance().setOceanKey(i, kaekOceanKeySet[i].split("\\s=\\s", 2)[1]); } + if (kaekOceanKeySet != null) + AppPreferences.getInstance().setKAKOceanCount(kaekOceanKeySet.length); String[] kaekSysKeySet = ListSelectorKAEKSysController.getList(); if (kaekSysKeySet != null){ for (int i = 0; i < kaekSysKeySet.length; i++) AppPreferences.getInstance().setSystemKey(i, kaekSysKeySet[i].split("\\s=\\s", 2)[1]); } + if (kaekSysKeySet != null) + AppPreferences.getInstance().setKAKSysCount(kaekSysKeySet.length); String[] titleKekSet = ListSelectorTitleKeksController.getList(); if (titleKekSet != null){ for (int i = 0; i < titleKekSet.length; i++) AppPreferences.getInstance().setTitleKek(i, titleKekSet[i].split("\\s=\\s", 2)[1]); } + if (titleKekSet != null) + AppPreferences.getInstance().setTitleKeksCount(titleKekSet.length); String[] titleKeysSet = ListSelectorTitleKeysController.getList(); if (titleKeysSet != null){ diff --git a/src/main/java/konogonka/Tools/NCA/NCAProvider.java b/src/main/java/konogonka/Tools/NCA/NCAProvider.java index 0b546c1..7bdf92d 100644 --- a/src/main/java/konogonka/Tools/NCA/NCAProvider.java +++ b/src/main/java/konogonka/Tools/NCA/NCAProvider.java @@ -171,13 +171,13 @@ public class NCAProvider { String keyAreaKey; switch (keyIndex){ case 0: - keyAreaKey = keys.get("key_area_key_application_"+String.format("%02d", cryptoTypeReal)); + keyAreaKey = keys.get(String.format("key_area_key_application_%02d", cryptoTypeReal)); break; case 1: - keyAreaKey = keys.get("key_area_key_ocean_"+String.format("%02d", cryptoTypeReal)); + keyAreaKey = keys.get(String.format("key_area_key_ocean_%02d", cryptoTypeReal)); break; case 2: - keyAreaKey = keys.get("key_area_key_system_"+String.format("%02d", cryptoTypeReal)); + keyAreaKey = keys.get(String.format("key_area_key_system_%02d", cryptoTypeReal)); break; default: keyAreaKey = null; @@ -259,7 +259,7 @@ public class NCAProvider { try { SecretKeySpec skSpec = new SecretKeySpec( - hexStrToByteArray(keys.get("titlekek_"+String.format("%02d", cryptoTypeReal)) + hexStrToByteArray(keys.get(String.format("titlekek_%02d", cryptoTypeReal)) ), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, skSpec);