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)
我已经找到答案了。
\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 }\nRun Code Online (Sandbox Code Playgroud)\n\n结果:
\n\n\n\n08-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
| 归档时间: |
|
| 查看次数: |
2022 次 |
| 最近记录: |