1

I am developing an application that encrypt images from gallery. The encryption works fine. The encrypted image should display only in my application without decrypting the image. The decryption process not working.

I have tried the below code.

//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();
}

//decryption code not working

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();
}

I want to display the decrypted image in Picasso viewer. How to achieve this. I have tried a lot to find the solution.

Jithin Varghese
  • 2,018
  • 3
  • 29
  • 56

1 Answers1

1

Well, for one thing the CipherInputStream isn't being read, use something like this:

            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());
            } 

And you need to include this for the padding beyond "AES"

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

Example:

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();
        } 
    } 
childofthehorn
  • 697
  • 1
  • 4
  • 12
  • 1
    When introducing CBC mode, you should also introduce code for handling the IV. – Ebbe M. Pedersen Jan 23 '16 at 08:21
  • Jithin, have you tried just following a simple string example like here to make sure you have everything else working right? http://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example – childofthehorn Jan 24 '16 at 19:42