gpt4 book ai didi

Android使用facebook Conceal 库加密纯文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:43:43 25 4
gpt4 key购买 nike

我尝试使用以下代码加密明文。该代码似乎对文本进行了加密,但并未解密为明文。我做错了什么?

代码:

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 库加密纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30420478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com