图像解密不工作android

时间:2016-01-23 04:14:36

标签: 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查看器中显示解密的图像。如何实现这一目标。我已经尝试了很多来找到解决方案。

1 个答案:

答案 0 :(得分:1)

嗯,一方面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();
        } 
    }