1 package com.freemindcafe.security.sample2;
3 import java.net.URLEncoder;
4 import java.security.Provider;
6 import javax.crypto.Cipher;
7 import javax.crypto.SecretKey;
9 import org.
apache.commons.codec.binary.Base64;
14 public static String encrypt(String value, String keyStorePath, String keyStoreType, String keyStorePassword, String secretKeyAlias)
throws Exception {
16 Provider securityProvider = null;
17 sKey =
EncryptionUtil.readKeyFromKeystore(keyStorePath, keyStoreType, keyStorePassword, secretKeyAlias);
18 securityProvider =
EncryptionUtil.getSupportingSecurityProvider(keyStoreType);
19 return encrypt(value, sKey, securityProvider);
22 public static String encrypt(String value, SecretKey secretKey, Provider securityProvider)
throws Exception {
23 Cipher cipher = Cipher.getInstance(
"AES/ECB/PKCS5Padding", securityProvider);
24 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
25 byte[] encryptedValue = cipher.doFinal( value.getBytes() );
26 byte[] encodedValue = Base64.encodeBase64(encryptedValue);
27 String encodedString =
new String(encodedValue);
28 String urlEncodedString = URLEncoder.encode(encodedString,
"UTF8");
29 return urlEncodedString;