55import javafx .fxml .Initializable ;
66import javafx .scene .Group ;
77import javafx .scene .control .Label ;
8- import javafx .scene .input .MouseEvent ;
98import javafx .scene .layout .AnchorPane ;
109import javafx .stage .DirectoryChooser ;
1110import javafx .stage .FileChooser ;
1211import org .bouncycastle .jce .provider .BouncyCastleProvider ;
1312import top .viewv .model .mac .SHA ;
13+ import top .viewv .model .symmetric .Decrypt ;
14+ import top .viewv .model .symmetric .DecryptProgress ;
1415import top .viewv .model .symmetric .Encrypt ;
1516import top .viewv .model .symmetric .EncryptProgress ;
1617import top .viewv .model .tools .GenerateSecKey ;
2425import java .security .Security ;
2526import 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
0 commit comments