· 7 years ago · May 13, 2018, 12:26 PM
1package lb.themike10452.ocrn00b;
2
3import android.content.Context;
4
5import java.io.ByteArrayOutputStream;
6import java.io.IOException;
7import java.io.InputStream;
8import java.lang.ref.WeakReference;
9import java.security.InvalidKeyException;
10import java.security.NoSuchAlgorithmException;
11import java.security.spec.InvalidKeySpecException;
12
13import javax.crypto.Cipher;
14import javax.crypto.CipherInputStream;
15import javax.crypto.NoSuchPaddingException;
16import javax.crypto.SecretKey;
17import javax.crypto.SecretKeyFactory;
18import javax.crypto.spec.DESKeySpec;
19
20public final class AssetLoader {
21
22 private static final String DesKey = "!@#$%^&a";
23
24 private WeakReference<Context> mContextRef;
25
26 public AssetLoader(WeakReference<Context> contextRef) {
27 this.mContextRef = contextRef;
28 }
29
30 public String LoadAssetString(String name) throws IOException {
31 byte[] data = LoadAssetBytes(name);
32 if (data != null) {
33 return new String(data);
34 } else {
35 return null;
36 }
37 }
38
39 public byte[] LoadAssetBytes(String name) throws IOException {
40 InputStream is = null;
41 CipherInputStream cis = null;
42
43 try {
44 is = mContextRef.get().getAssets().open(name);
45 Cipher des = Cipher.getInstance("DES");
46 DESKeySpec dks = new DESKeySpec(DesKey.getBytes());
47 SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
48 SecretKey desKey = skf.generateSecret(dks);
49
50 des.init(Cipher.DECRYPT_MODE, desKey);
51
52 cis = new CipherInputStream(is, des);
53 ByteArrayOutputStream baos = new ByteArrayOutputStream();
54
55 int size = 4096;
56 byte[] buffer = new byte[size];
57 int read;
58 while ((read = cis.read(buffer, 0, size)) != -1) {
59 baos.write(buffer, 0, read);
60 }
61
62 baos.flush();
63
64 return baos.toByteArray();
65 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | InvalidKeySpecException e) {
66 // do nothing
67 return null;
68 } finally {
69 if (cis != null) {
70 cis.close();
71 }
72
73 if (is != null) {
74 is.close();
75 }
76 }
77 }
78}