-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDigital_Watermarking.java
409 lines (334 loc) · 11.1 KB
/
Digital_Watermarking.java
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package AES;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.Scanner;
import java.util.Collections;
import java.util.HashMap;
import java.awt.DisplayMode;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AES extends dataclient implements Serializable {
static String thisLine = null;
public static BufferedWriter bufferedWriter;
public static String fileName = "db.txt";
public static String filename_encrypt = "encrypt.txt";
static String IV = "AAAAAAAAAAAAAAAA";
static String temp;
static String plaintext = "0000000000000001"; /* Note null padding */
static String encryptionKey = "0123456789abcdef";
public static void main(String[] args) {
int count = 0;
int flag = 1;
// AES[] d = new AES[10001];
ArrayList<Integer> list = new ArrayList<Integer>();
for (int i = 0; i <= 10001; i++) {
list.add(new Integer(i + 1));
}
Collections.shuffle(list);
try {
FileWriter fileWriter = new FileWriter(fileName);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (int i = 0; i < 10000; i++) {
float minX = (float) 1000.0;
float maxX = (float) 9999.0;
Random rand1 = new Random();
Random rand2 = new Random();
Random rand3 = new Random();
Random rand4 = new Random();
Random rand5 = new Random();
float random1 = rand1.nextFloat() * (maxX - minX) + minX;
float random2 = rand2.nextFloat() * (maxX - minX) + minX;
float random3 = rand3.nextFloat() * (maxX - minX) + minX;
float random4 = rand4.nextFloat() * (maxX - minX) + minX;
float random5 = rand5.nextFloat() * (maxX - minX) + minX;
AES(retrivefromit(list, i), random1, random2, random3, random4, random5);
bufferedWriter.write(String.valueOf(retrivefromit(list, i)) + " " + String.valueOf(random1) + " "
+ String.valueOf(random2) + " " + String.valueOf(random3) + " " + String.valueOf(random4) + " "
+ String.valueOf(random5));
bufferedWriter.newLine();
System.out.println("Line number " + i + "done");
}
bufferedWriter.close();
}
catch (Exception e) {
System.out.println("Error writing to file '" + fileName + "'");
}
}
public static int retrivefromit(ArrayList list,int n){
int i=0;
int x=0;
while(i<=n){
x = (int) list.get(i);
i++;
}
return x;
}
try
{
FileWriter p = new FileWriter("final.txt");
BufferedWriter b = new BufferedWriter(p);
FileReader fileWriter = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileWriter);
FileWriter fileWriter1 = new FileWriter(filename_encrypt);
BufferedWriter bufferedReader1 = new BufferedWriter(fileWriter1);
while ((thisLine = bufferedReader.readLine()) != null) {
flag = 1;
if (thisLine.length() == 0)
continue;
String[] splited = thisLine.split(" ");
int digit = 0;
String attach = "0";
String pk = splited[0];
String v = new StringBuilder(pk).reverse().toString();
digit = (int) (Math.log10((double) Integer.parseInt(pk))) + 1;
digit = 16 - digit;
for (int k = 0; k < digit; k++)
v += attach;
String plaintext = new StringBuilder(v).reverse().toString();
// System.out.println("Plaintext is " + plaintext);
// System.out.println("");
byte[] cipher = encrypt(plaintext, encryptionKey);
BigInteger big = BigInteger.valueOf(256);
BigInteger bi = BigInteger.valueOf(0);
for (int i = 0; i < cipher.length; i++) {
BigInteger t = big.pow(i);
BigInteger q = BigInteger.valueOf(1);
q = q.multiply(t);
q = q.multiply(BigInteger.valueOf((long) cipher[i]));
bi = bi.add(q);
}
BigInteger var = BigInteger.valueOf(10);
BigInteger var2 = BigInteger.valueOf(0);
if (bi.remainder(var) == var2) {
count = count + 1;
int w = bi.intValue();
String str = Integer.toBinaryString(w);
String str2 = str.substring(str.length() - 8, str.length());
int k = Integer.parseInt(str2, 2);
double u = k / 255.0;
System.out.println(u);
float x0 = (float) 0.05;
float x1 = (float) 0.85;
float y0 = (float) 0.02;
float y1 = (float) 0.90;
float x = (float) (x0 + u * (x1 - x0));
float y = (float) (y0 + u * (y1 - y0));
System.out.println("value of x and y: " + x + " " + y);
x *= 256;
y *= 256;
System.out.println("after multiplying by 256" + x + " " + y);
String binary_x = Integer.toBinaryString((int) x);
String binary_y = Integer.toBinaryString((int) y);
float floatvalueofa3 = Float.parseFloat(splited[3]);
float floatvalueofa4 = Float.parseFloat(splited[4]);
// System.out.println("Before change attribute a3 : " +
// floatvalueofa3+ " "+ floatvalueofa4);
String valueofmantissaofa3 = mantissa(floatvalueofa3);
String valueofmantissaofa4 = mantissa(floatvalueofa4);
String valueofexponentofa3 = exponent(floatvalueofa3);
String valueofexponentofa4 = exponent(floatvalueofa4);
binary_x = manipulate(binary_x);
binary_y = manipulate(binary_y);
String changedmantissaofa3 = selfencrypt(valueofmantissaofa3, binary_x);
String changedmantissaofa4 = selfencrypt(valueofmantissaofa4, binary_y);
String xy = "1" + changedmantissaofa3.substring(0, Integer.parseInt(valueofexponentofa3, 2) - 127);
Integer final_result = Integer.parseInt(xy, 2);
String adit = changedmantissaofa3.substring(Integer.parseInt(valueofexponentofa3, 2) - 127, 22);
float final_final = decimalmantissa(adit);
final_final += (float) final_result;
String xyz = "1" + changedmantissaofa4.substring(0, Integer.parseInt(valueofexponentofa4, 2) - 127);
Integer final_result1 = Integer.parseInt(xyz, 2);
String aditi = changedmantissaofa4.substring(Integer.parseInt(valueofexponentofa4, 2) - 127, 22);
float final_final1 = decimalmantissa(aditi);
final_final1 += (float) final_result1;
splited[3] = Float.toString(final_final);
splited[4] = Float.toString(final_final1);
flag = 0;
}
for (int i = 0; i < splited.length; i++) {
bufferedReader1.write(splited[i] + " ");
}
if (flag == 0)
bufferedReader1.write("*");
bufferedReader1.newLine();
}
bufferedReader1.close();
}
catch(
Exception e)
{
e.printStackTrace();
}System.out.println("Count is"+count);
}
public static byte[] encrypt(String 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.getBytes("UTF-8"));
}
public static String decrypt(byte[] cipherText, String encryptionKey) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding", "SunJCE");
SecretKeySpec key = new SecretKeySpec(encryptionKey.getBytes("UTF-8"), "AES");
cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV.getBytes("UTF-8")));
return new String(cipher.doFinal(cipherText), "UTF-8");
}
public static String combinemanexp(String exp, String mantissa, float valueindatabase, String binaryxy) {
String newmantissa = selfencrypt(mantissa, binaryxy);
String h = "";
if (valueindatabase > 0) {
h = "0";
} else
h = "1";
h += exp + newmantissa + h;
return h;
}
public static String mantissa(float abcd) {
int flag = 0;
int k = 0;
while (abcd > 2) {
abcd = abcd / 2;
}
int p = (int) abcd;
float q = (float) abcd - p;
ArrayList<Integer> mantissa = new ArrayList<>();
float variable = q;
int h = 0;
for (int i = 0; i < 23; i++) {
variable = q * 2;
int temp = (int) variable;
q = variable - temp;
float[] var = new float[23];
var[k] = q;
if (variable == 0.0) {
flag = 1;
break;
}
for (h = k - 1; h >= 0; h--) {
if (var[h] == q) {
flag = 0;
break;
}
}
mantissa.add(temp);
// system.out.println(temp);
k++;
}
int l = 0;
int g, ab, m = 0;
if (flag == 1) {
for (l = k; l < 23; l++)
mantissa.add(0);
}
// 173.7
else {
for (g = k; g < 23; g++) {
for (l = h; l < k; l++)
mantissa.add(mantissa.get(m));
}
}
String a = "";
for (ab = 0; ab < 23; ab++) {
a += mantissa.get(ab).toString();
}
// double value = Double.parseDouble(a);
return a;
}
public static String exponent(float a) {
String str = "";
int count = 0;
while (a > 2) {
a = a / 2;
count++;
}
int exp = count + 127;
str = Integer.toBinaryString(exp);
return str;
}
public static String manipulate(String str) {
if (str.length() == 8)
return str;
else if (str.length() > 8)
return str.substring(0, 8);
else {
String str2 = "";
for (int i = 0; i < (8 - str.length()); i++)
str2 += '0';
str2 += str;
return str2;
}
}
public static String selfencrypt(String str1, String str2) // Str1 is
// mantissa of
// a3/a4 and
// Str2 is
// binary no x/y
// to be hidden
{
str1 = str1.substring(0, 15);
str1 += str2;
return str1;
}
public static float decimalmantissa(String mantisaa) {
float decmantisaa = 0;
for (int i = 1; i <= mantisaa.length(); i++) {
decmantisaa += Math.pow(2, -i) * Character.getNumericValue(mantisaa.charAt(i - 1));
}
// System.out.println(decmantisaa);
return decmantisaa;
}
public static float decrpytedvalueofx(){
for(int i=0;i<=10000;i++){
int digit=0;
String attach="0";
double z=(double)d[i].pk;
String t=Integer.toString(d[i].pk);
String v=new StringBuilder(t).reverse().toString();
digit=(int)(Math.log10((double)z))+1;
digit=16-digit;
for(int k=0;k<digit;k++)
v+=attach;
String plaintext= new StringBuilder(v).reverse().toString();
byte[] cipher = encrypt(plaintext, encryptionKey);
BigInteger big=BigInteger.valueOf(256);
BigInteger bi=BigInteger.valueOf(0);
for (int i=0; i<cipher.length; i++){
BigInteger p = big.pow(i);
BigInteger q = BigInteger.valueOf(1);
q= q.multiply(p);
q= q.multiply(BigInteger.valueOf((long)cipher[i]));
bi=bi.add(q);
}
BigInteger var=BigInteger.valueOf(10);
BigInteger var2=BigInteger.valueOf(0);
if(bi.remainder(var)==var2)
{ int w = bi.intValue();
String str=Integer.toBinaryString(w);
String str2=str.substring(0,8);
}
float decrypt = d[i].a3;
float decypt1 = d[i].a4;
String dvalofmana3 = mantissa(decrypt);
String dvalofmana4 = mantissa(decypt1);
String jack = dvalofmana3.substring(14, 22);
String kate= dvalofmana4.substring(14,22);
int intBits = Integer.parseInt(myString, 2);
float myFloat = Float.intBitsToFloat(intBits);
float dercyptedx = Float.parseFloat(jack)/1000;
float dercyptedy = Float.parseFloat(kate)/1000;
}