[英]Image Encryption Using AES Algorithm
我想對圖像的內容進行加密,以便可以使用Image Viewer對其進行加密。
為此,我試圖加密圖像像素值,即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;
}
}
我希望這會對您有所幫助。 它並不能完成全部任務(我們不是來做您的學校作業),但是可以幫助您解決所遇到的問題。 將其與原始代碼進行比較,以了解您在哪里犯了錯誤(有多個錯誤)。
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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.