OSDN Git Service

A29794
[everfolder/source.git] / source / workspace / EverFolder / src / com / yuji / ef / utility / Encryption.java
1 package com.yuji.ef.utility;
2
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;
9
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;
18
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;
24
25 import com.yuji.ef.base64.Base64;
26
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 };
31
32         private static final byte[] IV = { 16, 74, 71, -80, 32, 101, -47, 72, 117,
33                         -14, 0, -29, 70, 65, -12, 74 };
34
35         private SecretKey secretKey = null;
36
37         private Encryption(){
38                 
39         }
40         
41         public static Encryption getInstance(){
42                 if (instance == null){
43                         instance = new Encryption();
44                 }
45                 return instance;
46         }
47         
48         public String getEncrypted(Context context, String contents) {
49                 byte[] encoded = null;
50                 try {
51                         if (secretKey == null){
52                                 secretKey = generateKey(context);
53                         }
54
55                         byte[] encrypted = encrypt(contents.getBytes(), secretKey);
56
57                         encoded = Base64.encode(encrypted);
58                 } catch (NoSuchAlgorithmException e) {
59                         ;
60                 } catch (InvalidKeySpecException e) {
61                         ;
62                 } catch (InvalidKeyException e) {
63                         ;
64                 } catch (NoSuchPaddingException e) {
65                         ;
66                 } catch (InvalidAlgorithmParameterException e) {
67                         ;
68                 } catch (IllegalBlockSizeException e) {
69                         ;
70                 } catch (BadPaddingException e) {
71                         ;
72                 }
73                 
74                 return new String(encoded);
75         }
76
77         public String getDecrypted(Context context, String encoded){
78                 byte[] decrypted = null;
79                 try {
80                         if (secretKey == null){
81                                 secretKey = generateKey(context);
82                         }
83                         byte[] crypted = Base64.decode(encoded.getBytes());
84                         
85                         decrypted = decrypt(crypted, secretKey);
86                 } catch (NoSuchAlgorithmException e) {
87                         ;
88                 } catch (InvalidKeySpecException e) {
89                         ;
90                 } catch (InvalidKeyException e) {
91                         ;
92                 } catch (NoSuchPaddingException e) {
93                         ;
94                 } catch (InvalidAlgorithmParameterException e) {
95                         ;
96                 } catch (IllegalBlockSizeException e) {
97                         ;
98                 } catch (BadPaddingException e) {
99                         ;
100                 }
101                 return (decrypted != null)? new String(decrypted) : null;
102         }
103                         
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");
109
110                 cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(IV));
111
112                 byte[] encrypted = cipher.doFinal(src);
113
114                 return encrypted;
115         }
116
117         private byte[] decrypt(byte[] src, Key key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException{
118                 Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
119
120                 cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(IV));
121
122                 byte[] decrypted = cipher.doFinal(src);
123
124                 return decrypted;
125         }
126         
127         private SecretKey generateKey(Context context)
128                         throws NoSuchAlgorithmException, InvalidKeySpecException {
129                 char[] password = generatePassword(context);
130
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);
136
137                 return secretKey;
138         }
139
140         private char[] generatePassword(Context context) {
141                 PackageInfo packageInfo;
142                 StringBuffer sb = new StringBuffer();
143                 try {
144                         packageInfo = context.getPackageManager().getPackageInfo(
145                                         context.getPackageName(), PackageManager.GET_SIGNATURES);
146                         Signature[] signatures = packageInfo.signatures;
147
148                         for (int i = 0; i < signatures.length; i++) {
149                                 Signature signature = packageInfo.signatures[i];
150                                 char[] c = signature.toChars();
151                                 sb.append(c);
152                         }
153                 } catch (NameNotFoundException e) {
154                         return null;
155                 }
156                 
157                 String s = sb.toString();
158                 if (s.length() > 10){
159                         s = s.substring(0, 10);
160                 }
161                 return s.toCharArray();
162         }
163 }