Samples JDK
Encryption.java
1 package com.freemindcafe.security.sample2;
2 
3 import java.net.URLEncoder;
4 import java.security.Provider;
5 
6 import javax.crypto.Cipher;
7 import javax.crypto.SecretKey;
8 
9 import org.apache.commons.codec.binary.Base64;
10 
11 public class Encryption
12 {
13 
14  public static String encrypt(String value, String keyStorePath, String keyStoreType, String keyStorePassword, String secretKeyAlias) throws Exception {
15  SecretKey sKey;
16  Provider securityProvider = null;
17  sKey = EncryptionUtil.readKeyFromKeystore(keyStorePath, keyStoreType, keyStorePassword, secretKeyAlias);
18  securityProvider = EncryptionUtil.getSupportingSecurityProvider(keyStoreType);
19  return encrypt(value, sKey, securityProvider);
20  }
21 
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;
30  }
31 
32 }