· 8 years ago · Dec 21, 2017, 06:00 AM
1protected void EncryptAndroid()
2{
3byte[] 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 };
7byte[] 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',};
9byte[] 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
12try {
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}
58
59protected void DecryptAndroid() {
60 byte[] hBytes = new byte[10];
61 byte[] dBytes = new byte[100];
62
63try {
64 Log.d("Decryption", "Reading iv data ");
65 File f1 = new File(sdCard.getAbsolutePath() + "/Notes/iv.dat");
66 byte[] newivtext = new byte[(int) f1.length()];
67 FileInputStream readivStream = new FileInputStream(sdCard.getAbsolutePath() + "/Notes/iv.dat");
68 if (readivStream != null) {
69 readivStream.read(newivtext);
70 }
71
72 File f2 = new File(sdCard.getAbsolutePath() + "/Notes/iv1.dat");
73 byte[] newivtext1 = new byte[(int) f2.length()];
74 FileInputStream readivStream1 = new FileInputStream(sdCard.getAbsolutePath() + "/Notes/iv1.dat");
75 if (readivStream1 != null) {
76 readivStream1.read(newivtext1);
77 }
78
79 // The key can also be obtained from the Android Keystore any time as follows:
80 KeyStore keyStore = KeyStore.getInstance("AndroidKeyStore");
81 keyStore.load(null);
82 SecretKey dsecret = (SecretKey) keyStore.getKey("key2", null);
83
84 // Initialize dcipher
85 Cipher dcipher = Cipher.getInstance("AES/CTR/NoPadding");
86 dcipher.init(Cipher.DECRYPT_MODE, dsecret,new IvParameterSpec(newivtext1));
87
88 FileInputStream inputStream = new FileInputStream(sdCard.getAbsolutePath() + "/Notes/sample.encrypted");
89 FileOutputStream os = new FileOutputStream(sdCard.getAbsolutePath() + "/Notes/sample.decrypted");
90
91 //decrypt header
92 int b = inputStream.read(hBytes);
93 byte[] dB = new byte[10];
94 dB = dcipher.doFinal(hBytes);
95 os.write(dB, 0, b);
96
97 // Re-Initialize dcipher
98 dcipher = Cipher.getInstance("AES/CTR/NoPadding");
99 dcipher.init(Cipher.DECRYPT_MODE, dsecret,new IvParameterSpec(newivtext));
100 FileInputStream inputStream1 = new FileInputStream(sdCard.getAbsolutePath() + "/Notes/sample.encrypted");
101 inputStream1.skip(10);
102 CipherInputStream cis = new CipherInputStream(inputStream1, dcipher);
103 b = cis.read(dBytes);
104 while (b != -1) {
105 Log.d("Decryption", "Bytes decrypted" + b);
106 os.write(dBytes, 0, b);
107 }
108 cis.close();
109 os.close();
110
111} catch (Exception e) {
112 e.printStackTrace();
113}
114}