2015-02-07 58 views
0

我必须在加密图像(Stegnography)中嵌入文本。我搜索并找到了在图像中嵌入文本的代码。但我必须首先加密图像并将文本嵌入到此加密图像中。我的尝试如下。从java中的图像读取加密字节

/* 
* To change this template, choose Tools | Templates 
* and open the template in the editor. 
*/ 
package tbn; 

import java.awt.Graphics2D; 
import java.awt.Point; 
import java.awt.Transparency; 
import java.awt.image.BufferedImage; 
import java.awt.image.ColorModel; 
import java.awt.image.ComponentColorModel; 
import java.awt.image.DataBuffer; 
import java.awt.image.DataBufferByte; 
import java.awt.image.Raster; 
import java.awt.image.WritableRaster; 
import java.io.File; 
import java.io.IOException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import java.security.SecureRandom; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.crypto.BadPaddingException; 
import javax.crypto.Cipher; 
import javax.crypto.IllegalBlockSizeException; 
import javax.crypto.KeyGenerator; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.SecretKey; 
import javax.imageio.ImageIO; 

/** 
* 
* @author user 
*/ 
public class DbtClass { 

    public static void main(String[] args) { 
     try { 
      BufferedImage orgnlimage = ImageIO.read(new File("parrruuuuu.png")); 
      orgnlimage = user_space(orgnlimage); 
      byte[] orgnlimagebytes = get_byte_data(orgnlimage); 
      byte[] encryptedbytes = encrypt(orgnlimagebytes, "abc"); 
      BufferedImage encryptedimage = toImage(encryptedbytes, orgnlimage.getWidth(), orgnlimage.getHeight()); 
      ImageIO.write(encryptedimage, "png", new File("encrypted.png")); 

      ///////////////////////////////////////////////////////////////////// 
      ///////////////////////////////////////////////////////////////////// 

      byte[] encryptedbytes2 = get_byte_data(encryptedimage); 
      System.out.println("encryptedbytes before writing: "+encryptedbytes2.length); 

      BufferedImage encryptedimage3 = ImageIO.read(new File("encrypted.png")); 
      byte[] encryptedbyte3 = get_byte_data(encryptedimage3); 
      System.out.println("encryptedbytes after writing: "+encryptedbyte3.length); 


     } catch (IOException ex) { 
      Logger.getLogger(DbtClass.class.getName()).log(Level.SEVERE, null, ex); 
     } 
    } 

    public static BufferedImage user_space(BufferedImage image) { 
     //create new_img with the attributes of image 
     BufferedImage new_img = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR); 
     Graphics2D graphics = new_img.createGraphics(); 
     graphics.drawRenderedImage(image, null); 
     graphics.dispose(); //release all allocated memory for this image 
     return new_img; 
    } 

    public static byte[] get_byte_data(BufferedImage image) { 
     WritableRaster raster = image.getRaster(); 
     DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer(); 
     return buffer.getData(); 
    } 

    public static byte[] encrypt(byte[] orgnlbytes, String key) { 
     byte[] encbytes = null; 
     try { 
      Cipher cipher = Cipher.getInstance("AES"); 
      KeyGenerator keyGen = KeyGenerator.getInstance("AES"); 
      SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); 
      // cryptograph. secure random 
      random.setSeed(key.getBytes()); 

      keyGen.init(128, random); 
      // for example 
      SecretKey secretKey = keyGen.generateKey(); 
      cipher.init(Cipher.ENCRYPT_MODE, secretKey); 
      encbytes = cipher.doFinal(orgnlbytes); 
     } catch (NoSuchAlgorithmException ex) { 
      Logger.getLogger(DbtClass.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (NoSuchPaddingException ex) { 
      Logger.getLogger(DbtClass.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (InvalidKeyException ex) { 
      Logger.getLogger(DbtClass.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (IllegalBlockSizeException ex) { 
      Logger.getLogger(DbtClass.class.getName()).log(Level.SEVERE, null, ex); 
     } catch (BadPaddingException ex) { 
      Logger.getLogger(DbtClass.class.getName()).log(Level.SEVERE, null, ex); 
     } 
     return encbytes; 
    } 

    public static BufferedImage toImage(byte[] imagebytes, int width, int height) { 
     DataBuffer buffer = new DataBufferByte(imagebytes, imagebytes.length); 
     WritableRaster raster = Raster.createInterleavedRaster(buffer, width, height, 3 * width, 3, new int[]{2, 1, 0}, (Point) null); 
     ColorModel cm = new ComponentColorModel(ColorModel.getRGBdefault().getColorSpace(), false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE); 
     return new BufferedImage(cm, raster, true, null); 
    } 
} 

在这里,我使用光栅类和写入的加密图像ImageIO.write(),然后从文件中读取使用ImageIO.read()此加密的字节。写入所述图像和所述字节之前的加密的字节[] []看完图像后总是不一样

+0

但是在将图像写入文件之前,我从BufferedImage获得了额外的16字节,您提到过。所以我认为问题可能出现在ImageIO.write()或ImageIO.read()中,无论如何谢谢你的评论 @ Reti43 – TBN 2015-02-07 17:31:53

+0

“隐写术是在另一个文件中隐藏文件,消息,图像或视频的艺术或做法,消息,图片或视频。“如果你认为你需要加密图像来执行隐写术,那么你完全不在路上。 – 2015-02-07 17:41:35

+0

您是否第一次将邮件嵌入到运营商中,然后加密了运营商?为什么你甚至要加密载体图像? “* image无法从字节*中识别”是什么意思? – Reti43 2015-02-07 18:22:46

回答

1

所以这里发生了什么。假设尺寸为WxH的原始图像。由于每个像素有3个字节,因此您的图像orgnlimagebytes的字节数为S = 3*W*H

现在您使用AES加密此图像,结果为fixed block size of 16 bytes。如果S不能被16整除,它将被填充。如果它可以被16整除,则会添加另一个16字节的块。这里的要点是加密的字节数组encryptedbytes的尺寸大于orgnlimagebytes。致电S'

现在您使用方法toImage从该字节数组中创建一个BufferedImage。你创建一个缓冲区encryptedbytes,把它变成光栅和等等,等等等等。你最终会得到尺寸为WxH的图像。但是,会发生什么情况是BufferedImage对象具有对具有S'元素的缓冲区的引用。您只使用第一个S元素来构建图像的像素,但仍可以从缓冲区访问剩余的元素。所以当你再次将BufferedImage转换成一个字节数组时,你会得到所有的S'元素。

该图像只有WxH RGB像素,所以如果您尝试将其保存到图像文件,这就是您要保存的所有内容。您不会保存缓冲区引用中的任何附加字节。所以当你保存并加载图像并将其转换为字节数组时,expectedbytes3,你会得到预期的字节数,应该是S


这解释了在保存到文件之前和之后加密字节数组的意外不一致性。但是,除了加密方法,为什么你甚至要加密封面图像?如果您在将消息隐藏在图像中之前对其进行了加密,那么在有人设法检测并提取消息的情况下提供额外的安全性是有意义的。加密封面图像的像素值意味着大幅改变它们,这引入了明显的变化。

+0

http://stackoverflow.com/questions/28396404/save-encrypted-image-bytes-to-file-and-recover-to-原始图像在爪哇 – TBN 2015-02-08 16:54:08

+0

由于书面语言质量差,这篇论文很难理解,但从来没有谈及AES加密。 – Reti43 2015-02-08 17:05:50