-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathExtract.java
176 lines (172 loc) · 5.8 KB
/
Extract.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
import java.io.*;
import crypt.*;
import ortega.*;
public class Extract {
private static File f; // carrier file
private static byte[] carrier; // carrier data
private static int[] coeff; // dct values
private static FileOutputStream fos; // embedded file (output file)
private static String embFileName; // output file name
private static String password;
private static byte[] deZigZag = {
0, 1, 5, 6, 14, 15, 27, 28,
2, 4, 7, 13, 16, 26, 29, 42,
3, 8, 12, 17, 25, 30, 41, 43,
9, 11, 18, 24, 31, 40, 44, 53,
10, 19, 23, 32, 39, 45, 52, 54,
20, 22, 33, 38, 46, 51, 55, 60,
21, 34, 37, 47, 50, 56, 59, 61,
35, 36, 48, 49, 57, 58, 62, 63
};
static void usage() {
System.out.println("java Extract [Options] \"image.jpg\"");
System.out.println("Options:");
System.out.println("\t-p password (default: abc123)");
System.out.println("\t-e extractedFileName (default: output.txt)");
System.out.println("\nAuthor: Andreas Westfeld, [email protected]");
System.exit(0);
}
public static void main(String[] args) {
embFileName = "output.txt";
password = "abc123";
try {
if (args.length < 1)
usage();
for (int i=0; i<args.length; i++) {
if (!args[i].startsWith("-")) {
if (!args[i].endsWith(".jpg"))
usage();
f = new File(args[i]);
continue;
}
if (args.length < i+1) {
System.out.println("Missing parameter for switch "+args[i]);
usage();
}
if (args[i].equals("-e")) {
embFileName = args[i+1];
} else if (args[i].equals("-p")) {
password = args[i+1];
} else
System.out.println("Unknown switch "
+args[i]+" ignored.");
i++;
}
carrier = new byte[(int)f.length()];
FileInputStream fis = new FileInputStream(f);
fos = new FileOutputStream(new File(embFileName));
fis.read(carrier);
HuffmanDecode hd = new HuffmanDecode(carrier);
System.out.println("Huffman decoding starts");
coeff=hd.decode();
System.out.println("Permutation starts");
F5Random random = new F5Random(password.getBytes());
Permutation permutation = new Permutation(coeff.length, random);
System.out.println(coeff.length+" indices shuffled");
int extractedByte=0;
int availableExtractedBits=0;
int extractedFileLength=0;
int nBytesExtracted=0;
int shuffledIndex=0;
int extractedBit;
int i;
System.out.println("Extraction starts");
// extract length information
for (i=0; availableExtractedBits<32; i++) {
shuffledIndex = permutation.getShuffled(i);
if (shuffledIndex%64 == 0) continue; // skip DC coefficients
shuffledIndex = shuffledIndex-(shuffledIndex%64)+deZigZag[shuffledIndex%64];
if (coeff[shuffledIndex] == 0) continue; // skip zeroes
if (coeff[shuffledIndex] > 0)
extractedBit=coeff[shuffledIndex]&1;
else
extractedBit=1-(coeff[shuffledIndex]&1);
extractedFileLength |= extractedBit << availableExtractedBits++;
}
// remove pseudo random pad
extractedFileLength ^= random.getNextByte();
extractedFileLength ^= random.getNextByte()<<8;
extractedFileLength ^= random.getNextByte()<<16;
extractedFileLength ^= random.getNextByte()<<24;
int k = extractedFileLength >> 24;
k %= 32;
int n = (1 << k)-1;
extractedFileLength &= 0x007fffff;
System.out.println("Length of embedded file: "+extractedFileLength+" bytes");
availableExtractedBits = 0;
if (n>0) {
int startOfN = i;
int hash;
System.out.println("(1, "+n+", "+k+") code used");
extractingLoop:
do {
// 1. read n places, and calculate k bits
hash = 0;
int code = 1;
for (i=0; code<=n; i++) {
// check for pending end of coeff
if (startOfN+i>=coeff.length) break extractingLoop;
shuffledIndex = permutation.getShuffled(startOfN+i);
if (shuffledIndex%64 == 0) continue; // skip DC coefficients
shuffledIndex = shuffledIndex-(shuffledIndex%64)+deZigZag[shuffledIndex%64];
if (coeff[shuffledIndex] == 0) continue; // skip zeroes
if (coeff[shuffledIndex] > 0)
extractedBit=coeff[shuffledIndex]&1;
else
extractedBit=1-(coeff[shuffledIndex]&1);
if (extractedBit==1) {
hash ^= code;
}
code++;
}
startOfN += i;
// 2. write k bits bytewise
for (i=0; i<k; i++) {
extractedByte |= ((hash>>i)&1) << availableExtractedBits++;
if (availableExtractedBits == 8) {
// remove pseudo random pad
extractedByte ^= random.getNextByte();
fos.write((byte) extractedByte);
extractedByte=0;
availableExtractedBits=0;
nBytesExtracted++;
// check for pending end of embedded data
if (nBytesExtracted==extractedFileLength)
break extractingLoop;
}
}
} while (true);
} else {
System.out.println("Default code used");
for (; i<coeff.length; i++) {
shuffledIndex = permutation.getShuffled(i);
if (shuffledIndex%64 == 0) continue; // skip DC coefficients
shuffledIndex = shuffledIndex-(shuffledIndex%64)+deZigZag[shuffledIndex%64];
if (coeff[shuffledIndex] == 0) continue; // skip zeroes
if (coeff[shuffledIndex] > 0)
extractedBit=coeff[shuffledIndex]&1;
else
extractedBit=1-(coeff[shuffledIndex]&1);
extractedByte |= extractedBit << availableExtractedBits++;
if (availableExtractedBits == 8) {
// remove pseudo random pad
extractedByte ^= random.getNextByte();
fos.write((byte) extractedByte);
extractedByte=0;
availableExtractedBits=0;
nBytesExtracted++;
if (nBytesExtracted==extractedFileLength)
break;
}
}
}
if (nBytesExtracted<extractedFileLength) {
System.out.println("Incomplete file: only "+
nBytesExtracted+" of "+extractedFileLength+
" bytes extracted");
}
} catch(Exception e) {
e.printStackTrace();
}
}
}