Skip to content

Commit

Permalink
Merge pull request #50 from zeoflow/android-encryption
Browse files Browse the repository at this point in the history
Changed the encryption/decryption procedure
  • Loading branch information
teogor authored Apr 24, 2021
2 parents 343b399 + 6add4bb commit b13e175
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 33 deletions.
2 changes: 0 additions & 2 deletions memo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
69 changes: 39 additions & 30 deletions memo/src/main/java/com/zeoflow/memo/ConcealEncryption.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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);
}

}
2 changes: 1 addition & 1 deletion memo/src/main/java/com/zeoflow/memo/MemoBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ Encryption getEncryption()
{
if (encryption == null)
{
encryption = new ConcealEncryption(context, "sfdgfgsgs");
encryption = new ConcealEncryption("sfdgfgsgs");
if (!encryption.init())
{
encryption = new NoEncryption();
Expand Down

0 comments on commit b13e175

Please sign in to comment.