· 8 years ago · Feb 01, 2018, 10:16 AM
1// encryption and decriptin files
2 file = new File(
3 Environment.getExternalStorageDirectory() + "/" + "encrypt image/" + "img_encrypt.bin");
4 findById();
5
6 }
7
8 private String getUrlString() {
9 return getIntent().getStringExtra("imageUrl");
10 }
11
12 private void findById() {
13
14 image = findViewById(R.id.image_view);
15
16 String url = file.getAbsolutePath();
17
18 byte[] byte_key = LoadData("key.enc");
19 SecretKey key = new SecretKeySpec(byte_key, "AES");
20
21
22 // read file from assets
23// byte[] content = LoadData("img_encrypt.bin");
24
25 // read file from sdcard
26 byte[] content = OpenFileDialog(url);
27
28
29 byte[] decrypted = decryptFile(key, content);
30
31 image.setImageBitmap(ByteArrayToBitmap(decrypted));
32 }
33
34
35 public byte[] LoadData(String inFile) {
36 String tContents = "";
37
38 try {
39 InputStream stream = getAssets().open(inFile);
40
41 int size = stream.available();
42 buffer = new byte[size];
43 stream.read(buffer);
44 stream.close();
45 tContents = new String(buffer);
46 } catch (IOException e) {
47 // Handle exceptions here
48 }
49
50 return buffer;
51
52 }
53
54
55 public Bitmap ByteArrayToBitmap(byte[] byteArray) {
56 ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(byteArray);
57 Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);
58 return bitmap;
59 }
60
61
62 public byte[] OpenFileDialog(String file) {
63
64 FileInputStream fis;
65 String content = "";
66 try {
67
68 fis = new FileInputStream(new File(file));
69 input = new byte[fis.available()];
70 while (fis.read(input) != -1) {
71 }
72 content += new String(input);
73 fis.close();
74 } catch (FileNotFoundException e) {
75 e.printStackTrace();
76 } catch (IOException e) {
77 e.printStackTrace();
78 }
79 return input;
80 }
81
82// encryption class
83
84public class ImageEncoderDecoder {
85
86
87// is = context.getAssets().open("key.enc");
88
89
90 public static byte[] getFile(String file_location) {
91 File f = new File(file_location);
92 InputStream is = null;
93 try {
94 is = new FileInputStream(f);
95 } catch (FileNotFoundException e2) {
96 e2.printStackTrace();
97 }
98 byte[] content = null;
99 try {
100 content = new byte[is.available()];
101 } catch (IOException e1) {
102 e1.printStackTrace();
103 }
104 try {
105 is.read(content);
106 } catch (IOException e) {
107 e.printStackTrace();
108 }
109
110 return content;
111 }
112
113 public static byte[] encryptFile(Key key, byte[] content) {
114 Cipher cipher;
115 byte[] encrypted = null;
116 try {
117 cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
118 cipher.init(Cipher.ENCRYPT_MODE, key);
119 encrypted = cipher.doFinal(content);
120 } catch (Exception e) {
121 e.printStackTrace();
122 }
123 return encrypted;
124
125 }
126
127 public static byte[] decryptFile(Key key, byte[] textCryp) {
128 Cipher cipher;
129 byte[] decrypted = null;
130 try {
131 cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
132 cipher.init(Cipher.DECRYPT_MODE, key);
133 decrypted = cipher.doFinal(textCryp);
134 } catch (Exception e) {
135 e.printStackTrace();
136 }
137
138 return decrypted;
139 }
140
141 public static void saveFile(byte[] bytes, String location) throws IOException {
142 FileOutputStream fos = new FileOutputStream(location);
143 fos.write(bytes);
144 fos.close();
145 }
146}