kirron s
kirron s

Reputation: 61

Android encrypting plaint text using facebook conceal library

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

Upvotes: 6

Views: 2592

Answers (4)

fruqi
fruqi

Reputation: 5353

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.

Upvotes: 0

Desmond Lua
Desmond Lua

Reputation: 6290

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;
}

Upvotes: 3

FAHAD HAMMAD ALOTAIBI
FAHAD HAMMAD ALOTAIBI

Reputation: 143

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

Upvotes: 0

Simon
Simon

Reputation: 19938

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

Upvotes: 1

Related Questions