我尝试使用以下代码加密明文.代码似乎加密了文本但它没有解密到明文.我究竟做错了什么 ?
代码:
Entity entity = new Entity("password");byte[] ciphertext = crypto.encrypt(("data to encrypt").getBytes(),entity);plaintext = crypto.decrypt(ciphertext,entity)
输出:
Ecrypted text:[B@417a110Decrypted text:[B@417df20
解决方法:
以下代码可以加密/解密字符串
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;}
总结 以上是内存溢出为你收集整理的Android使用facebook隐藏库加密plaint文本全部内容,希望文章能够帮你解决Android使用facebook隐藏库加密plaint文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)