Skip to content

Commit f3a8c15

Browse files
committed
May be a workable yes just work enc.Dec. :)meybe
1 parent 5f78180 commit f3a8c15

File tree

16 files changed

+178
-102
lines changed

16 files changed

+178
-102
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>top.viewv</groupId>
66
<artifactId>EncBox</artifactId>
7-
<version>0.6.1-alpha</version>
7+
<version>0.7-alpha</version>
88

99
<properties>
1010
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

src/main/java/top/viewv/controller/MainController.java

Lines changed: 115 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
import javafx.fxml.Initializable;
66
import javafx.scene.Group;
77
import javafx.scene.control.Label;
8-
import javafx.scene.input.MouseEvent;
98
import javafx.scene.layout.AnchorPane;
109
import javafx.stage.DirectoryChooser;
1110
import javafx.stage.FileChooser;
1211
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1312
import top.viewv.model.mac.SHA;
13+
import top.viewv.model.symmetric.Decrypt;
14+
import top.viewv.model.symmetric.DecryptProgress;
1415
import top.viewv.model.symmetric.Encrypt;
1516
import top.viewv.model.symmetric.EncryptProgress;
1617
import top.viewv.model.tools.GenerateSecKey;
@@ -24,7 +25,7 @@
2425
import java.security.Security;
2526
import java.util.ResourceBundle;
2627

