1 /* 2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH 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 package de.smartics.properties.api.core.security; 17 18 import javax.crypto.Cipher; 19 import javax.crypto.SecretKey; 20 21 import org.apache.commons.codec.binary.Base64; 22 23 import de.smartics.properties.api.core.domain.PropertyDescriptor; 24 import de.smartics.properties.api.core.domain.PropertyDescriptorMessageBean; 25 26 /** 27 * Base implementation based on Java security. 28 */ 29 public abstract class AbstractPropertyValueSecurity implements 30 PropertyValueSecurity 31 { 32 // ********************************* Fields ********************************* 33 34 // --- constants ------------------------------------------------------------ 35 36 /** 37 * The class version identifier. 38 */ 39 private static final long serialVersionUID = 1L; 40 41 // --- members -------------------------------------------------------------- 42 43 // ****************************** Initializer ******************************* 44 45 // ****************************** Constructors ****************************** 46 47 /** 48 * Default constructor. 49 */ 50 protected AbstractPropertyValueSecurity() 51 { 52 } 53 54 // ****************************** Inner Classes ***************************** 55 56 // ********************************* Methods ******************************** 57 58 // --- init ----------------------------------------------------------------- 59 60 // --- get&set -------------------------------------------------------------- 61 62 // --- business ------------------------------------------------------------- 63 64 /** 65 * Decrypts the given property value. 66 * 67 * @param descriptor the descriptor of the property. 68 * @param encryptedValue the encrypted value of the property to be decrypted. 69 * @param key the key used for decryption. 70 * @param cipher the cipher instance for decryption. 71 * @return the decrypted value. 72 * @throws NullPointerException may be thrown if {@code descriptor} is 73 * <code>null</code>. 74 * @throws SecurityException if the value cannot be decrypted. 75 */ 76 protected String decrypt(final PropertyDescriptor descriptor, 77 final String encryptedValue, final SecretKey key, final Cipher cipher) 78 throws NullPointerException, SecurityException 79 { 80 try 81 { 82 cipher.init(Cipher.DECRYPT_MODE, key); 83 final byte[] decodedBytes = decodePropertyValue(encryptedValue); 84 final byte[] decryptedBytes = cipher.doFinal(decodedBytes); 85 final String plainValue = new String(decryptedBytes, "UTF-8"); 86 return plainValue; 87 } 88 catch (final Exception e) 89 { 90 throw new SecurityException(new PropertyDescriptorMessageBean( 91 SecurityCode.DECRYPTION_FAILED, e, descriptor)); 92 } 93 } 94 95 /** 96 * Encrypts the given property value. 97 * 98 * @param descriptor the descriptor of the property. 99 * @param plainValue the plain value of the property to be encrypted. 100 * @param key the key used for encryption. 101 * @param cipher the cipher instance for encryption. 102 * @return the encrypted value. 103 * @throws NullPointerException may be thrown if {@code descriptor} is 104 * <code>null</code>. 105 * @throws SecurityException if the value cannot be encrypted. 106 */ 107 protected String encrypt(final PropertyDescriptor descriptor, 108 final String plainValue, final SecretKey key, final Cipher cipher) 109 throws NullPointerException, SecurityException 110 { 111 try 112 { 113 cipher.init(Cipher.ENCRYPT_MODE, key); 114 final byte[] bytes = plainValue.getBytes("UTF-8"); 115 final byte[] encryptedBytes = cipher.doFinal(bytes); 116 final String encryptedValue = encodePropertyValue(encryptedBytes); 117 return encryptedValue; 118 } 119 catch (final Exception e) 120 { 121 throw new SecurityException(new PropertyDescriptorMessageBean( 122 SecurityCode.ENCRYPTION_FAILED, e, descriptor)); 123 } 124 } 125 126 /** 127 * Decodes the encrypted property value. 128 * 129 * @param encryptedValue the encrypted value to decode. 130 * @return the decoded value. 131 */ 132 protected byte[] decodePropertyValue(final String encryptedValue) 133 { 134 final byte[] decodedValue = Base64.decodeBase64(encryptedValue); 135 return decodedValue; 136 } 137 138 /** 139 * Encodes the encrypted bytes. 140 * 141 * @param encryptedBytes the bytes to encode. 142 * @return the encoded bytes. 143 */ 144 protected String encodePropertyValue(final byte[] encryptedBytes) 145 { 146 final String encodedValue = Base64.encodeBase64String(encryptedBytes); 147 return encodedValue; 148 } 149 150 // --- object basics -------------------------------------------------------- 151 152 }