diff --git a/app/src/main/java/me/impy/aegis/db/DatabaseFile.java b/app/src/main/java/me/impy/aegis/db/DatabaseFile.java index 86a009b4..20447714 100644 --- a/app/src/main/java/me/impy/aegis/db/DatabaseFile.java +++ b/app/src/main/java/me/impy/aegis/db/DatabaseFile.java @@ -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); diff --git a/app/src/main/java/me/impy/aegis/encoding/Base64.java b/app/src/main/java/me/impy/aegis/encoding/Base64.java new file mode 100644 index 00000000..e26c2a8a --- /dev/null +++ b/app/src/main/java/me/impy/aegis/encoding/Base64.java @@ -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); + } + } +} diff --git a/app/src/main/java/me/impy/aegis/encoding/Base64Exception.java b/app/src/main/java/me/impy/aegis/encoding/Base64Exception.java new file mode 100644 index 00000000..d2c7316b --- /dev/null +++ b/app/src/main/java/me/impy/aegis/encoding/Base64Exception.java @@ -0,0 +1,7 @@ +package me.impy.aegis.encoding; + +public class Base64Exception extends Exception { + public Base64Exception(Throwable cause) { + super(cause); + } +}