简体   繁体   中英

Decrypt pixels of bitmap

I have an app that encrypts the pixels of an image and then decrypts them.

The encryption code is:

        File file = new File(fullPath, "uoc" +format +".png");

        BufferedOutputStream bos   = new BufferedOutputStream(new FileOutputStream(file));
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  

        ByteBuffer bb = ByteBuffer.allocate(image.getByteCount());
        image.copyPixelsToBuffer(bb);
        byte[] b = bb.array();
        image.copyPixelsFromBuffer(ByteBuffer.wrap(encrypt_image(b, password)));

        image.compress(Bitmap.CompressFormat.PNG, 100, baos);

        byte[] b2         = baos.toByteArray();  
        bos.write(b2);
        bos.flush();
        bos.close();

So right now I can open the file and I see noise, that's just what I want.

Once it works I need to decrypt the content of the image with no success :(

My code is similar than the one to encrypt:

        ByteBuffer bb = ByteBuffer.allocate(image_file.getByteCount());
        image_file.copyPixelsToBuffer(bb);
        byte[] b = bb.array();

        // Decrypt
        decryptedData =  Security.decrypt(key,b);

        image_file.copyPixelsFromBuffer(ByteBuffer.wrap(decryptedData));

The problems is in Security.decrypt(key,b) because it throws pad bloc corrupted exception.

My encryption algorythm is:

  public static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
    byte[] encrypted = cipher.doFinal(clear);
    return encrypted;
}

and decryption:

 public static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception {

    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, skeySpec);
    byte[] decrypted = cipher.doFinal(encrypted);
    return decrypted;
}

And I have checked that the key is the same in both cases.

Anyone has an idea about how to solve that issue?

EDIT:

I have tried the next code but it throws exception as well:

        ByteBuffer bb = ByteBuffer.allocate(image.getByteCount());
        image.copyPixelsToBuffer(bb);
        byte[] b = bb.array();
        image.copyPixelsFromBuffer(ByteBuffer.wrap(Security.encrypt(Security.getRaw(password),b)));

        //****
        ByteBuffer bb2 = ByteBuffer.allocate(image.getByteCount());
        image.copyPixelsToBuffer(bb2);
        byte[] b_aux = bb.array();

        // Decrypt
        image.copyPixelsFromBuffer(ByteBuffer.wrap(Security.decrypt(Security.getRaw(password),b_aux)));
        //*****

        image.compress(Bitmap.CompressFormat.PNG, 100, baos);

Right now I am totally lost and my final project depends on this algorithm.

Anyone can help me??

The problem is that the encrypted bytes no longer fit the image bytes, the array size is going to be lost, and so on. Can't you just write the bytes immediately to a file?

It appears that you are PNG compressing the encrypted content. You need to do this in the reverse order. Perform any compression (such as PNG), then encrypt the result. On the client side, decrypt the content, then decompress.

i think it should look something like:

image.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] encryptedPNG = Security.encrypt(Security.getRaw(password), boas.toByteArray());


//Decrypt
byte[] decryptedPNG = Security.decrypt(Security.getRaw(password), encryptedPNG);

image2.decompress(Bitmap.CompressFormat.PNG, new ByteArrayInputStream(decryptedPNG))

"Is there any algorythm to uncompress a bitmap so I could get the correct pixels?"

---I would very much like to know the answer to this one .I couldn't find a decompress method .currently Im storing the bitmap.compressed version(as a blob field in the database) and would like to continue doing so(i'm aware of the option of storing the whole image before calling compress, dont want to shift to that as yet). I badly need the full image for full image display, the compressed one is perfect for thumbnails and storage, any thoughts welcome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM