-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDepLocEncode.java
50 lines (40 loc) · 1.49 KB
/
DepLocEncode.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
import java.security.Key;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
//import javax.persistence.AttributeConverter;
//import javax.persistence.Converter;
public class DepLocEncode {
public static void main(String argv[]){
//setKey("verysecretsecret");
setKey("tiansftdlstswbcf");
String encStr = convertToDatabaseColumn(argv[0]);
System.out.println(encStr);
}
private static final String ALGORITHM = "AES/ECB/PKCS5Padding";
private static byte[] KEY;
public static void setKey(String secret) {
KEY = secret.getBytes();
}
public static String convertToDatabaseColumn(String entityValue) {
Key key = new SecretKeySpec(KEY, "AES");
try {
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
String tmpStr = new String(Base64.getEncoder().encode(c.doFinal(entityValue.getBytes())));
return tmpStr.substring(0,24);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public String convertToEntityAttribute(String dbValue) {
Key key = new SecretKeySpec(KEY, "AES");
try {
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
return new String(c.doFinal(Base64.getDecoder().decode(dbValue)));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}