· 8 years ago · Dec 21, 2017, 02:22 AM
1protected void EncryptAndroid()
2{
3 byte[] data = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
4 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
5 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
6 };
7 byte[] header = {'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd',
8 'a', 'b', 'c', 'd',};
9 byte[] updatedHeader = {'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D',
10 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D', 'A', 'B', 'C', 'D'};
11
12 try {
13 SecretKey secret = generateKeyAndroid();
14
15 Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding");
16 cipher.init(Cipher.ENCRYPT_MODE, secret);
17
18 AlgorithmParameters params = cipher.getParameters();
19 byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
20
21 FileOutputStream os = new FileOutputStream(sdCard.getAbsolutePath() + "/Notes/sample.encrypted");
22 CipherOutputStream cos = new CipherOutputStream(os, cipher);
23
24 int offset = 0;
25 cos.write(header, offset, 10);
26 cos.write(data, offset, 40);
27
28 FileOutputStream ivStream = new FileOutputStream(sdCard.getAbsolutePath() + "/Notes/iv.dat");
29 if (ivStream != null) {
30 Log.d("Encryption", "Writing iv data to output file");
31 ivStream.write(iv);
32 }
33 ivStream.close();
34 cos.close();
35
36 //Create new instance of cipher
37 cipher = Cipher.getInstance("AES/CTR/NoPadding");
38 cipher.init(Cipher.ENCRYPT_MODE, secret);
39
40 AlgorithmParameters params1 = cipher.getParameters();
41 byte[] iv1 = params1.getParameterSpec(IvParameterSpec.class).getIV();
42
43 //Re open the file to Update the header at starting of the file.
44 FileOutputStream os1 = new FileOutputStream(sdCard.getAbsolutePath() + "/Notes/sample.encrypted");
45 CipherOutputStream cos1 = new CipherOutputStream(os1, cipher);
46 cos1.write(updatedHeader, offset, 10);
47 cos1.close();
48
49 FileOutputStream ivStream1 = new FileOutputStream(sdCard.getAbsolutePath() + "/Notes/iv1.dat");
50 if (ivStream1 != null) {
51 Log.d("Encryption", "Writing iv data to output file");
52 ivStream1.write(iv1);
53 }
54 }catch (Exception e) {
55 e.printStackTrace();
56 }
57}
58protected void DecryptAndroid() {
59 byte[] hBytes = new byte[10];
60 byte[] dBytes = new byte[100];
61
62 try {
63 Log.d("Decryption", "Reading iv data ");
64 File f1 = new File(sdCard.getAbsolutePath() + "/Notes/iv.dat");
65 byte[] newivtext = new byte[(int) f1.length()];
66 FileInputStream readivStream = new FileInputStream(sdCard.getAbsolutePath() + "/Notes/iv.dat");
67 if (readivStream != null) {
68 readivStream.read(newivtext);
69 }
70
71 File f2 = new File(sdCard.getAbsolutePath() + "/Notes/iv1.dat");
72 byte[] newivtext1 = new byte[(int) f2.length()];
73 FileInputStream readivStream1 = new FileInputStream(sdCard.getAbsolutePath() + "/Notes/iv1.dat");
74 if (readivStream1 != null) {
75 readivStream1.read(newivtext1);
76 }
77
78 // The key can also be obtained from the Android Keystore any time as follows:
79 KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
80 keyStore.load(null);
81 SecretKey dsecret = (SecretKey) keyStore.getKey("key2", null);
82
83 // Initialize dcipher
84 Cipher dcipher = Cipher.getInstance("AES/CTR/NoPadding");
85 dcipher.init(Cipher.DECRYPT_MODE, dsecret,new IvParameterSpec(newivtext1));
86
87 FileInputStream inputStream = new FileInputStream(sdCard.getAbsolutePath() + "/Notes/sample.encrypted");
88 FileOutputStream os = new FileOutputStream(sdCard.getAbsolutePath() + "/Notes/sample.decrypted");
89
90 //decrypt header
91 int b = inputStream.read(hBytes);
92 byte[] dB = new byte[10];
93 dB = dcipher.doFinal(hBytes);
94 os.write(dB, 0, b);
95
96 // Re-Initialize dcipher
97 dcipher = Cipher.getInstance("AES/CTR/NoPadding");
98 dcipher.init(Cipher.DECRYPT_MODE, dsecret,new IvParameterSpec(newivtext));
99 FileInputStream inputStream1 = new FileInputStream(sdCard.getAbsolutePath() + "/Notes/sample.encrypted");
100 inputStream1.skip(10);
101 CipherInputStream cis = new CipherInputStream(inputStream1, dcipher);
102 b = cis.read(dBytes);
103 while (b != -1) {
104 Log.d("Decryption", "Bytes decrypted" + b);
105 os.write(dBytes, 0, b);
106 }
107 cis.close();
108 os.close();
109
110 } catch (Exception e) {
111 e.printStackTrace();
112 }
113}