package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class Controller {
Crypto crypto = new Crypto();
private String username = "user19i2h3";
private String password = "A9m3n442";
@FXML
private Label lblCrypto;
@FXML
private TextField txtUserName;
@FXML
private TextField txtPassword;
@FXML
private TextField txtFilePath;
@FXML
private Label label1;
@FXML
private TextArea txtArea;
public void login(ActionEvent event) {
try {
if (txtUserName.getText().equals(username) && txtPassword.getText().equals(password)) {
lblCrypto.setText("Success!");
Stage primaryStage = new Stage();
Parent root = FXMLLoader.load(getClass().getResource("window.fxml"));
Scene scene = new Scene(root, 600, 600);
primaryStage.setScene(scene);
primaryStage.setTitle("CRYPTO");
primaryStage.show();
((Node) (event.getSource())).getScene().getWindow().hide();
} else {
lblCrypto.setText("Try again!");
}
} catch (Exception e) {
System.out.println(e);
}
}
public void encryption(ActionEvent event) {
try {
txtArea.clear();
String text = fileLoading();
int textLength = text.length();
crypto.encryption(textLength, text);
String encryptedText = crypto.sb.toString();
fileRewriting(encryptedText);
txtArea.appendText(encryptedText);
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Status...");
alert.setHeaderText("Results:");
alert.setContentText("Encrypted!");
alert.showAndWait();
} catch (Exception e) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Wrong file path!");
alert.setHeaderText("Results:");
alert.setContentText("Wrong file path!");
alert.showAndWait();
}
}
public void decryption(ActionEvent event) {
try {
txtArea.clear();
String text1 = fileLoading();
int textLength1 = text1.length();
crypto.decryption(text1, textLength1);
String decryptedText = crypto.sb2.toString();
fileRewriting(decryptedText);
txtArea.appendText(decryptedText);
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Status");
alert.setHeaderText("Results:");
alert.setContentText("Decrypted!");
alert.showAndWait();
} catch (Exception e) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Wrong file path!");
alert.setHeaderText("Results:");
alert.setContentText("Wrong file path!");
alert.showAndWait();
}
}
public void fileChooser(ActionEvent event) {
FileChooser fc = new FileChooser();
FileChooser.ExtensionFilter fileExtensions =
new FileChooser.ExtensionFilter("Text files", "*.txt");
fc.getExtensionFilters().add(fileExtensions);
File file=fc.showOpenDialog(null);
String str=file.toString();
String realFilePath = str.replaceAll("\\\\","/");
System.out.println(realFilePath);
; if(file != null){
label1.setText(realFilePath);
}
}
public String fileLoading() {
try {
FileReader fileReader = new FileReader(label1.getText());
BufferedReader reader = new BufferedReader(fileReader);
String text = "";
String line = reader.readLine();
while (line != null) {
text += line;
line = reader.readLine();
}
return text;
} catch (IOException e) {
System.out.println("File not found!");
}
return null;
}
public void fileRewriting(String text2) {
String strPath =label1.getText() ;
Path pathOfNewFile = Paths.get(strPath);
try {
BufferedWriter bufWriter = Files.newBufferedWriter(pathOfNewFile, StandardCharsets.UTF_8, StandardOpenOption.CREATE);
bufWriter.write(text2);
bufWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
方法加密和解密无法正常工作。它们在Intellij Idea中可以正常工作,但是当我制作jar文件时,它们会在文本文件中添加一些额外的符号。例如,我有一个内容文本文件:今天是美好的一天。加密后:ùÅÉ^ ynk * s} k qyyn *nkùÅÉ^ ynk * s} k qyyn * nk。 解密后:¹¹¹¹¹Toda¸ƈ是一个很好的da¸ƈ¹¹¹“¹...Toda¸ƈ是一个很好的da¸ƈ¹¹¹”¹...Toda¸ƈ是一个很好的da¸ƈ
答案 0 :(得分:0)
如果需要将加密的数据放在字符串中,则应该对加密的数据进行编码 具有Base 64的字节数组。否则,您的数据可能会损坏:
byte[] encryptedByteArray;
String encodedData = new String(Base64.getEncoder().encode(encryptedByteArray);
并进行解码:
byte[] decodedBytes = Base64.getDecoder().decode(encodedData.getBytes());