· 8 years ago · Dec 14, 2017, 05:58 AM
110-17 16:30:25.878: W/System.err(15231): java.io.IOException: pad block corrupted
210-17 16:30:25.889: W/System.err(15231): at javax.crypto.CipherInputStream.read(CipherInputStream.java:102)
310-17 16:30:25.889: W/System.err(15231): at javax.crypto.CipherInputStream.read(CipherInputStream.java:134)
410-17 16:30:25.898: W/System.err(15231): at java.io.InputStream.read(InputStream.java:163)
510-17 16:30:25.898: W/System.err(15231): at xont.virtusel.v4.controller.syn.DBEncript.ReadEncryptedFile(DBEncript.java:178)
610-17 16:30:25.898: W/System.err(15231): at xont.virtusel.v4.controller.syn.DBEncript.callRead(DBEncript.java:247)
710-17 16:30:25.898: W/System.err(15231): at xont.virtusel.v4.db.DataBaseHelper.extarctDataBase(DataBaseHelper.java:116)
810-17 16:30:25.898: W/System.err(15231): at xont.virtusel.v4.db.DataBaseHelper.copyDataBase(DataBaseHelper.java:104)
910-17 16:30:25.908: W/System.err(15231): at xont.virtusel.v4.db.DataBaseHelper.createDataBase(DataBaseHelper.java:59)
1010-17 16:30:25.908: W/System.err(15231): at xont.virtusel.v4.controller.syn.DatabaseSetupActivity$1.onItemClick(DatabaseSetupActivity.java:43)
1110-17 16:30:25.908: W/System.err(15231): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
1210-17 16:30:25.908: W/System.err(15231): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
1310-17 16:30:25.908: W/System.err(15231): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
1410-17 16:30:25.908: W/System.err(15231): at android.widget.AbsListView$1.run(AbsListView.java:3168)
1510-17 16:30:25.908: W/System.err(15231): at android.os.Handler.handleCallback(Handler.java:605)
1610-17 16:30:25.908: W/System.err(15231): at android.os.Handler.dispatchMessage(Handler.java:92)
1710-17 16:30:25.919: W/System.err(15231): at android.os.Looper.loop(Looper.java:137)
1810-17 16:30:25.919: W/System.err(15231): at android.app.ActivityThread.main(ActivityThread.java:4340)
1910-17 16:30:25.919: W/System.err(15231): at java.lang.reflect.Method.invokeNative(Native Method)
2010-17 16:30:25.928: W/System.err(15231): at java.lang.reflect.Method.invoke(Method.java:511)
2110-17 16:30:25.928: W/System.err(15231): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
2210-17 16:30:25.928: W/System.err(15231): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
2310-17 16:30:25.928: W/System.err(15231): at dalvik.system.NativeStart.main(Native Method)
24
25public class DBEncript {
26
27 String mPassword = null;
28 public final static int SALT_LEN = 8;
29 byte [] mInitVec = null;
30 byte [] mSalt = null;
31 Cipher mEcipher = null;
32 Cipher mDecipher = null;
33 private final int KEYLEN_BITS = 128; // see notes below where this is used.
34 private final int ITERATIONS = 65536;
35 private final int MAX_FILE_BUF = 1024;
36
37 public DBEncript (String password){
38 mPassword = password;
39 }
40
41 public byte [] getSalt (){
42 return (mSalt);
43 }
44
45 public byte [] getInitVec (){
46 return (mInitVec);
47 }
48
49 private void Db (String msg){
50 System.out.println ("** DBEncript ** " + msg);
51 }
52
53 /**
54 * @throws NoSuchAlgorithmException
55 * @throws InvalidKeySpecException
56 * @throws NoSuchPaddingException
57 * @throws InvalidParameterSpecException
58 * @throws IllegalBlockSizeException
59 * @throws BadPaddingException
60 * @throws UnsupportedEncodingException
61 * @throws InvalidKeyException
62 */
63 public void setupEncrypt () throws NoSuchAlgorithmException,
64 InvalidKeySpecException,
65 NoSuchPaddingException,
66 InvalidParameterSpecException,
67 IllegalBlockSizeException,
68 BadPaddingException,
69 UnsupportedEncodingException,
70 InvalidKeyException {
71 SecretKeyFactory factory = null;
72 SecretKey tmp = null;
73 mSalt = new byte [SALT_LEN];
74 SecureRandom rnd = new SecureRandom ();
75 rnd.nextBytes (mSalt);
76 Db ("generated salt :" + Hex.encodeHex (mSalt));
77
78 factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
79
80 KeySpec spec = new PBEKeySpec (mPassword.toCharArray (), mSalt, ITERATIONS, KEYLEN_BITS);
81 tmp = factory.generateSecret (spec);
82 SecretKey secret = new SecretKeySpec (tmp.getEncoded(), "AES");
83
84 mEcipher = Cipher.getInstance ("AES/CBC/PKCS5Padding");
85 mEcipher.init (Cipher.ENCRYPT_MODE, secret);
86 AlgorithmParameters params = mEcipher.getParameters ();
87
88 mInitVec = params.getParameterSpec (IvParameterSpec.class).getIV();
89
90 Db ("mInitVec is :" + Hex.encodeHex (mInitVec));
91 }
92
93 public void setupDecrypt (String initvec, String salt) throws NoSuchAlgorithmException,InvalidKeySpecException,NoSuchPaddingException,
94 InvalidKeyException,InvalidAlgorithmParameterException,DecoderException{
95 SecretKeyFactory factory = null;
96 SecretKey tmp = null;
97 SecretKey secret = null;
98
99 mSalt = Hex.decodeHex (salt.toCharArray ());
100 Db ("got salt " + Hex.encodeHex (mSalt));
101
102 mInitVec = Hex.decodeHex (initvec.toCharArray ());
103 Db ("got initvector :" + Hex.encodeHex (mInitVec));
104
105
106 factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
107 KeySpec spec = new PBEKeySpec(mPassword.toCharArray (), mSalt, ITERATIONS, KEYLEN_BITS);
108
109 tmp = factory.generateSecret(spec);
110 secret = new SecretKeySpec(tmp.getEncoded(), "AES");
111
112 mDecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
113 mDecipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(mInitVec));
114 }
115
116
117 public void WriteEncryptedFile (File input, File output) throws IOException, IllegalBlockSizeException,BadPaddingException{
118 FileInputStream fin;
119 FileOutputStream fout;
120 long totalread = 0;
121 int nread = 0;
122 byte [] inbuf = new byte [MAX_FILE_BUF];
123
124 fout = new FileOutputStream (output);
125 fin = new FileInputStream (input);
126
127 while ((nread = fin.read (inbuf)) > 0 )
128 {
129 // Db ("read " + nread + " bytes");
130 totalread += nread;
131 byte [] trimbuf = new byte [nread];
132 for (int i = 0; i < nread; i++)
133 trimbuf[i] = inbuf[i];
134
135 byte [] tmp = mEcipher.update (trimbuf);
136
137 if (tmp != null)
138 fout.write (tmp);
139 }
140
141 byte [] finalbuf = mEcipher.doFinal ();
142 if (finalbuf != null)
143 fout.write (finalbuf);
144
145 fout.flush();
146 fin.close();
147 fout.close();
148 fout.close ();
149
150 Db ("wrote " + totalread + " encrypted bytes");
151 }
152
153
154
155 public void ReadEncryptedFile (File input, File output) throws IllegalBlockSizeException,BadPaddingException, IOException{
156
157 FileInputStream fin;
158 FileOutputStream fout;
159 CipherInputStream cin;
160 long totalread = 0;
161 int nread = 0;
162 byte [] inbuf = new byte [MAX_FILE_BUF];
163
164 fout = new FileOutputStream (output);
165 fin = new FileInputStream (input);
166
167 cin = new CipherInputStream (fin, mDecipher);
168 while ((nread = cin.read (inbuf)) > 0 )
169 {
170 //Db ("read " + nread + " bytes");
171 totalread += nread;
172
173 byte [] trimbuf = new byte [nread];
174 for (int i = 0; i < nread; i++)
175 trimbuf[i] = inbuf[i];
176
177 fout.write (trimbuf);
178 }
179
180 fout.flush();
181 cin.close();
182 fin.close ();
183 fout.close();
184
185 }
186
187
188 public void callRead(File input, File output) {
189 String iv = null;
190 String salt = null;
191 DBEncript en = new DBEncript ("123");
192 try{
193 en.setupEncrypt ();
194 iv = new String(Hex.encodeHex(en.getInitVec())).toUpperCase ();
195 salt = new String(Hex.encodeHex(en.getSalt())).toUpperCase ();
196 }catch (InvalidKeyException e){
197 e.printStackTrace();
198 }catch (NoSuchAlgorithmException e){
199 e.printStackTrace();
200 }catch (InvalidKeySpecException e){
201 e.printStackTrace();
202 }catch (NoSuchPaddingException e){
203 e.printStackTrace();
204 }catch (InvalidParameterSpecException e){
205 e.printStackTrace();
206 }catch (IllegalBlockSizeException e){
207 e.printStackTrace();
208 }catch (BadPaddingException e){
209 e.printStackTrace();
210 }catch (UnsupportedEncodingException e){
211 e.printStackTrace();
212 }
213
214
215 /*
216 * decrypt file
217 */
218 DBEncript dc = new DBEncript ("123");
219 try{
220 dc.setupDecrypt (iv, salt);
221 }catch (InvalidKeyException e){
222 e.printStackTrace();
223 }catch (NoSuchAlgorithmException e){
224 e.printStackTrace();
225 }catch (InvalidKeySpecException e){
226 e.printStackTrace();
227 }catch (NoSuchPaddingException e){
228 e.printStackTrace();
229 }catch (InvalidAlgorithmParameterException e){
230 e.printStackTrace();
231 }catch (DecoderException e){
232 e.printStackTrace();
233 }
234
235
236 try{
237 dc.ReadEncryptedFile (input, output);
238 System.out.println ("decryption finished to " + output.getName ());
239 }catch (IllegalBlockSizeException e){
240 e.printStackTrace();
241 }catch (BadPaddingException e){
242 e.printStackTrace();
243 }catch (IOException e){
244 e.printStackTrace();
245 }
246 }
247
248
249
250}
251
252private void extarctDataBase() throws IOException {
253 // Open your local db as the input stream
254 String outFileName = DB_PATH + DB_NAME;
255 String extFileName = DB_PATH + SEC_NAME;
256 DBEncript dc = new DBEncript ("xont@123");
257 File eoutput = new File (extFileName);
258 File doutput = new File (outFileName);
259 System.out.println("==START===");
260 dc.callRead(eoutput, doutput);
261 System.out.println ("decryption finished to " + doutput.getName ());
262
263}
264
265mDecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
266
267mDecipher = Cipher.getInstance("AES/CFB8/NoPadding");