· 9 years ago · Sep 09, 2016, 06:50 PM
1package com.example.filecryptoapp;
2
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.security.InvalidKeyException;
8import java.security.Key;
9import java.security.NoSuchAlgorithmException;
10import java.security.spec.InvalidKeySpecException;
11import java.security.spec.KeySpec;
12
13import javax.crypto.BadPaddingException;
14import javax.crypto.Cipher;
15import javax.crypto.IllegalBlockSizeException;
16import javax.crypto.NoSuchPaddingException;
17import javax.crypto.SecretKey;
18import javax.crypto.SecretKeyFactory;
19import javax.crypto.spec.PBEKeySpec;
20import javax.crypto.spec.SecretKeySpec;
21
22import android.util.Base64;
23
24/**
25 * A utility class that encrypts or decrypts a file.
26 * @author www.codejava.net
27 *
28 */
29public class CryptoUtils {
30 private static final String ALGORITHM = "AES";
31 private static final String TRANSFORMATION = "AES";
32 private static final int MAX_FILE_BUF=1024;
33 public static void encrypt(String key, File inputFile, File outputFile)
34 throws CryptoException {
35 doCrypto(Cipher.ENCRYPT_MODE, key, inputFile, outputFile);
36 }
37
38 public static void decrypt(String key, File inputFile, File outputFile)
39 throws CryptoException {
40 doCrypto(Cipher.DECRYPT_MODE, key, inputFile, outputFile);
41 }
42
43 private static void doCrypto(int cipherMode, String key, File inputFile,
44 File outputFile) throws CryptoException {
45 try {
46 Key secretKey =generatedefault256bitkey(key);
47 // Key secretKey = new SecretKeySpec(key.getBytes(), ALGORITHM);
48 Cipher cipher = Cipher.getInstance(TRANSFORMATION);
49
50 cipher.init(cipherMode, secretKey);
51 FileInputStream inputStream = new FileInputStream(inputFile);
52 /* byte[] inputBytes = new byte[(int) inputFile.length()];
53 inputStream.read(inputBytes);
54
55 byte[] outputBytes = cipher.doFinal(inputBytes);
56 */
57 FileOutputStream outputStream = new FileOutputStream(outputFile);
58 /* outputStream.write(outputBytes);
59
60 inputStream.close();
61 outputStream.close();
62 */
63 int nread=0; byte [] inbuf = new byte [MAX_FILE_BUF];
64 long totalread = 0;
65 while ((nread = inputStream.read (inbuf)) > 0 )
66 {
67 totalread += nread;
68
69 byte [] trimbuf = new byte [nread];
70 for (int i = 0; i < nread; i++)
71 trimbuf[i] = inbuf[i];
72
73
74 byte [] tmp = cipher.update(trimbuf);
75
76 if (tmp != null)
77 outputStream.write (tmp);
78 }
79
80
81 byte [] finalbuf = cipher.doFinal ();
82 if (finalbuf != null)
83 outputStream.write (finalbuf);
84
85 outputStream.flush();
86 inputStream.close();
87 outputStream.close();
88
89
90 } catch (NoSuchPaddingException | NoSuchAlgorithmException
91 | InvalidKeyException | BadPaddingException
92 | IllegalBlockSizeException | IOException ex) {
93 throw new CryptoException("Error encrypting/decrypting file", ex);
94 }
95
96 }
97 private static SecretKey generatedefault256bitkey(String password)
98 {String key="";
99 password= getfake256bitkey(password);
100 for(int i=0 ;i<64;i++)
101 {
102 char c;
103
104 c=password.charAt(i);
105 if(i%2==0){
106 key += getcharpos(c)%10;
107
108 }
109 else
110 {
111 key += getcharisvow(c).toLowerCase();
112 }
113
114 }
115
116 SecretKey secret=null;
117 try {
118 SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
119 KeySpec spec = new PBEKeySpec(password.toCharArray(), key.getBytes(), 1920, 256);
120 SecretKey tmp = factory.generateSecret(spec);
121 secret = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);
122 } catch (NoSuchAlgorithmException e) {
123 // TODO Auto-generated catch block
124 e.printStackTrace();
125 } catch (InvalidKeySpecException e) {
126 // TODO Auto-generated catch block
127 e.printStackTrace();
128 }
129
130
131 return secret;
132 }
133 private static int getcharpos(char c)
134 {
135 if(Character.isLowerCase(c))
136 {
137 return (int)c-96;
138 }
139 else
140 return (int)c-64;
141
142 }
143 private static String getfake256bitkey(String password)
144 {
145 String fakekey="";
146 for(int i=0,index=0 ;i<64;i++,index++)
147 {
148 if(password.length()>i)
149 fakekey += password.charAt(index);
150 else
151 {index=0;
152 fakekey += password.charAt(index);
153 }
154 }
155 return fakekey;
156 }
157 private static String getcharisvow(char c)
158 {
159 c = Character.toLowerCase(c);
160 if(c=='a' || c=='e' || c=='i' ||c=='o' ||c=='u' )
161 return "Y";
162 else
163 return "N";
164 }
165
166 public static String get256bitKey(String password)
167 {
168
169 SecretKey secret = generatedefault256bitkey(password);
170 String key256 =Base64.encodeToString(secret.getEncoded(),Base64.DEFAULT);
171
172 return key256;
173
174 }
175 public static SecretKey getSecretkey(String key256)
176 {
177 byte[] decodedKey = Base64.decode(key256,Base64.DEFAULT);
178
179 SecretKey secret = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
180 return secret;
181 }
182}