问题描述:

Here is my simple image encryption class, in which I am:

  1. Reading a image from one place into bytes
  2. Encrypting those bytes
  3. Again create image from these encrypted bytes

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

}

}

Now I am getting problem in step three when I try to make image from encrypted bytes. It throws an exception given below:

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)

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?

网友答案:

After you've encrypted the data, it's no longer a valid image file. Trying to use ImageIO on the encrypted data is like opening up the result of encrypting a text file in notepad, and expecting to see text.

To get an Image, you need to decrypt the data before you ask ImageIO to read it.

It's not at all clear what your convertByteArrayToImage method is really meant to do - if the aim is just to write the bytes to a file, why go via Image at all? Why not dump the bytes straight to disk? They're already the encrypted representation of an image - there's no need to try to interpret them as an image again. If you want to apply some sort of image transformation (e.g. always writing out a JPEG, even if the input is a PNG) then that should be done on the unencrypted data.

(Likewise on convertImageToByteArray, given that you're starting with a file on disk, there's no point in loading that as an image unless you want to perform an image-specific transformation.)

Side-notes:

  • Don't use the overload of String.getBytes() which doesn't take a charset - it will use the platform default encoding, which means it's not portable across platforms
  • You're converting the encrypted data to base64 as well, but then ignoring the result. Why?
相关阅读:
Top