diff --git a/memo/build.gradle b/memo/build.gradle index f474a1a..7defede 100644 --- a/memo/build.gradle +++ b/memo/build.gradle @@ -22,8 +22,6 @@ android { dependencies { implementation("com.zeoflow:zson:1.3.0") - implementation("com.facebook.conceal:conceal:1.1.3@aar") - implementation("androidx.annotation:annotation:1.3.0-alpha01") coreLibraryDesugaring("com.android.tools:desugar_jdk_libs:1.1.5") diff --git a/memo/src/main/java/com/zeoflow/memo/ConcealEncryption.java b/memo/src/main/java/com/zeoflow/memo/ConcealEncryption.java index fcc410e..1c29caf 100644 --- a/memo/src/main/java/com/zeoflow/memo/ConcealEncryption.java +++ b/memo/src/main/java/com/zeoflow/memo/ConcealEncryption.java @@ -1,42 +1,42 @@ package com.zeoflow.memo; -import android.content.Context; +import android.annotation.SuppressLint; import android.util.Base64; -import com.facebook.android.crypto.keychain.AndroidConceal; -import com.facebook.android.crypto.keychain.SharedPrefsBackedKeyChain; -import com.facebook.crypto.Crypto; -import com.facebook.crypto.CryptoConfig; -import com.facebook.crypto.Entity; -import com.facebook.crypto.keychain.KeyChain; +import java.nio.charset.StandardCharsets; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; -import static com.zeoflow.memo.MemoApplication.getContext; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.spec.SecretKeySpec; +@SuppressWarnings({"unused", "RedundantSuppression", "RedundantThrows"}) public class ConcealEncryption implements Encryption { - private final Crypto crypto; private final String encryptionKey; + private final SecretKey secretKey; public ConcealEncryption(String encryptionKey) { - this(getContext(), encryptionKey); + this(encryptionKey, true); } - public ConcealEncryption(Context context, String encryptionKey) + protected ConcealEncryption(String encryptionKey, boolean init) { - this(new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256), encryptionKey); - } - - protected ConcealEncryption(KeyChain keyChain, String encryptionKey) - { - this(AndroidConceal.get().createDefaultCrypto(keyChain), encryptionKey); - } - - protected ConcealEncryption(Crypto crypto, String encryptionKey) - { - this.crypto = crypto; this.encryptionKey = encryptionKey; + int keyLength = 128; + byte[] keyBytes = new byte[keyLength / 8]; + Arrays.fill(keyBytes, (byte) 0x0); + byte[] passwordBytes = encryptionKey.getBytes(StandardCharsets.UTF_8); + int length = Math.min(passwordBytes.length, keyBytes.length); + System.arraycopy(passwordBytes, 0, keyBytes, 0, length); + this.secretKey = new SecretKeySpec(keyBytes, "AES"); } @Override @@ -47,24 +47,33 @@ public String encryptionKey() @Override public boolean init() { - return crypto.isAvailable(); + return secretKey != null; } + @SuppressLint("GetInstance") @Override public String encrypt(String key, String plainText) throws Exception { - Entity entity = Entity.create(key); - byte[] bytes = crypto.encrypt(plainText.getBytes(), entity); - return Base64.encodeToString(bytes, Base64.NO_WRAP); + try + { + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + cipher.init(Cipher.ENCRYPT_MODE, secretKey); + byte[] cipherText = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); + return Base64.encodeToString(cipherText, Base64.NO_WRAP); + } catch (NoSuchAlgorithmException | IllegalBlockSizeException | InvalidKeyException | BadPaddingException | NoSuchPaddingException e) + { + e.printStackTrace(); + } + return null; } + @SuppressLint("GetInstance") @Override public String decrypt(String key, String cipherText) throws Exception { - Entity entity = Entity.create(key); - byte[] decodedBytes = Base64.decode(cipherText, Base64.NO_WRAP); - byte[] bytes = crypto.decrypt(decodedBytes, entity); - return new String(bytes); + Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); + cipher.init(Cipher.DECRYPT_MODE, secretKey); + return new String(cipher.doFinal(Base64.decode(cipherText, Base64.NO_WRAP)), StandardCharsets.UTF_8); } } diff --git a/memo/src/main/java/com/zeoflow/memo/MemoBuilder.java b/memo/src/main/java/com/zeoflow/memo/MemoBuilder.java index eaf7876..a48149b 100644 --- a/memo/src/main/java/com/zeoflow/memo/MemoBuilder.java +++ b/memo/src/main/java/com/zeoflow/memo/MemoBuilder.java @@ -93,7 +93,7 @@ Encryption getEncryption() { if (encryption == null) { - encryption = new ConcealEncryption(context, "sfdgfgsgs"); + encryption = new ConcealEncryption("sfdgfgsgs"); if (!encryption.init()) { encryption = new NoEncryption();