-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageEncrypt.java
More file actions
288 lines (214 loc) · 6.65 KB
/
ImageEncrypt.java
File metadata and controls
288 lines (214 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import java.awt.*;
import java.awt.image.*;
import java.util.Random;
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.math.BigInteger;
class ImageEncrypt{
boolean verbose=false;
Random generator;
Cipher cipher;
SecretKeySpec skeySpec;
/** Constructor */
ImageEncrypt() {
try{
// Used for noise
generator = new Random();
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128);
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
skeySpec = new SecretKeySpec(raw, "AES");
cipher = Cipher.getInstance("AES/ECB/NoPadding");
}catch(Exception e){ System.out.println("ERROR: " + e);}
}
/** Creates a new blank image
* @param fill the fill color
* @param w the width of the image
* @param h the height of the image
* @return an image filled with a solid color
*/
public BufferedImage blankImage(Color fill, int w, int h){
int color = fill.getRGB();
BufferedImage image = new BufferedImage(w,h,
BufferedImage.TYPE_4BYTE_ABGR);
for(int i=0;i<image.getWidth();i++){
for(int j=0;j<image.getHeight();j++){
image.setRGB(i,j,color);
}
}
return image;
}
/** Noise an image **/
public BufferedImage addNoise(BufferedImage image){
BufferedImage encImage = new BufferedImage(
image.getWidth(),
image.getHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
for(int i=0;i<image.getWidth();i++){
for(int j=0;j<image.getHeight();j++){
encImage.setRGB(i,j,noiseRGB(image.getRGB(i,j)));
}
}
return encImage;
}
/** add noise to an integer **/
public int noiseRGB(int val){
int encVal = val + generator.nextInt(10000);
return encVal;
}
/** Set the key **/
public void setKey(byte [] key){
skeySpec = new SecretKeySpec(key,"AES");
}
byte [] getKey(){ return skeySpec.getEncoded();}
/** Encrypt an Image **/
public BufferedImage map(BufferedImage image,boolean encrypt,boolean trick) throws Exception{
// Test if the coordinates are O.K.
if(image.getWidth() % 2 != 0 || image.getHeight() % 2 != 0){
throw(new Exception("Image size not multiple of 2 :("));
}
BufferedImage encImage = new BufferedImage(image.getWidth(),image.getHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
if(encrypt){
System.out.println("Encrypting Image ... trick=" + trick);
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
}else{
System.out.println("Decrypting Image ... trick=" + trick);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
}
for(int x=0;x<image.getWidth(); x+=2){
for(int y=0;y<image.getHeight(); y+=2){
if(verbose) System.out.println("Block: (" + x+","+y+") -----");
int counter =0;
byte [] pixelBytes = new byte[16];
// Loop through internal block
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
int val = image.getRGB(x+i,y+j);
// Where is the java t. op?
if(trick && encrypt) val +=x*y;
byte [] sub = intToByteArray(val);
if(verbose){
System.out.println("Val: " + val + " Bytes: ");
printByteArray(sub);
}
for(int k=0;k<4;k++) pixelBytes[(counter)*4+k] = sub[k];
counter++;
}
}
// Cipher
byte [] enc = cipher.doFinal(pixelBytes);
if(verbose){
System.out.println("Block to encode:");
printByteArray(pixelBytes);
System.out.println("Cipher:");
printByteArray(enc);
}
counter =0;
// Re-encode the new image
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
byte [] sub = new byte[4];
for(int k=0;k<4;k++) sub[k] = enc[(counter)*4+k];
int val = byteArrayToInt(sub);
if(trick && !encrypt) val -=x*y;
encImage.setRGB(x+i,y+j,val);
counter++;
}
}
}
}
return encImage;
}
/** Attack!
* @param img1 image for the attack (blank or actual)
* @param img2 image for the attach (blank or actual)
* @return the combined image
* */
public BufferedImage attack(BufferedImage img1, BufferedImage img2) throws Exception{
// Make sure the images are the same size
if(img1.getWidth() != img2.getWidth() || img1.getHeight() != img2.getHeight()){
throw(new Exception("Two images do not have the same dimensions!"));
}
BufferedImage attackImg = new BufferedImage(img1.getWidth(),img1.getHeight(),
BufferedImage.TYPE_4BYTE_ABGR);
for(int i=0;i<attackImg.getWidth();i++){
for(int j=0;j<attackImg.getHeight();j++){
int rgb1 = img1.getRGB(i,j);
int rgb2 = img2.getRGB(i,j);
attackImg.setRGB(i,j,rgb1-rgb2);
}
}
return attackImg;
}
/** Sanity Check **/
public void test()
{
// integer test
int testInt= generator.nextInt();
byte [] testBytes = intToByteArray(testInt);
int returnTestInt = byteArrayToInt(testBytes);
System.out.print("Testing byte conversion ... ");
if(testInt == returnTestInt){ System.out.print("passed");}
else{ System.out.print("failed");}
System.out.println("");
// test cipher
int [] testInts = { generator.nextInt(),
generator.nextInt(),
generator.nextInt(),
generator.nextInt() };
testBytes = new byte[16];
int counter = 0;
for (int i=0;i<2;i++){
for (int j=0;j<2;j++){
int val = testInts[counter];
byte [] sub = intToByteArray(val);
System.out.print("Test Integer[" + (i+j) + "]: " + val + " Bytes: ");
printByteArray(sub);
for(int k=0;k<4;k++) testBytes[(counter)*4+k] = sub[k];
counter++;
}
}
System.out.println("Array to Encrypt:");
printByteArray(testBytes);
byte [] encBytes = null;
try{
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
encBytes = cipher.doFinal(testBytes);
}catch(Exception e){ System.out.println("ERROR: "+e);}
System.out.println("Encrypted Bytes:");
printByteArray(encBytes);
byte [] decBytes = null;
// lets try to decrypt
try{
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
decBytes = cipher.doFinal(encBytes);
}catch(Exception e){ System.out.println("ERROR: "+e);}
System.out.println("Decrypted Bytes:");
printByteArray(decBytes);
}
public static final byte[] intToByteArray(int value)
{
return new byte[] {
(byte)(value >>> 24),
(byte)(value >>> 16),
(byte)(value >>> 8),
(byte)value};
}
public static final int byteArrayToInt(byte [] b)
{
return (b[0] << 24)
+ ((b[1] & 0xFF) << 16)
+ ((b[2] & 0xFF) << 8)
+ (b[3] & 0xFF);
}
public static void printByteArray(byte [] array)
{
System.out.print("{");
for(int i=0;i<array.length;i++)
System.out.print(" " + array[i]);
System.out.println(" }");
}
}