【问题标题】:Android encrypting plaint text using facebook conceal libraryAndroid使用facebook隐藏库加密纯文本
【发布时间】:2015-05-24 05:43:58
【问题描述】:

我尝试使用以下代码加密明文。该代码似乎加密了文本,但它没有解密回明文。我做错了什么?

代码:

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

输出:

Ecrypted text:[B@417a110
Decrypted text:[B@417df20

【问题讨论】:

  • 我现在也有同样的问题,有人知道为什么我们看不到实际的文字吗?

标签: android facebook-conceal


【解决方案1】:

以下代码可以加密/解密字符串

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】:

    我找到了答案。

    原因是我们打印的是字节数组而不是字符串。

    数组将由一组字节组成,这就是我们在 logcat 中打印它们时所看到的。

    要查看实际的字符串,我们只需要将 byte[] 放入一个新的 String(byte[]) - 这是从官方 facebook 示例和我的修改中获取的:

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

    结果:

    08-02 19:31:11.237 29364-29364/?电子/加密加密: 0...... E/加密解密:密码

    【讨论】:

    • 为了将来参考,前面的输出[B@417a110 是Java 的toString() 方法的默认实现。我认为Bbyte[]。如果您打印对象,您可以覆盖toString,以获得自定义输出。
    【解决方案3】:
    Base64.encodeToString(cipherText, Base64.DEFAULT); then store it.
    

    【讨论】:

    【解决方案4】:

    这可能有点太晚了,但我遇到了同样的问题,并在解密后设法得到了纯文本。

    你真正需要做的是使用ByteArrayOutputStream,如下代码:

    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();
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-05
      • 2013-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 2012-12-31
      • 1970-01-01
      相关资源
      最近更新 更多