mirror of
https://github.com/beemdevelopment/Aegis.git
synced 2025-05-14 14:02:49 +00:00
Wrap the Base64 class to prevent a runtime exception for bad input
This commit is contained in:
parent
f1a03638a0
commit
9c433f96cf
3 changed files with 41 additions and 5 deletions
|
@ -1,7 +1,5 @@
|
||||||
package me.impy.aegis.db;
|
package me.impy.aegis.db;
|
||||||
|
|
||||||
import android.util.Base64;
|
|
||||||
|
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
@ -13,6 +11,8 @@ import me.impy.aegis.crypto.MasterKey;
|
||||||
import me.impy.aegis.crypto.MasterKeyException;
|
import me.impy.aegis.crypto.MasterKeyException;
|
||||||
import me.impy.aegis.db.slots.SlotCollection;
|
import me.impy.aegis.db.slots.SlotCollection;
|
||||||
import me.impy.aegis.db.slots.SlotCollectionException;
|
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.Hex;
|
||||||
import me.impy.aegis.encoding.HexException;
|
import me.impy.aegis.encoding.HexException;
|
||||||
|
|
||||||
|
@ -95,10 +95,10 @@ public class DatabaseFile {
|
||||||
|
|
||||||
public JSONObject getContent(MasterKey key) throws DatabaseFileException {
|
public JSONObject getContent(MasterKey key) throws DatabaseFileException {
|
||||||
try {
|
try {
|
||||||
byte[] bytes = Base64.decode((String) _content, Base64.NO_WRAP);
|
byte[] bytes = Base64.decode((String) _content);
|
||||||
CryptResult result = key.decrypt(bytes, _cryptParameters);
|
CryptResult result = key.decrypt(bytes, _cryptParameters);
|
||||||
return new JSONObject(new String(result.Data, "UTF-8"));
|
return new JSONObject(new String(result.Data, "UTF-8"));
|
||||||
} catch (MasterKeyException | JSONException | UnsupportedEncodingException e) {
|
} catch (MasterKeyException | JSONException | UnsupportedEncodingException | Base64Exception e) {
|
||||||
throw new DatabaseFileException(e);
|
throw new DatabaseFileException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -114,7 +114,7 @@ public class DatabaseFile {
|
||||||
byte[] dbBytes = string.getBytes("UTF-8");
|
byte[] dbBytes = string.getBytes("UTF-8");
|
||||||
|
|
||||||
CryptResult result = key.encrypt(dbBytes);
|
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;
|
_cryptParameters = result.Parameters;
|
||||||
} catch (MasterKeyException | UnsupportedEncodingException | JSONException e) {
|
} catch (MasterKeyException | UnsupportedEncodingException | JSONException e) {
|
||||||
throw new DatabaseFileException(e);
|
throw new DatabaseFileException(e);
|
||||||
|
|
29
app/src/main/java/me/impy/aegis/encoding/Base64.java
Normal file
29
app/src/main/java/me/impy/aegis/encoding/Base64.java
Normal file
|
@ -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…
Add table
Add a link
Reference in a new issue