Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to decrypt and read encrypted image file
    primarykey
    data
    text
    <p>Here is my simple image encryption class, in which I am:</p> <ol> <li>Reading a image from one place into bytes</li> <li>Encrypting those bytes</li> <li>Again create image from these encrypted bytes</li> </ol> <p>Code:</p> <pre><code>public class ImageEncrypt { Cipher cipher; public static byte[] convertImageToByteArray(String sourcePath) { byte[] imageInByte = null; try{ BufferedImage originalImage = ImageIO.read(new File( sourcePath)); // convert BufferedImage to byte array ByteArrayOutputStream baos = new ByteArrayOutputStream(); ImageIO.write(originalImage, "jpg", baos); baos.flush(); imageInByte = baos.toByteArray(); baos.close(); }catch(Exception e){ e.printStackTrace(); } return imageInByte; } public static void convertByteArrayToImage(byte[] b , String path) { try{ InputStream in = new ByteArrayInputStream(b); BufferedImage bImageFromConvert = ImageIO.read(in); ImageIO.write(bImageFromConvert, "jpg", new File( path)); }catch(Exception e) { e.printStackTrace(); } } public static void main(String args []){ final String strPassword = "password12345678"; SecretKeySpec initVector = new SecretKeySpec(strPassword.getBytes(), "AES"); AlgorithmParameterSpec paramSpec = new IvParameterSpec(strPassword.getBytes()); try{ Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, initVector, paramSpec); byte[] ecrypted = cipher.doFinal(convertImageToByteArray("C:/Users/user/Desktop/a.jpg")); convertByteArrayToImage(ecrypted,"C:/Users/user/user/enc.jpg"); System.out.println("converted to encrypted file .... "); }catch(Exception e){ e.printStackTrace(); } } </code></pre> <p>Now I am getting problem in step three when I try to make image from encrypted bytes. It throws an exception given below:</p> <pre><code>java.lang.IllegalArgumentException: image == null! at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925) at javax.imageio.ImageIO.getWriter(ImageIO.java:1591) at javax.imageio.ImageIO.write(ImageIO.java:1520) at ImageEncrypt.convertByteArrayToImage(ImageEncrypt.java:55) at ImageEncrypt.main(ImageEncrypt.java:83) </code></pre> <p>I don't know where I am going wrong? I am doing the same to convert files (docs, pdf, etc.) to encrypt and it works fine (of course in that case I am using different stream classes for byte conversion) but I am unable to understand why it is messing up here?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload