Android使用Facebook隐藏库加密原始文本

kir*_*n s 5 android facebook-conceal

我尝试使用以下代码对纯文本进行加密。该代码似乎对文本进行了加密,但是并没有将其解密为纯文本。我究竟做错了什么 ?

代码:

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

输出:

Ecrypted text:[B@417a110
Decrypted text:[B@417df20
Run Code Online (Sandbox Code Playgroud)

Sim*_*mon 1

我已经找到答案了。

\n\n

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

\n\n

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

\n\n

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

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

结果:

\n\n
\n

08-02 19:31:11.237 29364-29364/?E/加密加密\xef\xb9\x95\n 0\xef\xbf\xbd\xef\xbf\xbd&\xef\xbf\xbd?B\xef\xbf\xbd6\xef\xbf\xbd\xef\xbf\ xbd\xef\xbf\xbdH\xef\xbf\xbd\xef\xbf\xbd\xef\xbf\xbd`\xef\xbf\xbd\xef\xbf\xbd"\xef\xbf\xbd1\xef\xbf\ xbd\xef\xbf\xbdxx\xef\xbf\xbd 08-02 19:31:11.237 29364-29364/?\n E/加密解密\xef\xb9\x95 密码

\n
\n

  • 为了将来参考,之前的输出“[B@417a110”是 Java 的“toString()”方法的默认实现。我认为“B”代表“byte[]”。如果打印对象,您可以覆盖“toString”以获取自定义输出。 (2认同)