Wrap the Base64 class to prevent a runtime exception for bad input

pull/41/head
Alexander Bakker 7 years ago
parent f1a03638a0
commit 9c433f96cf

@ -1,7 +1,5 @@
package me.impy.aegis.db;
import android.util.Base64;
import org.json.JSONException;
import org.json.JSONObject;
@ -13,6 +11,8 @@ import me.impy.aegis.crypto.MasterKey;
import me.impy.aegis.crypto.MasterKeyException;
import me.impy.aegis.db.slots.SlotCollection;
import me.impy.aegis.db.slots.SlotCollectionException;
import me.impy.aegis.encoding.Base64;
import me.impy.aegis.encoding.Base64Exception;
import me.impy.aegis.encoding.Hex;
import me.impy.aegis.encoding.HexException;
@ -95,10 +95,10 @@ public class DatabaseFile {
public JSONObject getContent(MasterKey key) throws DatabaseFileException {
try {
byte[] bytes = Base64.decode((String) _content, Base64.NO_WRAP);
byte[] bytes = Base64.decode((String) _content);
CryptResult result = key.decrypt(bytes, _cryptParameters);
return new JSONObject(new String(result.Data, "UTF-8"));
} catch (MasterKeyException | JSONException | UnsupportedEncodingException e) {
} catch (MasterKeyException | JSONException | UnsupportedEncodingException | Base64Exception e) {
throw new DatabaseFileException(e);
}
}
@ -114,7 +114,7 @@ public class DatabaseFile {
byte[] dbBytes = string.getBytes("UTF-8");
CryptResult result = key.encrypt(dbBytes);
_content = new String(Base64.encode(result.Data, Base64.NO_WRAP), "UTF-8");
_content = Base64.encode(result.Data);
_cryptParameters = result.Parameters;
} catch (MasterKeyException | UnsupportedEncodingException | JSONException e) {
throw new DatabaseFileException(e);

@ -0,0 +1,29 @@
package me.impy.aegis.encoding;
import java.io.UnsupportedEncodingException;
public class Base64 {
private static final int _flags = android.util.Base64.NO_WRAP;
private Base64() {
}
public static byte[] decode(String s) throws Base64Exception {
try {
return android.util.Base64.decode(s, _flags);
} catch (IllegalArgumentException e) {
throw new Base64Exception(e);
}
}
public static String encode(byte[] data) {
byte[] encoded = android.util.Base64.encode(data, _flags);
try {
return new String(encoded, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}

@ -0,0 +1,7 @@
package me.impy.aegis.encoding;
public class Base64Exception extends Exception {
public Base64Exception(Throwable cause) {
super(cause);
}
}
Loading…
Cancel
Save