6

I tried to encrypt plaintext using the below code. The code seems encrypt the text but it doesnt decrypt to plaintext 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)

Output:

Ecrypted text:[B@417a110
Decrypted text:[B@417df20
kirron s
  • 61
  • 1
  • 3

4 Answers4

3

The following code can encrypt/decrypt 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;
}
Desmond Lua
  • 6,142
  • 4
  • 37
  • 50
1

I have found the answer.

The reason is we were printing the byte array instead of the String.

The array is going to comprise of a set of bytes so that's what we saw when we printed them out in the logcat.

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

    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﹕ Password

Simon
  • 19,658
  • 27
  • 149
  • 217
  • 2
    For future reference, the previous ouput `[B@417a110` was the default implementation of Java's `toString()` method. I presume the `B` is for `byte[]`. You can override `toString` if you print the object, to get custom output. – Chris Cirefice Aug 02 '15 at 17:38
0
Base64.encodeToString(cipherText, Base64.DEFAULT); then store it.
0

It might be a little too late but I had the same issue and managed to get the plain text after the decryption.

What you really need to do is to use ByteArrayOutputStream like the following code:

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

ByteArrayOutputStream out = new ByteArrayOutputStream();
out.write(plainText, 0, plainText.length);
String decryptedPassword = out.toString();
out.close();

Hope this helps.

fruqi
  • 4,983
  • 4
  • 26
  • 32