Android 即时解密图像

发布于 12-18 12:12 字数 1170 浏览 5 评论 0原文

我正在尝试在我的应用程序中动态解密图像。我正在通过互联网下载它们,然后我想解密它们并将它们显示为背景或在图像视图中。所以我的问题是我无法弄清楚如何构建整个逻辑。

现在我正在通过我编写的方法生成的路径从SD卡获取图像:

    public static String getImagePathFromExternalStorage(String server, int userId, String filename){
        String path=Environment.getExternalStorageDirectory()
        + "/Documents/Users/"+server+"/"+userId+"/Storage/" + filename;

        return path;
    }

    public static String getImagePathFromInternalStorage(String server, int userId, String filename, Context context){
        String path = context.getFilesDir() + "/documents/users/"+server+"/"+userId+"/storage/"+filename;

        return path;
    }

并且我正在解密这样的图像:

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
ByteArrayInputStream input = new ByteArrayInputStream(mediaCollBuffer); 
CipherInputStream cis = new CipherInputStream(input, cipher);

所以我试图弄清楚如何通过文件的路径获取文件,解密它是动态的并将其设置为背景。有什么建议吗?

I'm trying to decrypt images on the fly in my application. I'm downloading them over internet and than I want to decrypt them and show 'em as backgrounds or in imageview. So my problem is that I can't figure it out how to make the whole logic.

For now I'm getting the images from sdcard by their path which is generated by a method i wrote :

    public static String getImagePathFromExternalStorage(String server, int userId, String filename){
        String path=Environment.getExternalStorageDirectory()
        + "/Documents/Users/"+server+"/"+userId+"/Storage/" + filename;

        return path;
    }

    public static String getImagePathFromInternalStorage(String server, int userId, String filename, Context context){
        String path = context.getFilesDir() + "/documents/users/"+server+"/"+userId+"/storage/"+filename;

        return path;
    }

And I'm decrypting images like this :

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
ByteArrayInputStream input = new ByteArrayInputStream(mediaCollBuffer); 
CipherInputStream cis = new CipherInputStream(input, cipher);

So I'm trying to figure it out how to get the file by it's path, decrypt it on the fly and set it as background. Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷子口的你2024-12-25 12:12:30

试试这个:

File bufferFile = new File(path);
FileInputStream fis   = new FileInputStream(bufferFile);

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
CipherInputStream cis = new CipherInputStream(fis, cipher);
Bitmap ops = BitmapFactory.decodeStream(cis);
logo.setImageBitmap(ops);

我认为这应该有帮助。

Try this :

File bufferFile = new File(path);
FileInputStream fis   = new FileInputStream(bufferFile);

Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
SecretKeySpec keySpec = new SecretKeySpec("01234567890abcde".getBytes(), "AES");
IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes());
cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec);
CipherInputStream cis = new CipherInputStream(fis, cipher);
Bitmap ops = BitmapFactory.decodeStream(cis);
logo.setImageBitmap(ops);

I think this should help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文