-
Notifications
You must be signed in to change notification settings - Fork 3
/
cryptor.java
74 lines (59 loc) · 2.27 KB
/
cryptor.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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.security.MessageDigest;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
class Cryptor {
private static final byte[] initVector = {
(byte)0x13, (byte)0x37, (byte)0x13, (byte)0x37,
(byte)0x13, (byte)0x37, (byte)0x13, (byte)0x37,
(byte)0x13, (byte)0x37, (byte)0x13, (byte)0x37,
(byte)0x13, (byte)0x37, (byte)0x13, (byte)0x37
};
public static void main(String... args) {
System.out.println("encrypting " + args[0] + " => " + args[1] + " with key " + args[2]);
System.out.println(encrypt(args[0], args[1], args[2]));
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
public static byte[] hash(byte[] in) throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(in);
return md.digest();
}
public static String encrypt(String ptFp, String ctFp, String key) {
try {
byte[] K0 = hexStringToByteArray(key);
byte[] keyBytes = hash(K0);
File ptFile = new File(ptFp);
File ctFile = new File(ctFp);
byte[] pt = Files.readAllBytes(ptFile.toPath());
IvParameterSpec iv = new IvParameterSpec(initVector);
SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] ct = cipher.doFinal(pt);
OutputStream out = new FileOutputStream(ctFile);
out.write(ct, 0, ct.length);
out.flush();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}