· 6 years ago · Jan 04, 2020, 01:32 PM
1package com.knowledgeflex.myencdycexo;
2
3import androidx.appcompat.app.AppCompatActivity;
4
5import android.os.Bundle;
6import android.util.Log;
7
8import java.io.File;
9import java.io.FileInputStream;
10import java.io.FileNotFoundException;
11import java.io.FileOutputStream;
12import java.io.IOException;
13import java.nio.charset.StandardCharsets;
14import java.security.InvalidAlgorithmParameterException;
15import java.security.InvalidKeyException;
16import java.security.NoSuchAlgorithmException;
17import java.security.SecureRandom;
18
19import javax.crypto.BadPaddingException;
20import javax.crypto.Cipher;
21import javax.crypto.IllegalBlockSizeException;
22import javax.crypto.KeyGenerator;
23import javax.crypto.NoSuchPaddingException;
24import javax.crypto.SecretKey;
25import javax.crypto.SecretKeyFactory;
26import javax.crypto.spec.IvParameterSpec;
27import javax.crypto.spec.SecretKeySpec;
28
29public class MainActivity extends AppCompatActivity {
30 private static Cipher cipher;
31 private int size;
32 private byte[] bytes;
33 private byte[] iv = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
34
35 @Override
36 protected void onCreate(Bundle savedInstanceState) {
37 super.onCreate(savedInstanceState);
38 setContentView(R.layout.activity_main);
39
40 try {
41 cipher = Cipher.getInstance("AES/CTR/NoPadding");
42 } catch (NoSuchAlgorithmException e) {
43 e.printStackTrace();
44 } catch (NoSuchPaddingException e) {
45 e.printStackTrace();
46 }
47
48 /*KeyGenerator keyGenerator = null;
49 try {
50 keyGenerator = KeyGenerator.getInstance("AES");
51 } catch (NoSuchAlgorithmException e) {
52 e.printStackTrace();
53 }
54
55 SecureRandom secureRandom = new SecureRandom();
56 int keyBitSize = 256;
57 keyGenerator.init(keyBitSize, secureRandom);
58
59 SecretKey secretKey = keyGenerator.generateKey(); */
60
61 SecretKeySpec key = new SecretKeySpec("abcdefghijklmnop".getBytes(StandardCharsets.UTF_8), "AES");
62
63 try {
64 try {
65 cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
66 } catch (InvalidAlgorithmParameterException e) {
67 e.printStackTrace();
68 }
69 } catch (InvalidKeyException e) {
70 e.printStackTrace();
71 }
72
73 // byte[] testBytes = "This is a test String".getBytes(StandardCharsets.UTF_8);
74
75 String path = getExternalFilesDir("enc_test").getAbsolutePath();
76
77 File file = new File(path, "encTextTest.mp4");
78
79 if (file.exists()) {
80 size = (int) file.length();
81 Log.v("MainActivity" , " File Size : " + size);
82 bytes = new byte[size];
83
84 }
85 FileInputStream fileInputStream = null;
86 try {
87 fileInputStream = new FileInputStream(file);
88 } catch (FileNotFoundException e) {
89 e.printStackTrace();
90 }
91 try {
92 fileInputStream.read(bytes, 0, size - 1);
93 } catch (IOException e) {
94 e.printStackTrace();
95 }
96 try {
97 fileInputStream.close();
98 } catch (IOException e) {
99 e.printStackTrace();
100 }
101 File outputFile = new File(path, "decTextTest.mp4");
102
103
104 FileOutputStream fileOutputStream = null;
105 try {
106 fileOutputStream = new FileOutputStream(outputFile , false);
107 } catch (FileNotFoundException e) {
108 e.printStackTrace();
109 }
110 if(fileOutputStream != null) {
111 try {
112 Log.v("MainActivity" , " Dec Started : " + size);
113 fileOutputStream.write(cipher.doFinal(bytes));
114 fileOutputStream.flush();
115 fileOutputStream.close();
116 Log.v("MainActivity" , " Dec Done : " + size);
117 } catch (IOException e) {
118 e.printStackTrace();
119 } catch (BadPaddingException e) {
120 e.printStackTrace();
121 } catch (IllegalBlockSizeException e) {
122 e.printStackTrace();
123 }
124 }
125 /* try {
126 bytes = cipher.doFinal(testBytes);
127 String normal = new String(testBytes, StandardCharsets.UTF_8);
128 String s = new String(bytes, StandardCharsets.UTF_8);
129 Log.v("MainActivity : ", "Normal : " + normal + "Encryption Succesful : " + s );
130 } catch (BadPaddingException e) {
131 e.printStackTrace();
132 } catch (IllegalBlockSizeException e) {
133 e.printStackTrace();
134 } */
135
136 /* Cipher cipher1 = null;
137 try {
138 cipher1 = Cipher.getInstance("AES/CTR/NoPadding");
139 } catch (NoSuchAlgorithmException e) {
140 e.printStackTrace();
141 } catch (NoSuchPaddingException e) {
142 e.printStackTrace();
143 }*/
144 /* try {
145 cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
146 } catch (InvalidKeyException e) {
147 e.printStackTrace();
148 } catch (InvalidAlgorithmParameterException e) {
149 e.printStackTrace();
150 }
151 try {
152 String s = new String(cipher.doFinal(bytes), StandardCharsets.UTF_8);
153 Log.v("MainActivity : ", "Decryption Succesful : " + s);
154 } catch (BadPaddingException e) {
155 e.printStackTrace();
156 } catch (IllegalBlockSizeException e) {
157 e.printStackTrace();
158 } */
159 }
160}