From c6e56956e1a608bb8581e2693867f59a2282a62e Mon Sep 17 00:00:00 2001 From: Dmitry Isaenko Date: Wed, 17 May 2023 16:37:27 +0300 Subject: [PATCH] Minor corrections --- src/main/java/libKonogonka/KeyChainHolder.java | 16 ++++++++-------- .../java/libKonogonka/aesctr/AesCtrDecrypt.java | 6 +----- .../java/libKonogonka/fs/NCA/NCAProvider.java | 7 +++---- 3 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/main/java/libKonogonka/KeyChainHolder.java b/src/main/java/libKonogonka/KeyChainHolder.java index a430984..d438fce 100644 --- a/src/main/java/libKonogonka/KeyChainHolder.java +++ b/src/main/java/libKonogonka/KeyChainHolder.java @@ -46,14 +46,14 @@ public class KeyChainHolder { private void collectEverything() throws Exception{ rawKeySet = new HashMap<>(); - BufferedReader br = new BufferedReader(new FileReader(keysFile)); - - String fileLine; - String[] keyValue; - while ((fileLine = br.readLine()) != null){ - keyValue = fileLine.trim().split("\\s+?=\\s+?", 2); - if (keyValue.length == 2) - rawKeySet.put(keyValue[0], keyValue[1]); + try (BufferedReader br = new BufferedReader(new FileReader(keysFile))) { + String fileLine; + String[] keyValue; + while ((fileLine = br.readLine()) != null) { + keyValue = fileLine.trim().split("\\s+?=\\s+?", 2); + if (keyValue.length == 2) + rawKeySet.put(keyValue[0], keyValue[1]); + } } key_area_key_application = collectKeysByType("key_area_key_application"); diff --git a/src/main/java/libKonogonka/aesctr/AesCtrDecrypt.java b/src/main/java/libKonogonka/aesctr/AesCtrDecrypt.java index 78f86b9..88d8ff8 100644 --- a/src/main/java/libKonogonka/aesctr/AesCtrDecrypt.java +++ b/src/main/java/libKonogonka/aesctr/AesCtrDecrypt.java @@ -5,13 +5,9 @@ import org.bouncycastle.jce.provider.BouncyCastleProvider; import java.security.Security; public abstract class AesCtrDecrypt { - private static boolean shouldBeInitialized = true; - protected AesCtrDecrypt(){ - if (shouldBeInitialized){ + if (Security.getProvider("BC") == null) Security.addProvider(new BouncyCastleProvider()); - shouldBeInitialized = false; - } } /** diff --git a/src/main/java/libKonogonka/fs/NCA/NCAProvider.java b/src/main/java/libKonogonka/fs/NCA/NCAProvider.java index 2d6bef5..74200c2 100644 --- a/src/main/java/libKonogonka/fs/NCA/NCAProvider.java +++ b/src/main/java/libKonogonka/fs/NCA/NCAProvider.java @@ -255,15 +255,14 @@ public class NCAProvider { byte[] rightsIdKey = hexStrToByteArray(keys.get(byteArrToHexStringAsLE(rightsId))); // throws NullPointerException SecretKeySpec skSpec = new SecretKeySpec( - hexStrToByteArray(keys.get(String.format("titlekek_%02x", cryptoTypeReal)) - ), "AES"); + hexStrToByteArray(keys.get(String.format("titlekek_%02x", cryptoTypeReal))), "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, skSpec); return cipher.doFinal(rightsIdKey); } catch (Exception e){ - throw new Exception("No title.keys loaded for '"+ - String.format("titlekek_%02x", cryptoTypeReal)+"' or '"+ byteArrToHexStringAsLE(rightsId)+"'? ("+e+")", e); + throw new Exception(String.format("No title.keys loaded for 'titlekek_%02x' or '%s' (%s)", + cryptoTypeReal, byteArrToHexStringAsLE(rightsId), e), e); } } private void setupNcaContentByNumber(int number, byte[] key){