android 隐藏文本,Android使用facebook隐藏库加密plaint文本

我尝试使用以下代码加密明文.代码似乎加密了文本但它没有解密到明文.我究竟做错了什么 ?

代码:

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

解决方法:

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

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-conceal

来源: https://codeday.me/bug/20190702/1359928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值