Java 基于AES算法的图像加密

Java 基于AES算法的图像加密,java,encryption,cryptography,aes,Java,Encryption,Cryptography,Aes,我想加密图像的内容,这样我就可以看到它与图像查看器加密 对此,我尝试加密图像像素值,我指的是RGB值 所以我所做的就是: 1-从图像中获取所有RGB值 2-将所有RGB值存储到整数数组中 3-将整数数组转换为字节数组,用于AES输入加密 4-从加密中获取输出并转换为整数数组 5-设置新整数数组中的新RGB值 但是所有这些辛苦的工作都没有表现出来,我看不到输出图像,因为AES算法的输出值太大了!!,大于255且RGB值必须介于0-255之间 public class img { s

我想加密图像的内容,这样我就可以看到它与图像查看器加密

对此,我尝试加密图像像素值,我指的是RGB值

所以我所做的就是:

1-从图像中获取所有RGB值

2-将所有RGB值存储到整数数组中

3-将整数数组转换为字节数组,用于AES输入加密

4-从加密中获取输出并转换为整数数组

5-设置新整数数组中的新RGB值

但是所有这些辛苦的工作都没有表现出来,我看不到输出图像,因为AES算法的输出值太大了!!,大于255且RGB值必须介于0-255之间

public class img {
        static String IV = "AAAAAAAAAAAAAAAA";
        static String encryptionKey = "0123456789abcdef";

static public void main(String args[]) throws Exception {
    try {
        BufferedImage image;
        int width;
        int height;

        File input = new File("C:\\Users\\AKRAM\\Desktop\\sample.jpg");
        image = ImageIO.read(input);
        width = image.getWidth();
        height = image.getHeight();

        int[] t = new int[width * height * 3];
        int k = 0;
        int kk = 0;

        // fill the table t with RGB values;
        for (int i = 0; i < height; i++) {

            for (int j = 0; j < width; j++) {

                Color c = new Color(image.getRGB(j, i));
                int r = c.getRed();
                int g = c.getGreen();
                int b = c.getBlue();

                t[k] = r;
                k++;
                t[k] = g;
                k++;
                t[k] = b;
                k++;

            }
        }

        // convert table of RGB values into byte Array for the Encryption
        byte[] bb = integersToBytes(t);

        /* AES Encryption */
        byte[] cipher = encrypt(bb, encryptionKey);

        t = convertByte2Int(cipher);

        // create image with table RGB values;
        for (int i = 0; i < height; i++) {

            for (int j = 0; j < width; j++) {

                int r = t[kk];
                kk++;
                int g = t[kk];
                kk++;
                int b = t[kk];
                kk++;

                Color newColor = new Color(r, g, b);
                image.setRGB(j, i, newColor.getRGB());

            }
        }
        //write the output image
        File ouptut = new File("C:\\Users\\AKRAM\\Desktop\\output.jpg");
        ImageIO.write(image, "jpg", ouptut);

    } catch (Exception e) {
    }
}// end main

public static byte[] encrypt(byte[] plainText, String encryptionKey) throws Exception {
    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
    SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
    cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
    return cipher.doFinal(plainText);
}

public static byte[] integersToBytes(int[] values) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    for (int i = 0; i < values.length; ++i) {
        dos.writeInt(values[i]);
    }

    return baos.toByteArray();
}

public static int[] convertByte2Int(byte buf[]) {
    int intArr[] = new int[buf.length / 4];
    int offset = 0;
    for (int i = 0; i < intArr.length; i++) {
        intArr[i] = (buf[3 + offset] & 0xFF) | ((buf[2 + offset] & 0xFF) << 8) | ((buf[1 + offset] & 0xFF) << 16)
                | ((buf[0 + offset] & 0xFF) << 24);
        offset += 4;
    }
    return intArr;
  }
公共类img{
静态字符串IV=“aaaaaaaaaaaaaa”;
静态字符串encryptionKey=“0123456789abcdef”;
静态公共void main(字符串args[])引发异常{
试一试{
缓冲图像;
整数宽度;
内部高度;
文件输入=新文件(“C:\\Users\\AKRAM\\Desktop\\sample.jpg”);
图像=图像IO.read(输入);
宽度=image.getWidth();
高度=image.getHeight();
int[]t=新int[宽度*高度*3];
int k=0;
int kk=0;
//用RGB值填充表t;
对于(int i=0;iintArr[i]=(buf[3+offset]&0xFF)|((buf[2+offset]&0xFF)我希望这会对您有所帮助。它并不能完成全部工作(我们不是来给您做学校作业的),但可以帮助您解决遇到的问题。将它与原始代码进行比较,以了解您在哪里犯了错误(不止一个)

包kulatamicuda.aesimage.core;
导入java.awt.Color;
导入java.awt.image.buffereImage;
导入java.io.File;
导入javax.imageio.imageio;
/**
*Stacko的示例类。
* 
*@作者kulatamicuda
*
*/
公共期末班{
/**
*RGB大小为3(红色、绿色、蓝色)。
*/
私有静态最终整数RGB_大小=3;
/**
*有符号->无符号的字节移位器。
*/
专用静态最终int BSHIFT=0xFF;
/**
*主溶液样品。
* 
*@param args
*忽略参数
*/
公共静态void main(字符串[]args){
试一试{
缓冲图像;
整数宽度;
内部高度;
文件输入=新文件(“sample.jpg”);
图像=图像IO.read(输入);
宽度=image.getWidth();
高度=image.getHeight();
字节[]t=新字节[宽度*高度*RGB_大小];
int指数=0;
//用RGB值填充表t;
对于(int i=0;i
我希望这会对您有所帮助。它不会起作用
package kulatamicuda.aesimage.core;

import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

/**
 * Sample class for Stacko.
 * 
 * @author kulatamicuda
 *
 */
public final class Img {

  /**
   * RGB SIZE IS 3 (RED, GREEN, BLUE).
   */
  private static final int RGB_SIZE = 3;

  /**
   * Byte shifter for SIGNED->UNSIGNED.
   */
  private static final int BSHIFT = 0xFF;

  /**
   * Solution sample in main.
   * 
   * @param args
   *          ignored args
   */
  public static void main(String[] args) {
    try {
      BufferedImage image;
      int width;
      int height;

      File input = new File("sample.jpg");
      image = ImageIO.read(input);
      width = image.getWidth();
      height = image.getHeight();

      byte[] t = new byte[width * height * RGB_SIZE];
      int index = 0;

      // fill the table t with RGB values;
      for (int i = 0; i < height; i++) {

        for (int j = 0; j < width; j++) {

          Color c = new Color(image.getRGB(j, i));

          // As byte is SIGNED in Java overflow will occur for values > 127
          byte r = (byte) c.getRed();
          byte g = (byte) c.getGreen();
          byte b = (byte) c.getBlue();

          t[index++] = r;
          t[index++] = g;
          t[index++] = b;
        }
      }

      // Re-create image with table-encrypted RGB values
      BufferedImage newImage = new BufferedImage(width, height,
          BufferedImage.TYPE_3BYTE_BGR);
      index = 0;
      for (int i = 0; i < height; i++) {

        for (int j = 0; j < width; j++) {

          // Need to deal with values < 0 so binary AND with 0xFF
          // Java 8 provides Byte.toUnsignedInt but I am from the old school ;-)
          int r = t[index++] & BSHIFT;
          int g = t[index++] & BSHIFT;
          int b = t[index++] & BSHIFT;

          Color newColor = new Color(r, g, b);
          newImage.setRGB(j, i, newColor.getRGB());

        }
      }
      // write the output image
      File output = new File("output.jpg");
      ImageIO.write(newImage, "jpg", output);

    } catch (Exception e) {
      e.printStackTrace();
    }
  }

}