1 package com.yuji.ef.utility;
3 import java.security.InvalidAlgorithmParameterException;
4 import java.security.InvalidKeyException;
5 import java.security.Key;
6 import java.security.NoSuchAlgorithmException;
7 import java.security.spec.InvalidKeySpecException;
8 import java.security.spec.KeySpec;
10 import javax.crypto.BadPaddingException;
11 import javax.crypto.Cipher;
12 import javax.crypto.IllegalBlockSizeException;
13 import javax.crypto.NoSuchPaddingException;
14 import javax.crypto.SecretKey;
15 import javax.crypto.SecretKeyFactory;
16 import javax.crypto.spec.IvParameterSpec;
17 import javax.crypto.spec.PBEKeySpec;
19 import android.content.Context;
20 import android.content.pm.PackageInfo;
21 import android.content.pm.PackageManager;
22 import android.content.pm.PackageManager.NameNotFoundException;
23 import android.content.pm.Signature;
25 import com.yuji.ef.base64.Base64;
27 public class Encryption {
28 private static Encryption instance = null;
29 private static final byte[] SALT = new byte[] { -47, 66, 32, -127, 98, -12,
30 4, 113, -24, -111, 23, 121, -97 };
32 private static final byte[] IV = { 16, 74, 71, -80, 32, 101, -47, 72, 117,
33 -14, 0, -29, 70, 65, -12, 74 };
35 private SecretKey secretKey = null;
41 public static Encryption getInstance(){
42 if (instance == null){
43 instance = new Encryption();
48 public String getEncrypted(Context context, String contents) {
49 byte[] encoded = null;
51 if (secretKey == null){
52 secretKey = generateKey(context);
55 byte[] encrypted = encrypt(contents.getBytes(), secretKey);
57 encoded = Base64.encode(encrypted);
58 } catch (NoSuchAlgorithmException e) {
60 } catch (InvalidKeySpecException e) {
62 } catch (InvalidKeyException e) {
64 } catch (NoSuchPaddingException e) {
66 } catch (InvalidAlgorithmParameterException e) {
68 } catch (IllegalBlockSizeException e) {
70 } catch (BadPaddingException e) {
74 return new String(encoded);
77 public String getDecrypted(Context context, String encoded){
78 byte[] decrypted = null;
80 if (secretKey == null){
81 secretKey = generateKey(context);
83 byte[] crypted = Base64.decode(encoded.getBytes());
85 decrypted = decrypt(crypted, secretKey);
86 } catch (NoSuchAlgorithmException e) {
88 } catch (InvalidKeySpecException e) {
90 } catch (InvalidKeyException e) {
92 } catch (NoSuchPaddingException e) {
94 } catch (InvalidAlgorithmParameterException e) {
96 } catch (IllegalBlockSizeException e) {
98 } catch (BadPaddingException e) {
101 return (decrypted != null)? new String(decrypted) : null;
104 private byte[] encrypt(byte[] src, Key key) throws NoSuchAlgorithmException,
105 NoSuchPaddingException, InvalidKeyException,
106 InvalidAlgorithmParameterException, IllegalBlockSizeException,
107 BadPaddingException {
108 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
110 cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));
112 byte[] encrypted = cipher.doFinal(src);
117 private byte[] decrypt(byte[] src, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
118 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
120 cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));
122 byte[] decrypted = cipher.doFinal(src);
127 private SecretKey generateKey(Context context)
128 throws NoSuchAlgorithmException, InvalidKeySpecException {
129 char[] password = generatePassword(context);
131 //KeySpec keySpec = new PBEKeySpec(password, SALT, 1024, 256);
132 KeySpec keySpec = new PBEKeySpec(password, SALT, 256, 64);
133 SecretKeyFactory factory = SecretKeyFactory
134 .getInstance("PBEWITHSHAAND256BITAES-CBC-BC");
135 SecretKey secretKey = factory.generateSecret(keySpec);
140 private char[] generatePassword(Context context) {
141 PackageInfo packageInfo;
142 StringBuffer sb = new StringBuffer();
144 packageInfo = context.getPackageManager().getPackageInfo(
145 context.getPackageName(), PackageManager.GET_SIGNATURES);
146 Signature[] signatures = packageInfo.signatures;
148 for (int i = 0; i < signatures.length; i++) {
149 Signature signature = packageInfo.signatures[i];
150 char[] c = signature.toChars();
153 } catch (NameNotFoundException e) {
157 String s = sb.toString();
158 if (s.length() > 10){
159 s = s.substring(0, 10);
161 return s.toCharArray();