Android text encryption using facebook library

I tried to encrypt plaintext using the code below. The code seems to be encrypted text, but it does not decrypt it back. What am I doing wrong?

The code:

Entity entity = new Entity("password");
byte[] ciphertext = crypto.encrypt(("data to encrypt").getBytes(),entity);
plaintext = crypto.decrypt(ciphertext,entity)

Conclusion:

Ecrypted text:[B@417a110
Decrypted text:[B@417df20
+4
source share
3 answers

The following code can encrypt / decrypt a string

KeyChain keyChain = new SharedPrefsBackedKeyChain(context, CryptoConfig.KEY_256);
crypto = AndroidConceal.get().createDefaultCrypto(keyChain);

public static String encrypt(String key, String value) throws KeyChainException, CryptoInitializationException, IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    OutputStream cryptoStream = crypto.getCipherOutputStream(bout, Entity.create(key));
    cryptoStream.write(value.getBytes("UTF-8"));
    cryptoStream.close();
    String result = Base64.encodeToString(bout.toByteArray(), Base64.DEFAULT);
    bout.close();
    return result;
}

public static String decrypt(String key, String value) throws KeyChainException, CryptoInitializationException, IOException {
    ByteArrayInputStream bin = new ByteArrayInputStream(Base64.decode(value, Base64.DEFAULT));
    InputStream cryptoStream = crypto.getCipherInputStream(bin, Entity.create(key));
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    int read = 0;
    byte[] buffer = new byte[1024];
    while ((read = cryptoStream.read(buffer)) != -1) {
        bout.write(buffer, 0, read);
    }
    cryptoStream.close();
    String result = new String(bout.toByteArray(), "UTF-8");
    bin.close();
    bout.close();
    return result;
}
+2
source

I have found the answer.

The reason is that we printed an array of bytes instead of a string.

The array will consist of a set of bytes so that we can see when we printed them in logcat.

To see the actual line, we just need to put the byte [] in a new line (byte []) - this is taken from the official facebook examples with my changes:

    Crypto crypto = new Crypto(
            new SharedPrefsBackedKeyChain(getActivity()),
            new SystemNativeCryptoLibrary());

    if (!crypto.isAvailable()) {
        Log.e("Crypto","Crypto is missing");
    }
    String password = "Password";
    Entity entity = new Entity("TEST");
    byte[] encryptedPass = new byte[0];
    byte[] b = password.getBytes(Charset.forName("UTF-8"));
    try {
        encryptedPass = crypto.encrypt(b, entity);
        Log.e("Crypto Encrypted", new String(encryptedPass));
        byte[] decryptedPass = crypto.decrypt(encryptedPass, entity);
        Log.e("Crypto Decrypted ", new String(decryptedPass));
    } catch (KeyChainException e) {
        e.printStackTrace();
    } catch (CryptoInitializationException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Results:

08-02 19: 31:11.237 29364-29364/? E/Crypto Encrypted: 0 &? B 6 H ` " 1 xx 08-02 19: 31:11.237 29364-29364/? E/Crypto Decrypted:

0
Base64.encodeToString(cipherText, Base64.DEFAULT); then store it.
0
source

All Articles