android - 图像解密不工作android

标签 android encryption

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

我试过下面的代码。

//加密

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/

相关文章:

c# - CryptDeriveKey 算法名称

android - 使用 gradle 从 Intellij IDEA 项目迁移到 Android-studio

javascript - 替代 getUserMedia() 在 iOS 上读取实时摄像头流?

hadoop - 使用 oozie 工作流的 s3 服务器端加密

javascript - 如何在浏览器中使用 Javascript 加密/解密任意二进制文件?

algorithm - 我的 RSA 算法只适用于非常短的单词

linux - Linux 中的加密

android - 如何在 Android Studio 中制作颜色数组

android - 为所有单选按钮添加事件监听器

android - 我们如何使用 webview 在 Android 应用程序中播放 YouTube 嵌入代码?