gpt4 book ai didi

android - 如何在不保存到设备的情况下在 ImageView 中加载 Cipher 加密的图像文件

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

我正在创建一个具有内容安全性的应用程序,因为任何人都无法复制内容和文件。我正在使用密码直接从 URL 加密图像,而无需下载到设备。请在下面找到我的代码。

URL url = new URL(images.getImageurl());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
File folder = new File(Environment.getExternalStorageDirectory(), "zerb");
boolean success = true;

if (!folder.exists()){
folder.mkdirs();
}
InputStream fis = connection.getInputStream();
String path = folder.getAbsolutePath() + "images.getImageName + ".jpg";
encryptfile(fis, path, AppConstants.password + images.getContentid() + images.getTopicid())
fis.close();

而密码加密方法代码为

private static boolean encryptfile(InputStream inputStream, String path, String password) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {

FileOutputStream fos = new FileOutputStream(path.concat(".crypt"));
byte[] key = (AppConstants.salt + password).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sks);
CipherOutputStream cos = new CipherOutputStream(fos, cipher);
int b;
byte[] d = new byte[8];
while ((b = inputStream.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
inputStream.close();
File encryptedFile = new File(path.concat(".crypt"));
return (encryptedFile.exists());
}

解密密码是

 public static void decrypt(String path, String password, String outPath) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
FileOutputStream fos = new FileOutputStream(outPath);
byte[] key = (AppConstants.salt + password).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);
int b;
byte[] d = new byte[8];
while ((b = cis.read(d)) != -1) {
fos.write(d, 0, b);
}
fos.flush();
fos.close();
cis.close();
}

如果我解密图像,它将显示并可以从设备复制。我只需要将加密图像加载到 ImageView,而不将解密图像保存到设备,这样就没有人可以复制了。请有人帮助我。

最佳答案

ImageView 可以显示内存中的 android.graphics.Bitmap,它可以直接从 InputStream 中读取。

例如,decrypt() 方法可以修改为返回一个 Bitmap :

public Bitmap decrypt(String path, String password) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
FileInputStream fis = new FileInputStream(path);
byte[] key = (AppConstants.salt + password).getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sks);
CipherInputStream cis = new CipherInputStream(fis, cipher);

Bitmap bitmap = BitmapFactory.decodeStream(cis);
cis.close();

return bitmap;
}

(虽然叫Bitmap,但是解码成.jpg或者.png就可以了)

然后可以在 ImageView 中显示:

ImageView imageView = findViewById(R.id.imageView);
Bitmap bitmap = decrypt(path + ".crypt", password);
imageView.setImageBitmap(bitmap);

关于android - 如何在不保存到设备的情况下在 ImageView 中加载 Cipher 加密的图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51042823/

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