Я не могу поймать исключение в этом коде. Посмотрите в коде комментарии для подсказок. Очевидно, сериализация работала нормально, поэтому я не буду вставлять код метода сериализации.
public class NewCipher {
private static final String password = "somestatickey";
private Cipher desCipher;
private SecretKey secretKey;
private Context ctx;
public NewCipher(Context ctx) throws Exception {
this.ctx = ctx;
// Create Key
byte key[] = password.getBytes();
DESKeySpec desKeySpec = new DESKeySpec(key);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
secretKey = keyFactory.generateSecret(desKeySpec);
// Create Cipher
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
}
Здесь начинается сложная часть:
public ArrayList<Category> loadCategories(){
try {
try {
// Change cipher mode
desCipher.init(Cipher.DECRYPT_MODE, secretKey); //some uncatchable exception seems to be appearing here
// Create stream
FileInputStream fis;
fis = ctx.openFileInput("categories.des");
BufferedInputStream bis = new BufferedInputStream(fis);
CipherInputStream cis = new CipherInputStream(bis, desCipher);
ObjectInputStream ois = new ObjectInputStream(cis);
try {
// Read objects
ArrayList<Category> categories = (ArrayList<Category>) ois.readObject(); //however the debugger goes right to this line and then goes to the finally, and then straight to final catch block
return categories; //not beeing executed
}
finally {
ois.close(); //debugger does a step here and then jumps to the end
}
}
catch(GeneralSecurityException ex) {
Log.v("Debug", "Some message", ex); //not beeing executed
return null; //not beeing executed
}
} catch (Exception e) {
Log.v("Debug", "Some message", e); //not beeing executed
return null; //actually the debugger jumps right here avoiding the log line above
}
}
Откуда мне знать, что проблема в строке desCipher.init(Cipher.DECRYPT_MODE, secretKey);
? Я удалял строку один за другим и всегда получал один и тот же результат. Что-то не так должно было случиться в первой строке.
К сожалению, я не могу его поймать, и по какой-то причине код пытается выполняться дальше. Я полностью запутался здесь. Я пробовал IOException
и IllegalStateException
вместо GeneralSecurityException
. Также попытался скинуть BadPaddingException. журналов нет.
Пожалуйста, мне нужна помощь в этом.