Я не могу поймать исключение в этом коде. Посмотрите комментарии к коду для подсказок. По-видимому, сериализация работала нормально, поэтому я не буду вставлять код метода сериализации.Deserializing Encrypted ObjectList
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");
}
Tricky часть начинается здесь:
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. однако никаких журналов.
Пожалуйста, мне нужна помощь.
эта строка 'Log.v (« Debug »,« Some message », e);' сообщает вам, какое это исключение. это первый шаг – njzk2
проблема в том, что строка не выполняется –
, а затем поймать Throwable, а не исключение – njzk2