1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.encryption.properties;
20
21 import java.security.Security;
22
23 import org.bouncycastle.jce.provider.BouncyCastleProvider;
24 import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
25
26
27
28
29 public final class EncryptProperty {
30
31
32 private static final int EXPECTED_NUMBER_ARGS = 2;
33
34
35 private static final String PBEWITHSHA256AND128BITAES_CBC_BC = "PBEWITHSHA256AND128BITAES-CBC-BC";
36
37
38 private static final String BC_PROVIDER_NAME = "BC";
39
40
41 private static final String ENC_CONTENT_SUFFIX = ")";
42
43
44 private static final String ENC_CONTENT_PREFIX = "ENC(";
45
46
47 private static final String ENCRYPTED_PROPERTY_VALUE = "Encrypted property value:";
48
49
50 private static final String ENCRYPTED_VALUE = "Encrypted value:";
51
52
53 private static final String HELP_MESSAGE = "Encrypt property value with PBEWITHSHA256AND128BITAES_CBC_BC, using symmetric key and value as arguments. ./encryptProperty [key] [value]";
54
55
56
57
58
59 public EncryptProperty() {
60 super();
61 }
62
63
64
65
66
67
68 public static void main(String[] args) {
69 if (args.length == EXPECTED_NUMBER_ARGS) {
70 final String encryptValue = new EncryptProperty().encryptValue(args[0], args[1]);
71 System.out.println(ENCRYPTED_VALUE +encryptValue);
72 System.out.println(ENCRYPTED_PROPERTY_VALUE + ENC_CONTENT_PREFIX +encryptValue +ENC_CONTENT_SUFFIX);
73 } else {
74 System.out.println(HELP_MESSAGE);
75 }
76 }
77
78
79
80
81
82
83
84
85
86
87 public String encryptValue(final String symmetricKey,final String value) {
88 return getEncryptor(symmetricKey).encrypt(value);
89
90 }
91
92
93
94
95
96
97
98
99 private static StandardPBEStringEncryptor getEncryptor(final String symmetricKey) {
100 Security.addProvider(new BouncyCastleProvider());
101 final StandardPBEStringEncryptor mySecondEncryptor = new StandardPBEStringEncryptor();
102 mySecondEncryptor.setProviderName(BC_PROVIDER_NAME);
103 mySecondEncryptor.setAlgorithm(PBEWITHSHA256AND128BITAES_CBC_BC);
104 mySecondEncryptor.setPassword(symmetricKey);
105 return mySecondEncryptor;
106 }
107
108
109
110
111
112
113
114
115
116
117
118 public String decryptValue(final String symmetricKey,final String value) {
119 return getEncryptor(symmetricKey).decrypt(value);
120
121 }
122
123 }