-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAESExample.java
More file actions
133 lines (112 loc) · 4.1 KB
/
AESExample.java
File metadata and controls
133 lines (112 loc) · 4.1 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
126
127
128
129
130
131
132
133
import java.security.Key;
import java.util.Base64;
import java.io.*;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JFrame;
public class AESExample {
private static final String ALGO = "AES";
private static byte[] keyvalue;
public static void EnImage(int key, String filepath, JFrame p) {
try {
if(key==0){
key = 1212;
}
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(filepath));
int total = fis.available();
ProgressDialog jp = new ProgressDialog(p);
jp.jprog.setMinimum(0);
jp.jprog.setMaximum(total);
jp.setTitle("Encrypting...");
jp.jprog.setStringPainted(true);
jp.setVisible(true);
int j = 1;
byte[] data = new byte[fis.available()];
fis.read(data);
int i = 0;
for (byte b : data) {
data[i] = (byte) (b ^ key);
i++;
// progressbar
jp.jprog.paintImmediately(0, 0, 1000, 100);
jp.jprog.setValue(j);
j++;
}
fis.close();
jp.setVisible(false);
String Temp = filepath;
filepath = filepath + ".fileEnc";
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filepath));
fos.write(data);
fos.close();
File deleteFile = new File(Temp);
deleteFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void DeImage(int key, String filepath, JFrame p) {
try {
if(key==0){
key = 1212;
}
BufferedInputStream fis = new BufferedInputStream(new FileInputStream(filepath));
int total = fis.available();
ProgressDialog jp = new ProgressDialog(p);
jp.jprog.setMinimum(0);
jp.jprog.setMaximum(total);
jp.setTitle("Decrypting...");
jp.jprog.setStringPainted(true);
jp.setVisible(true);
int j = 1;
byte[] data = new byte[fis.available()];
fis.read(data);
int i = 0;
for (byte b : data) {
data[i] = (byte) (b ^ key);
i++;
// progressbar
jp.jprog.paintImmediately(0, 0, 1000, 100);
jp.jprog.setValue(j);
j++;
}
fis.close();
jp.setVisible(false);
String Temp = filepath;
filepath = filepath.substring(0, filepath.length() - 8);
BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(filepath));
fos.write(data);
fos.close();
File deleteFile = new File(Temp);
deleteFile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String encrypt(String Data, String keyString) throws Exception {
keyvalue = keyString.getBytes();
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
Base64.Encoder encoder = Base64.getEncoder();
byte[] byteArr2 = encoder.encode(encVal);
String encryptedValue = new String(byteArr2);
return encryptedValue;
}
public static String decrypt(String encryptedData, String keyString) throws Exception {
keyvalue = keyString.getBytes();
Base64.Decoder decoder = Base64.getDecoder();
byte[] byteArr3 = decoder.decode(encryptedData);
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decValue = c.doFinal(byteArr3);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyvalue, ALGO);
return key;
}
}