· 7 years ago · Aug 31, 2018, 03:48 AM
1 public static InputStream decompress(InputStream is) throws IOException {
2 byte[] compressed = Base64.decode(zipText);
3 if (is.available() > 4) {
4 return new BufferedInputStream(new GZIPInputStream(
5 new ByteArrayInputStream(new Base64InputStream(is), 4, compressed.length - 4)));
6 } else {
7 return null;
8 }
9 }
10
11 public static String decrypt(InputStream is, String password)
12 throws Exception {
13
14 return decrypt(new Base64InputStream(is), password);
15 }
16
17 public static InputStream decrypt(InputStream is, String password) throws Exception {
18 byte[] passwordKey = encodeDigest(password);
19 try {
20 aesCipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
21 } catch (NoSuchAlgorithmException e) {
22 throw new Exception(
23 "Decryption Exception: No such algorithm\r\n" + e
24 .toString());
25 } catch (NoSuchPaddingException e) {
26 throw new Exception(
27 "Decryption Exception: No such padding PKCS5\r\n" + e
28 .toString());
29 }
30 secretKey = new SecretKeySpec(passwordKey, CIPHER_ALGORITHM);
31
32 try {
33 aesCipher.init(Cipher.DECRYPT_MODE, secretKey, ivParameterSpec);
34 } catch (InvalidKeyException e) {
35 throw new Exception(
36 "Decryption Exception: Invalid key\r\n" + e.toString());
37 } catch (InvalidAlgorithmParameterException e) {
38 throw new Exception(
39 "Decryption Exception: Invalid algorithm\r\n" + e
40 .toString());
41 }
42
43 CipherInputStream cis = new CipherInputStream(is, aesCipher);
44 try {
45 return new CipherInputStream(is, aesCipher);
46 } catch (IllegalBlockSizeException e) {
47 throw new Exception(
48 "Decryption Exception: Illegal block size\r\n" + e
49 .toString());
50 } catch (BadPaddingException e) {
51 throw new Exception(
52 "Decryption Exception: Bad padding\r\n" + e
53 .toString());
54 }
55 }
56
57 public EntMyClass getDatat(InputStream is) {
58 JsonReader r = null;
59 try {
60 Reader reader = new BufferedReader(new InputStreamReader(is));
61 r = new JsonReader(reader);
62 result = gson.fromJson(r, EntMyClass.class);
63 } finally {
64 if (null != r) {
65 r.close();
66 }
67 }
68 }