EncryptProperty.java

  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. import java.security.Security;

  21. import org.bouncycastle.jce.provider.BouncyCastleProvider;
  22. import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;

  23. /**
  24.  * The Class EncryptProperty.
  25.  */
  26. public final class EncryptProperty {

  27.     /** The Constant EXPECTED_NUMBER_ARGS. */
  28.     private static final int EXPECTED_NUMBER_ARGS = 2;

  29.     /** The Constant PBEWITHSHA256AND128BITAES_CBC_BC. */
  30.     private static final String PBEWITHSHA256AND128BITAES_CBC_BC = "PBEWITHSHA256AND128BITAES-CBC-BC";
  31.    
  32.     /** The Constant BC_PROVIDER_NAME. */
  33.     private static final String BC_PROVIDER_NAME = "BC";
  34.    
  35.     /** The Constant ENC_CONTENT_SUFFIX. */
  36.     private static final String ENC_CONTENT_SUFFIX = ")";
  37.    
  38.     /** The Constant ENC_CONTENT_PREFIX. */
  39.     private static final String ENC_CONTENT_PREFIX = "ENC(";
  40.    
  41.     /** The Constant ENCRYPTED_PROPERTY_VALUE. */
  42.     private static final String ENCRYPTED_PROPERTY_VALUE = "Encrypted property value:";
  43.    
  44.     /** The Constant ENCRYPTED_VALUE. */
  45.     private static final String ENCRYPTED_VALUE = "Encrypted value:";
  46.    
  47.     /** The Constant HELP_MESSAGE. */
  48.     private static final String HELP_MESSAGE = "Encrypt property value with PBEWITHSHA256AND128BITAES_CBC_BC, using symmetric key and value as arguments. ./encryptProperty [key] [value]";
  49.    
  50.    
  51.     /**
  52.      * Instantiates a new encrypt property.
  53.      */
  54.     public EncryptProperty() {
  55.         super();
  56.     }

  57.     /**
  58.      * The main method.
  59.      *
  60.      * @param args the arguments
  61.      */
  62.     public static void main(String[] args) {
  63.         if (args.length == EXPECTED_NUMBER_ARGS) {
  64.             final String encryptValue = new EncryptProperty().encryptValue(args[0], args[1]);
  65.             System.out.println(ENCRYPTED_VALUE +encryptValue);
  66.             System.out.println(ENCRYPTED_PROPERTY_VALUE + ENC_CONTENT_PREFIX +encryptValue +ENC_CONTENT_SUFFIX);
  67.         } else {
  68.             System.out.println(HELP_MESSAGE);
  69.         }
  70.     }
  71.    
  72.     /**
  73.      * Encrypt value.
  74.      *
  75.      * @param symmetricKey
  76.      *            the symmetric key
  77.      * @param value
  78.      *            the value
  79.      * @return the string
  80.      */
  81.     public String encryptValue(final String symmetricKey,final String value) {
  82.         return getEncryptor(symmetricKey).encrypt(value);
  83.        
  84.     }

  85.     /**
  86.      * Gets the encryptor.
  87.      *
  88.      * @param symmetricKey
  89.      *            the symmetric key
  90.      * @return the encryptor
  91.      */
  92.     private static StandardPBEStringEncryptor getEncryptor(final String symmetricKey) {
  93.         Security.addProvider(new BouncyCastleProvider());
  94.         final StandardPBEStringEncryptor mySecondEncryptor = new StandardPBEStringEncryptor();
  95.         mySecondEncryptor.setProviderName(BC_PROVIDER_NAME);
  96.         mySecondEncryptor.setAlgorithm(PBEWITHSHA256AND128BITAES_CBC_BC);
  97.         mySecondEncryptor.setPassword(symmetricKey);
  98.         return mySecondEncryptor;
  99.     }


  100.     /**
  101.      * Decrypt value.
  102.      *
  103.      * @param symmetricKey
  104.      *            the symmetric key
  105.      * @param value
  106.      *            the value
  107.      * @return the string
  108.      */
  109.     public String decryptValue(final String symmetricKey,final String value) {
  110.         return getEncryptor(symmetricKey).decrypt(value);
  111.        
  112.     }

  113. }