forked from MasterFlomaster1/JFXCrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDes.java
More file actions
161 lines (146 loc) · 5.62 KB
/
Copy pathDes.java
File metadata and controls
161 lines (146 loc) · 5.62 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package Cipher;
import GUI.Main.AlertDialog;
import Utils.RepairString;
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
class Des {
private SecretKey key;
private Cipher cipher;
private IvParameterSpec ivParameterSpec;
Des() {
try {
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
e.printStackTrace();
return;
}
generateIV();
generateKey();
}
String encryptString(String text) {
try {
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
return Base64.getEncoder().encodeToString(cipher.doFinal(text.getBytes(StandardCharsets.UTF_8)));
} catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
AlertDialog.showError("DES text encryption error", e.toString());
return null;
}
}
String decryptString(String encryptedText) {
byte[] data;
try {
data = Base64.getDecoder().decode(encryptedText.getBytes(StandardCharsets.UTF_8));
} catch (IllegalArgumentException e) {
System.out.println("IllegalArgumentException caught");
data = Base64.getDecoder().decode(RepairString.repairString(encryptedText).getBytes(StandardCharsets.UTF_8));
}
try {
cipher.init(Cipher.DECRYPT_MODE, key, ivParameterSpec);
return new String(cipher.doFinal(data), StandardCharsets.UTF_8);
} catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
AlertDialog.showError("Error occurred while decrypting text", e.toString());
return null;
}
}
void encryptFile(File in, File out) {
try {
cipher.init(Cipher.ENCRYPT_MODE, key, ivParameterSpec);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
AlertDialog.showError("DES init error!", e.toString());
return;
}
byte[] iv = cipher.getIV();
byte[] buffer = new byte[2048];
try {
FileOutputStream fileOut = new FileOutputStream(out);
FileInputStream fileIn = new FileInputStream(in);
CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher);
fileOut.write(iv);
while (fileIn.read(buffer, 0, buffer.length)!=-1) {
cipherOut.write(buffer, 0, buffer.length);
}
fileIn.close();
fileOut.close();
cipherOut.close();
AlertDialog.showInfo("File was successfully encrypted!");
} catch (IOException e) {
e.printStackTrace();
AlertDialog.showError("DES file encryption error!", e.toString());
}
}
void decryptFile(File in, File out) {
IvParameterSpec parameterSpec;
try {
byte[] iv = new byte[8];
FileInputStream temp = new FileInputStream(in);
temp.read(iv);
parameterSpec = new IvParameterSpec(iv);
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
cipher.init(Cipher.DECRYPT_MODE, key, parameterSpec);
} catch (InvalidKeyException e) {
e.printStackTrace();
AlertDialog.showError("DES file decryption error!", e.toString());
return;
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
return;
//perform file decryption without initialization vector
}
byte[] buffer = new byte[2048];
try {
FileOutputStream fileOut = new FileOutputStream(out);
FileInputStream fileIn = new FileInputStream(in);
CipherOutputStream cipherOut = new CipherOutputStream(fileOut, cipher);
while (fileIn.read(buffer, 0, buffer.length)!=-1) {
cipherOut.write(buffer, 0, buffer.length);
}
fileIn.close();
fileOut.close();
cipherOut.close();
AlertDialog.showInfo("File was successfully decrypted!");
} catch (IOException e) {
e.printStackTrace();
AlertDialog.showError("DES file decryption error!", e.toString());
}
}
void generateKey() {
try {
KeyGenerator generator = KeyGenerator.getInstance("DES");
key = generator.generateKey();
System.out.println("DES key generated");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
private void generateIV() {
SecureRandom secureRandom = new SecureRandom();
byte[] iv = new byte[8];
secureRandom.nextBytes(iv);
ivParameterSpec = new IvParameterSpec(iv);
}
void setKey(String stringKey) {
key = new SecretKeySpec(stringKey.getBytes(), "AES");
System.out.println("DES key set");
}
String getKey() {
return Base64.getEncoder().encodeToString(key.getEncoded());
}
}