-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAES.java
More file actions
125 lines (103 loc) · 4.25 KB
/
Copy pathAES.java
File metadata and controls
125 lines (103 loc) · 4.25 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package com.ilcapo.framework.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AES {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/CBC/PKCS5PADDING";
private boolean badDecryptionKey = false;
public void encrypt(SecretKey key, SecretKey initVector, File inputFile, File outputFile) {
doCrypto(Cipher.ENCRYPT_MODE, key, initVector, inputFile, outputFile);
}
public void decrypt(SecretKey key, SecretKey initVector, File inputFile, File outputFile) {
doCrypto(Cipher.DECRYPT_MODE, key, initVector, inputFile, outputFile);
}
private void doCrypto(int cipherMode, SecretKey key, SecretKey initVector, File inputFile, File outputFile) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getEncoded());
Key secretKey = new SecretKeySpec(key.getEncoded(), ALGORITHM);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(cipherMode, secretKey, iv);
FileInputStream inputStream = new FileInputStream(inputFile);
byte[] inputBytes = new byte[(int) inputFile.length()];
inputStream.read(inputBytes);
byte[] outputBytes = cipher.doFinal(inputBytes);
FileOutputStream outputStream = new FileOutputStream(outputFile);
outputStream.write(outputBytes);
inputStream.close();
outputStream.close();
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IOException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
//e.printStackTrace();
System.err.print("A bad key is used during decryption");
badDecryptionKey = true;
}
}
public boolean isDecryptionKeyBad() {
return badDecryptionKey;
}
public SecretKey generateIV() throws NoSuchAlgorithmException {
long timestamp = System.currentTimeMillis()*1000;
SecureRandom random = new SecureRandom();
random.setSeed(timestamp);
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, random); // 128 bits key size
return kgen.generateKey();
}
public SecretKey generateKey(String seed) throws NoSuchAlgorithmException {
SecureRandom random = new SecureRandom();
random.setSeed(convertSeed(seed));
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, random); // 128 bits key size
return kgen.generateKey();
}
private long convertSeed(String strSeed) {
long seed = 0;
for (int i = 0; i < strSeed.length(); i++) {
char ch = strSeed.charAt(i);
seed = seed + (long)ch;
}
return seed;
}
public void writeKeyToFile(SecretKey key, String filename) throws IOException {
FileOutputStream fos = new FileOutputStream(new File(filename));
ObjectOutputStream out = new ObjectOutputStream(fos);
try {
out.writeObject(key);
} finally {
out.close();
}
}
public SecretKey readKeyFromFile(String filename) throws IOException {
FileInputStream fis = new FileInputStream(new File(filename));
Key key = null;
ObjectInputStream oin = new ObjectInputStream(fis);
try {
key = (Key) oin.readObject();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
oin.close();
}
SecretKey sKey = new SecretKeySpec(key.getEncoded(), 0, key.getEncoded().length, "AES");
return sKey;
}
}