gpt4 book ai didi

android - 图像解密不工作android

转载 作者:行者123 更新时间:2023-11-30 01:37:25 25 4
gpt4 key购买 nike

我正在开发一个加密图库中图像的应用程序。加密工作正常。加密的图像应该只显示在我的应用程序中而不解密图像。解密过程无效。

我试过下面的代码。

//加密

try {
encrypt(filePath);
} catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
e.printStackTrace();
}

public void encrypt(String image) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; //Choose a key wisely
FileInputStream fis = new FileInputStream(image);
FileOutputStream fos = new FileOutputStream(image);

// Length is 16 byte
SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
// Create cipher
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
// Wrap the output stream
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
// Write bytes
int b;
byte[] d = new byte[8];
while((b = fis.read(d)) != -1) {
cos.write(d, 0, b);
}
// Flush and close streams.
cos.flush();
cos.close();
fis.close();
}

//解密代码无效

try {
decrypt(list.image_path);
} catch (IOException | NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException e) {
e.printStackTrace();
}

Picasso.with(context).load(new File(list.image_path)).error(R.drawable.logo).placeholder(R.drawable.logo)
.into(holder.lock_image);

public void decrypt(String image) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; //Choose a key wisely
FileInputStream fis = new FileInputStream(image);

FileOutputStream fos = new FileOutputStream(image);
SecretKeySpec sks = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);

fos.flush();
fos.close();
cis.close();
}

我想在 Picasso 查看器中显示解密后的图像。如何实现这一目标。我已经尝试了很多方法来找到解决方案。

最佳答案

好吧,一方面 CipherInputStream 没有被读取,使用这样的东西:

            byte[] data = new byte[1024];
int read = cis.read(data);
while (read != -1) {
fos.write(data, 0, read);
read = cis.read(data);
System.out.println(new String(data, "UTF-8").trim());
}

并且您需要将其包含在“AES”之外的填充中

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

示例:

public void decrypt(File inputFile, File outputFile, byte[] key) throws Exception {
Cipher cipher = getCipherDecrypt(key);
FileOutputStream fos = null;
CipherInputStream cis = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(inputFile);
cis = new CipherInputStream(fis, cipher);
fos = new FileOutputStream(outputFile);
byte[] data = new byte[1024];
int read = cis.read(data);
while (read != -1) {
fos.write(data, 0, read);
read = cis.read(data);
System.out.println(new String(data, "UTF-8").trim());
}
} finally {
fos.close();
cis.close();
fis.close();
}
}

关于android - 图像解密不工作android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34959528/

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