View Javadoc
1   /*
2    * Copyright 2014 James Pether Sörling
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
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   * The Class EncryptProperty.
28   */
29  public final class EncryptProperty {
30  
31  	/** The Constant EXPECTED_NUMBER_ARGS. */
32  	private static final int EXPECTED_NUMBER_ARGS = 2;
33  
34  	/** The Constant PBEWITHSHA256AND128BITAES_CBC_BC. */
35  	private static final String PBEWITHSHA256AND128BITAES_CBC_BC = "PBEWITHSHA256AND128BITAES-CBC-BC";
36  	
37  	/** The Constant BC_PROVIDER_NAME. */
38  	private static final String BC_PROVIDER_NAME = "BC";
39  	
40  	/** The Constant ENC_CONTENT_SUFFIX. */
41  	private static final String ENC_CONTENT_SUFFIX = ")";
42  	
43  	/** The Constant ENC_CONTENT_PREFIX. */
44  	private static final String ENC_CONTENT_PREFIX = "ENC(";
45  	
46  	/** The Constant ENCRYPTED_PROPERTY_VALUE. */
47  	private static final String ENCRYPTED_PROPERTY_VALUE = "Encrypted property value:";
48  	
49  	/** The Constant ENCRYPTED_VALUE. */
50  	private static final String ENCRYPTED_VALUE = "Encrypted value:";
51  	
52  	/** The Constant HELP_MESSAGE. */
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  	 * Instantiates a new encrypt property.
58  	 */
59  	public EncryptProperty() {
60  		super();
61  	}
62  
63  	/**
64  	 * The main method.
65  	 *
66  	 * @param args the arguments
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  	 * Encrypt value.
80  	 *
81  	 * @param symmetricKey
82  	 *            the symmetric key
83  	 * @param value
84  	 *            the value
85  	 * @return the string
86  	 */
87  	public String encryptValue(final String symmetricKey,final String value) {
88  		return getEncryptor(symmetricKey).encrypt(value);
89  		
90  	}
91  
92  	/**
93  	 * Gets the encryptor.
94  	 *
95  	 * @param symmetricKey
96  	 *            the symmetric key
97  	 * @return the encryptor
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 	 * Decrypt value.
111 	 *
112 	 * @param symmetricKey
113 	 *            the symmetric key
114 	 * @param value
115 	 *            the value
116 	 * @return the string
117 	 */
118 	public String decryptValue(final String symmetricKey,final String value) {
119 		return getEncryptor(symmetricKey).decrypt(value);
120 		
121 	}
122 
123 }