· 6 years ago · Aug 29, 2019, 10:08 AM
1import android.os.Environment;
2import org.junit.Test;
3import java.io.File;
4import java.io.FileInputStream;
5import java.io.FileOutputStream;
6import java.io.IOException;
7import java.security.InvalidAlgorithmParameterException;
8import java.security.InvalidKeyException;
9import java.security.Key;
10import java.security.NoSuchAlgorithmException;
11
12import javax.crypto.BadPaddingException;
13import javax.crypto.Cipher;
14import javax.crypto.IllegalBlockSizeException;
15import javax.crypto.NoSuchPaddingException;
16import javax.crypto.spec.IvParameterSpec;
17import javax.crypto.spec.SecretKeySpec;
18
19public class CryptoUtilsTest {
20
21
22 private static void doCryptoInAES(int cipherMode, String key, File inputFile,
23 File outputFile) throws CryptoException {
24 try {
25 Key secretKey = new SecretKeySpec(key.getBytes(), "AES");
26 Cipher cipher = Cipher.getInstance("AES");
27 cipher.init(cipherMode, secretKey);
28
29 FileInputStream inputStream = new FileInputStream(inputFile);
30 byte[] inputBytes = new byte[(int) inputFile.length()];
31 inputStream.read(inputBytes);
32
33 byte[] outputBytes = cipher.doFinal(inputBytes);
34
35 FileOutputStream outputStream = new FileOutputStream(outputFile);
36 outputStream.write(outputBytes);
37
38 inputStream.close();
39 outputStream.close();
40
41 } catch (NoSuchPaddingException | NoSuchAlgorithmException
42 | InvalidKeyException | BadPaddingException
43 | IllegalBlockSizeException | IOException ex) {
44 throw new CryptoException("Error encrypting/decrypting file", ex);
45 }
46 }
47
48 private static void doCryptoInBlowFish(int cipherMode,String KEY,File inputFile,File outputFile) throws CryptoException{
49 String ALGORITHM = "Blowfish";
50 String MODE = "Blowfish/CBC/PKCS5Padding";
51 String IV = "!a3edr45";
52
53 try {
54 Key secretKey = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
55 Cipher cipher = Cipher.getInstance(MODE);
56 cipher.init(cipherMode, secretKey, new IvParameterSpec(IV.getBytes()));
57
58 FileInputStream inputStream = new FileInputStream(inputFile);
59 byte[] inputBytes = new byte[(int) inputFile.length()];
60 inputStream.read(inputBytes);
61
62 byte[] outputBytes = cipher.doFinal(inputBytes);
63
64 FileOutputStream outputStream = new FileOutputStream(outputFile);
65 outputStream.write(outputBytes);
66
67 inputStream.close();
68 outputStream.close();
69
70 } catch (NoSuchPaddingException | NoSuchAlgorithmException
71 | InvalidKeyException | BadPaddingException
72 | IllegalBlockSizeException | IOException ex) {
73 ex.fillInStackTrace();
74 } catch (InvalidAlgorithmParameterException e) {
75 e.printStackTrace();
76 }
77 }
78 @Test
79 public void encryptFileinAES() throws CryptoException {
80 final File saveFile = new File(Environment.getExternalStorageDirectory(), "intruder.jpg");
81 doCryptoInAES(Cipher.ENCRYPT_MODE,"ak$#54%^RtF%g^Hf",saveFile,saveFile);
82 doCryptoInAES(Cipher.DECRYPT_MODE,"ak$#54%^RtF%g^Hf",saveFile,saveFile);
83 }
84 @Test
85 public void encryptFileinBlowfish() throws CryptoException {
86 final File saveFile = new File(Environment.getExternalStorageDirectory(), "intruder.jpg");
87 doCryptoInBlowFish(Cipher.ENCRYPT_MODE,"SAMPLEKEY",saveFile,saveFile);
88 doCryptoInBlowFish(Cipher.DECRYPT_MODE,"SAMPLEKEY",saveFile,saveFile);
89 }
90}