27-
public class MainController implements Initializable,Deliver {
28+
public class MainController implements Initializable, Deliver {
2829

2930
// UI part
3031
public AnchorPane basePane;
@@ -85,23 +86,35 @@ public class MainController implements Initializable,Deliver {
8586
private int numOfSpecial = 0;
8687
private int numOfUpper = 0;
8788

88-
private SecureRandom secureRandom = new SecureRandom();
89+
private final SecureRandom secureRandom = new SecureRandom();
8990

9091
private long sourceFileLength = 1;
9192

93+
private int workMode = 0;
94+
9295
@Override
93-
public void deliver(long process){
96+
public void deliver(long process) {
9497
pbarProcess.setProgress(process / sourceFileLength);
95-
int p = (int) (process/sourceFileLength) * 100;
98+
int p = (int) (process / sourceFileLength) * 100;
9699
labProcess.setText(p + "%");
100+
System.out.println(process);
97101
}
98102

99103
@Override
100-
public void reply(String message){
101-
if (message.equals("Error")){
104+
public void reply(String message) {
105+
if (message.equals("Error")) {
102106
labFinalAlert.setText("IO Error!");
103107
}
104-
if (message.equals("OK")){
108+
if (message.equals("IO Error")){
109+
labFinalAlert.setText("IO Error");
110+
}
111+
if (message.equals("Password Error")){
112+
labFinalAlert.setText("Password Error");
113+
}
114+
if (message.equals("Password Don't Match")){
115+
labFinalAlert.setText("Password Don't Match");
116+
}
117+
if (message.equals("OK")) {
105118
labFinalAlert.setText("All Done!");
106119

107120
btnStart.setDisable(false);
@@ -160,7 +173,26 @@ public void initialize(URL url, ResourceBundle rb) {
160173
Security.addProvider(new BouncyCastleProvider());
161174
}
162175

163-
public void onClickedBtnOpenFile(MouseEvent mouseEvent) {
176+
public void switchMode() {
177+
if (togModeChooser.isSelected()){
178+
179+
togModeChooser.setText("Decrypt");
180+
181+
workMode = 1;
182+
183+
setS2state(false);
184+
setS3state(true);
185+
186+
textPassword.setDisable(false);
187+
188+
btnStart.setDisable(true);
189+
}else {
190+
togModeChooser.setText("Encrypt");
191+
workMode = 0;
192+
}
193+
}
194+
195+
public void onClickedBtnOpenFile() {
164196
FileChooser fileChooser = new FileChooser();
165197

166198
File selectFile = fileChooser.showOpenDialog(null);
@@ -177,24 +209,26 @@ public void onClickedBtnOpenFile(MouseEvent mouseEvent) {
177209
labFileinfo.setText(filename);
178210
labFileAlert.setText(filepath);
179211

180-
setS2state(true);
212+
if (workMode == 0){
213+
setS2state(true);
181214

182-
// after choose right AEAD mode will use this part
183-
togAead.setVisible(false);
184-
textAEAD.setVisible(false);
215+
// after choose right AEAD mode will use this part
216+
togAead.setVisible(false);
217+
textAEAD.setVisible(false);
185218

186-
cboxMode.setDisable(true);
187-
cboxPadding.setDisable(true);
188-
cboxLength.setDisable(true);
219+
cboxMode.setDisable(true);
220+
cboxPadding.setDisable(true);
221+
cboxLength.setDisable(true);
189222

190-
// must select right algorithm
191-
GroupPassword.setDisable(true);
192-
textPassword.setDisable(true);
193-
sldPasswordLength.setDisable(true);
194-
chkUpper.setDisable(true);
195-
chkSpecial.setDisable(true);
223+
// must select right algorithm
224+
GroupPassword.setDisable(true);
225+
textPassword.setDisable(true);
226+
sldPasswordLength.setDisable(true);
227+
chkUpper.setDisable(true);
228+
chkSpecial.setDisable(true);
196229

197-
cboxAlgo.getSelectionModel().selectFirst();
230+
cboxAlgo.getSelectionModel().selectFirst();
231+
}
198232

199233
} else {
200234
labFileAlert.setText("Error Please Choose the right file");
@@ -221,7 +255,7 @@ public void selectAlgorithm() {
221255
cboxLength.setDisable(true);
222256
}
223257

224-
public void selectMode(MouseEvent mouseEvent) {
258+
public void selectMode() {
225259

226260
cboxMode.getItems().clear();
227261

@@ -301,7 +335,7 @@ public void selectPadding() {
301335
}
302336

303337

304-
public void selectLength(MouseEvent mouseEvent) {
338+
public void selectLength() {
305339

306340
cboxLength.getItems().clear();
307341

@@ -325,16 +359,18 @@ public void selectLength(MouseEvent mouseEvent) {
325359
}
326360

327361

328-
public void setPassword(MouseEvent mouseEvent) {
362+
public void setPassword() {
329363

330-
length = cboxLength.getValue();
331-
labAlgoLength.setText(length.toString());
364+
if (workMode == 0){
365+
length = cboxLength.getValue();
366+
labAlgoLength.setText(length.toString());
332367

333-
setS3state(true);
334-
btnStart.setDisable(true);
368+
setS3state(true);
369+
btnStart.setDisable(true);
370+
}
335371
}
336372

337-
public void changeLength(MouseEvent mouseEvent) {
373+
public void changeLength() {
338374
passwordLength = (int) sldPasswordLength.getValue();
339375

340376
if (chkSpecial.isSelected()) {
@@ -359,7 +395,7 @@ public void changeLength(MouseEvent mouseEvent) {
359395
textPassword.setText(randomPassword);
360396
}
361397

362-
public void changeAeadMode(MouseEvent mouseEvent) {
398+
public void changeAeadMode() {
363399
if (togAead.isSelected()) {
364400
textAEAD.setVisible(true);
365401
textAEAD.setDisable(false);
@@ -370,7 +406,7 @@ public void changeAeadMode(MouseEvent mouseEvent) {
370406
}
371407

372408

373-
public void onClickedChooseDir(MouseEvent mouseEvent) {
409+
public void onClickedChooseDir() {
374410
DirectoryChooser directoryChooser = new DirectoryChooser();
375411

376412
File selectPath = directoryChooser.showDialog(null);
@@ -384,60 +420,80 @@ public void onClickedChooseDir(MouseEvent mouseEvent) {
384420
}
385421
}
386422

387-
public void onClickStart(MouseEvent mouseEvent) {
423+
public void onClickStart() {
388424

389425
String password = textPassword.getText();
426+
390427
if (password == null) {
391428
labFinalAlert.setText("Password!");
392429
return;
393430
}
394431

395-
String associateData;
432+
if (workMode == 0) {
433+
String associateData;
396434

397-
if (algorithm.equals("ChaCha20") || mode.equals("GCM") || mode.equals("CCM")) {
398-
if (togAead.isSelected()) {
399-
associateData = textAEAD.getText();
400-
if (associateData == null) {
401-
labFinalAlert.setText("AEAD String");
402-
return;
435+
if (algorithm.equals("ChaCha20") || mode.equals("GCM") || mode.equals("CCM")) {
436+
if (togAead.isSelected()) {
437+
associateData = textAEAD.getText();
438+
if (associateData == null) {
439+
labFinalAlert.setText("AEAD String");
440+
return;
441+
}
403442
}
404443
}
405-
}
406444

407-
labFinalAlert.setText("");
445+
labFinalAlert.setText("");
408446

409-
Encrypt encrypt = new Encrypt();
410-
EncryptProgress encryptProgress = new EncryptProgress(MainController.this,encrypt);
411-
SecretKey secretKey = GenerateSecKey.generateKey(password, length, 65566,
412-
1, "AES");
447+
Encrypt encrypt = new Encrypt();
448+
EncryptProgress encryptProgress = new EncryptProgress(MainController.this, encrypt);
449+
SecretKey secretKey = GenerateSecKey.generateKey(password, length, 65566,
450+
1, "AES");
413451

414-
if (algorithm.equals("ChaCha20")) {
452+
String destfile = destFilepath + File.separator + sourceFilename.substring(0, sourceFilename.lastIndexOf(".")) + ".enc";
415453

416454
byte[] associatedBytes;
417455

418456
if (togAead.isSelected()) {
419457
associateData = textAEAD.getText();
420458
associatedBytes = associateData.getBytes();
421-
}else {
459+
} else {
422460
try {
423-
associatedBytes = SHA.digest(sourceFile,"3/512");
461+
associatedBytes = SHA.digest(sourceFile, "3/512");
424462
} catch (IOException e) {
425-
labFinalAlert.setText("Source File Broken!");
426-
return;
463+
labFinalAlert.setText("Source File Broken!");
464+
return;
427465
}
428466
}
429467

430-
Platform.runLater(() -> encryptProgress.doEncrypt(sourceFile,sourceFilename,destFilepath,
431-
"ChaCha20-Poly1305",secretKey,true,associatedBytes));
468+
boolean ifAEAD = false;
432469

433-
btnStart.setDisable(true);
434-
setS1state(false);
435-
setS2state(false);
436-
}
470+
if (algorithm.equals("ChaCha20") || mode.equals("GCM") || mode.equals("CCM")) {
471+
ifAEAD = true;
472+
}
473+
474+
if (algorithm.equals("ChaCha20")) {
475+
algorithm = "ChaCha20-Poly1305";
476+
} else if (algorithm.equals("AES")) {
477+
algorithm += "/" + mode;
478+
algorithm += "/" + padding;
479+
}
480+
481+
boolean finalIfAEAD = ifAEAD;
482+
Platform.runLater(() -> encryptProgress.doEncrypt(sourceFile, sourceFilename, destfile,
483+
algorithm, secretKey, finalIfAEAD, associatedBytes));
437484

438-
if (algorithm.equals("AES")){
485+
}else {
486+
Decrypt decrypt = new Decrypt();
487+
DecryptProgress decryptProgress = new DecryptProgress(MainController.this,decrypt);
488+
Platform.runLater(() -> decryptProgress.doDecrypt(sourceFile,destFilepath,password));
439489

440490
}
491+
btnStart.setDisable(true);
492+
493+
System.out.println(sourceFileLength);
494+
495+
setS1state(false);
496+
setS2state(false);
441497
}
442498
}
443499

src/main/java/top/viewv/model/symmetric/Decrypt.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
import javax.crypto.NoSuchPaddingException;
99
import javax.crypto.SecretKey;
1010
import javax.crypto.spec.IvParameterSpec;
11-
import java.io.FileInputStream;
12-
import java.io.FileOutputStream;
13-
import java.io.IOException;
14-
import java.io.OutputStream;
11+
import java.io.*;
1512
import java.nio.charset.StandardCharsets;
1613
import java.security.*;
1714

@@ -20,13 +17,11 @@ public class Decrypt {
2017
public static final int EOF = -1;
2118
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
2219

23-
public void decrypt(CallBack callBack, String sourcefile, String destfilepath, String password) {
20+
public void decrypt(CallBack callBack, String sourcefile, String destfilepath, String password) throws InvalidKeyException, IOException, NoSuchAlgorithmException, NoSuchProviderException,
21+
NoSuchPaddingException, InvalidAlgorithmParameterException {
2422
Security.addProvider(new BouncyCastleProvider());
2523

2624
try {
27-
28-
System.out.println("Staring Decrypt!");
29-
3025
FileInputStream body = new FileInputStream(sourcefile);
3126

3227
byte[] heads = new byte[1];
@@ -133,7 +128,7 @@ public void decrypt(CallBack callBack, String sourcefile, String destfilepath, S
133128

134129
String filename = new String(filenameBytes, StandardCharsets.UTF_8);
135130

136-
OutputStream out = new FileOutputStream(destfilepath + filename);
131+
OutputStream out = new FileOutputStream(destfilepath + File.separator + filename);
137132

138133
long count = 0;
139134
int n;
@@ -149,14 +144,11 @@ public void decrypt(CallBack callBack, String sourcefile, String destfilepath, S
149144
out.close();
150145
body.close();
151146

152-
System.out.println("Decryption Finish");
147+
callBack.report(-10);
153148

154-
} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException
155-
| InvalidAlgorithmParameterException e) {
156-
e.printStackTrace();
157-
} catch (IOException e) {
149+
}catch (NegativeArraySizeException e){
158150
e.printStackTrace();
159-
System.out.println("IOError!!!");
151+
throw new NegativeArraySizeException("Password Don't match");
160152
}
161153
}
162154
}

0 commit comments

Comments
 (0)