Java tutorial
/******************************************************************************* * Copyright (c) 2015-2016, WSO2.Telco Inc. (http://www.wso2telco.com) * * All Rights Reserved. WSO2.Telco Inc. licences this file to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ package com.wso2telco.cryptosystem; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import com.wso2telco.core.config.service.ConfigurationService; import com.wso2telco.core.config.service.ConfigurationServiceImpl; import org.apache.commons.codec.binary.Base64; import java.nio.charset.Charset; import java.security.Key; // TODO: Auto-generated Javadoc /** * The Class AESencrp. */ public class AESencrp { /** * The Constant ALGO. */ private static final String ALGO = "AES"; /** * The Configuration service */ private static ConfigurationService configurationService = new ConfigurationServiceImpl(); /** * The key. */ private static String key = configurationService.getDataHolder().getMobileConnectConfig() .getSessionUpdaterConfig().getAeskey(); /** * The Constant keyValue. */ private static final byte[] keyValue = key.getBytes(Charset.forName("UTF-8")); /** * Encrypt. * * @param Data the data * @return the string * @throws Exception the exception */ public static String encrypt(String Data) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.ENCRYPT_MODE, key); byte[] encVal = c.doFinal(Data.getBytes()); //String encryptedValue = new BASE64Encoder().encode(encVal); String encryptedValue = new String(Base64.encodeBase64(encVal)); return encryptedValue; } /** * Decrypt. * * @param encryptedData the encrypted data * @return the string * @throws Exception the exception */ public static String decrypt(String encryptedData) throws Exception { Key key = generateKey(); Cipher c = Cipher.getInstance(ALGO); c.init(Cipher.DECRYPT_MODE, key); //byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes()); byte[] decValue = c.doFinal(decordedValue); String decryptedValue = new String(decValue); return decryptedValue; } /** * Generate key. * * @return the key * @throws Exception the exception */ private static Key generateKey() throws Exception { Key key = new SecretKeySpec(keyValue, ALGO); return key; } }