forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScramble.java
More file actions
84 lines (78 loc) · 2.02 KB
/
Copy pathScramble.java
File metadata and controls
84 lines (78 loc) · 2.02 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
import java.io.FilterOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.Random;
class ScrambledOutputStream extends FilterOutputStream{
private int[] map;
public ScrambledOutputStream(OutputStream out, int[] map){
super(out);
if(map==null || map.length!=256){
throw new IllegalArgumentException("map need contain 256 elements exactly.");
}
this.map = map;
}
@Override
public void write(int b) throws IOException{
// implementing trivial encryption, true value used as index. The map's role is key, so we need store it or retrive the identical one.
// The result looks having no relation with the source, haha.
out.write(map[b]);
// filed out is protected, so you can access it here.
}
}
public class Scramble{
public static final int RANDOM_SEED = 0;
public static void main(String[] args){
if(args.length != 2){
System.err.println("usage: java Scramble srcpath destpath");
return;
}
FileInputStream fis = null;
ScrambledOutputStream sos = null;
try{
fis = new FileInputStream(args[0]);
FileOutputStream fos = new FileOutputStream(args[1]);
sos = new ScrambledOutputStream(fos, makeMap());
int b;
while((b=fis.read())!=-1){
sos.write(b);
}
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
if(fis!=null){
try{
fis.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
if(sos!=null){
try{
sos.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
}
static int[] makeMap(){
int[] map = new int[256];
for(int i=0;i<map.length;i++){
map[i] = i;
}
// shuffle the map
Random random = new Random(RANDOM_SEED);
// Specified seed intentionally to retrive the identical map when operating decryption.
for(int i=0;i<map.length;i++){
int index = random.nextInt(map.length);
if(index!=i){
int temp = map[index];
map[index] = map[i];
map[i] = temp;
}
}
return map;
}
}