1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
package de.pixart.messenger.utils;
import android.util.Log;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.CipherOutputStream;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import de.pixart.messenger.Config;
public class EncryptDecryptFile {
private static String cipher_mode = "AES/CBC/PKCS5Padding";
public static void encrypt(FileInputStream iFile, FileOutputStream oFile, String iKey) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
byte[] key = iKey.getBytes("UTF-8");
Log.d(Config.LOGTAG, "Cipher key: " + Arrays.toString(key));
MessageDigest sha = MessageDigest.getInstance("SHA-1");
Log.d(Config.LOGTAG, "Cipher sha: " + sha.toString());
key = sha.digest(key);
Log.d(Config.LOGTAG, "Cipher sha key: " + Arrays.toString(key));
key = Arrays.copyOf(key, 16); // use only first 128 bit
Log.d(Config.LOGTAG, "Cipher sha key 16 bytes: " + Arrays.toString(key));
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(cipher_mode);
cipher.init(Cipher.ENCRYPT_MODE, sks);
Log.d(Config.LOGTAG, "Cipher IV: " + Arrays.toString(cipher.getIV()));
CipherOutputStream cos = new CipherOutputStream(oFile, cipher);
Log.d(Config.LOGTAG, "Encryption with: " + cos.toString());
int b;
byte[] d = new byte[8];
while ((b = iFile.read(d)) != -1) {
cos.write(d, 0, b);
}
cos.flush();
cos.close();
iFile.close();
}
public static void decrypt(FileInputStream iFile, FileOutputStream oFile, String iKey) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
byte[] key = iKey.getBytes("UTF-8");
Log.d(Config.LOGTAG, "Cipher key: " + Arrays.toString(key));
MessageDigest sha = MessageDigest.getInstance("SHA-1");
Log.d(Config.LOGTAG, "Cipher sha: " + sha.toString());
key = sha.digest(key);
Log.d(Config.LOGTAG, "Cipher sha key: " + Arrays.toString(key));
key = Arrays.copyOf(key, 16); // use only first 128 bit
Log.d(Config.LOGTAG, "Cipher sha key 16 bytes: " + Arrays.toString(key));
SecretKeySpec sks = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance(cipher_mode);
cipher.init(Cipher.DECRYPT_MODE, sks);
Log.d(Config.LOGTAG, "Cipher IV: " + Arrays.toString(cipher.getIV()));
CipherInputStream cis = new CipherInputStream(iFile, cipher);
Log.d(Config.LOGTAG, "Decryption with: " + cis.toString());
int b;
byte[] d = new byte[8];
while ((b = cis.read(d)) != -1) {
oFile.write(d, 0, b);
}
oFile.flush();
oFile.close();
cis.close();
}
}
|