Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javafx.application.Application;
- import javafx.stage.Stage;
- import javafx.scene.Group;
- import javafx.scene.Scene;
- import javafx.scene.paint.Color;
- import javafx.scene.text.Text;
- import javafx.scene.control.TextArea;
- import javafx.scene.control.PasswordField;
- import javafx.scene.layout.GridPane;
- import javafx.scene.control.Button;
- import javafx.geometry.Pos;
- import javafx.event.*;
- import javafx.scene.input.MouseEvent;
- import javafx.scene.image.Image;
- import javafx.scene.image.ImageView;
- import javafx.geometry.HPos;
- import javafx.scene.layout.StackPane;
- import javafx.geometry.Insets;
- import javafx.scene.text.Font;
- import javafx.scene.effect.DropShadow;
- import javafx.stage.FileChooser;
- import java.io.File;
- import java.util.Scanner;
- import java.io.FileWriter;
- public class Window extends Application {
- private boolean fileFlag = false;
- private String filePath;
- @Override
- public void start(Stage primaryStage) throws Exception {
- System.out.println(System.getProperty("user.dir"));
- //text labels
- Text t1 = new Text("Enter text to encrypt or decrypt: ");
- Text t2 = new Text("Enter the key to use for encryption or decrytion: ");
- Text t3 = new Text("Or select a file: ");
- //input and output text areas
- TextArea textInputArea = new TextArea();
- PasswordField keyInputField = new PasswordField();
- TextArea textOutputArea = new TextArea();
- //set text area properties
- textInputArea.setWrapText(true);
- textOutputArea.setWrapText(true);
- textOutputArea.setEditable(false);
- textInputArea.setPromptText("Enter text here");
- keyInputField.setPromptText("Enter cipher key here");
- textInputArea.setFocusTraversable(false);
- keyInputField.setFocusTraversable(false);
- textInputArea.setFont(Font.font("Verdana", 14));
- keyInputField.setFont(Font.font("Verdana", 14));
- textOutputArea.setFont(Font.font("Verdana", 14));
- //encrypt and decrypt buttons
- Button btnEncrypt = new Button("Encrypt");
- Button btnDecrypt = new Button("Decrypt");
- //button size
- btnEncrypt.setMinSize(100, 50);
- btnDecrypt.setMinSize(100, 50);
- //set style
- t1.setStyle("-fx-font: normal bold 15px 'Arial'");
- t2.setStyle("-fx-font: normal bold 15px 'Arial'");
- t3.setStyle("-fx-font: normal bold 15px 'Arial'");
- btnEncrypt.setStyle("-fx-background-color: rgb(98,131,149); -fx-text-fill: white;");
- btnDecrypt.setStyle("-fx-background-color: rgb(98,131,149); -fx-text-fill: white;");
- Button btnSlctFile = new Button("Select File");
- btnSlctFile.setStyle("-fx-background-color: rgb(98,131,149); -fx-text-fill: white;");
- FileChooser fileChooser = new FileChooser();
- fileChooser.getExtensionFilters().addAll(
- new FileChooser.ExtensionFilter("Text Files", "*.txt")
- );
- btnSlctFile.setOnMouseClicked(new EventHandler<MouseEvent>() {
- public void handle(MouseEvent event) {
- File selectedFile = fileChooser.showOpenDialog(primaryStage);
- if (selectedFile != null) {
- fileFlag = true;
- btnSlctFile.setText(selectedFile.getName());
- filePath = selectedFile.toString();
- textInputArea.clear();
- }
- }
- });
- btnEncrypt.setOnMouseClicked(new EventHandler<MouseEvent>() {
- public void handle(MouseEvent event) {
- if (fileFlag) {
- try {
- File file = new File(filePath);
- Scanner sc = new Scanner(file);
- sc.useDelimiter("\\Z");
- String text = sc.next();
- String key = keyInputField.getText();
- if (key.length() != 0 && text.length() != 0) {
- FileWriter fw = new FileWriter(filePath);
- fw.write(VigenereCipher.encrypt(text, key));
- fw.close();
- textOutputArea.setText("File Encrypted");
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- fileFlag = false;
- btnSlctFile.setText("Select File");
- } else {
- String text = textInputArea.getText();
- String key = keyInputField.getText();
- if (key.length() != 0 && text.length() != 0) {
- textOutputArea.setText(VigenereCipher.encrypt(text, key));
- }
- }
- }
- });
- btnDecrypt.setOnMouseClicked(new EventHandler<MouseEvent>() {
- public void handle(MouseEvent event) {
- if (fileFlag) {
- try {
- File file = new File(filePath);
- Scanner sc = new Scanner(file);
- sc.useDelimiter("\\Z");
- String text = sc.next();
- String key = keyInputField.getText();
- if (key.length() != 0 && text.length() != 0) {
- FileWriter fw = new FileWriter(filePath);
- fw.write(VigenereCipher.decrypt(text, key));
- fw.close();
- textOutputArea.setText("File Decrypted");
- }
- } catch (Exception e) {
- System.out.println(e);
- }
- fileFlag = false;
- btnSlctFile.setText("Select File");
- } else {
- String text = textInputArea.getText();
- String key = keyInputField.getText();
- if (key.length() != 0 && text.length() != 0) {
- textOutputArea.setText(VigenereCipher.decrypt(text, key));
- }
- }
- }
- });
- btnSlctFile.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- DropShadow shadow = new DropShadow();
- btnSlctFile.setEffect(shadow);
- }
- });
- btnSlctFile.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- btnSlctFile.setEffect(null);
- }
- });
- btnEncrypt.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- DropShadow shadow = new DropShadow();
- btnEncrypt.setEffect(shadow);
- }
- });
- btnEncrypt.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- btnEncrypt.setEffect(null);
- }
- });
- btnDecrypt.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- DropShadow shadow = new DropShadow();
- btnDecrypt.setEffect(shadow);
- }
- });
- btnDecrypt.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent>() {
- @Override
- public void handle(MouseEvent e) {
- btnDecrypt.setEffect(null);
- }
- });
- GridPane gp = new GridPane();
- gp.setAlignment(Pos.CENTER);
- gp.add(btnEncrypt, 0, 0);
- gp.add(btnDecrypt, 1, 0);
- gp.setHgap(30);
- GridPane gpFile = new GridPane();
- //gpFile.setAlignment(Pos.CENTER);
- gpFile.add(t3, 0, 0);
- gpFile.add(btnSlctFile, 1, 0);
- gp.setHgap(15);
- Image image = new Image("/images/logo.jpg");
- ImageView imageView = new ImageView(image);
- imageView.setPreserveRatio(true);
- imageView.setFitWidth(175);
- StackPane topPane = new StackPane();
- topPane.getChildren().add(imageView);
- topPane.setStyle("-fx-background-color: rgb(98,131,149);");
- GridPane gridPane = new GridPane();
- //gridPane.setMinSize(500, 650);
- gridPane.setVgap(10);
- gridPane.setHgap(20);
- gridPane.setAlignment(Pos.CENTER);
- gridPane.setStyle("-fx-background-color: rgb(174, 194, 209);");
- //posistion elements
- gridPane.add(topPane, 0, 0);
- GridPane.setFillHeight(topPane, true);
- GridPane.setFillWidth(topPane, true);
- gridPane.add(t1, 0, 1);
- gridPane.add(textInputArea, 0, 2);
- gridPane.add(gpFile, 0, 3);
- gridPane.add(t2, 0, 4);
- gridPane.add(keyInputField, 0, 5);
- gridPane.add(gp, 0, 6);
- gridPane.add(textOutputArea, 0, 7);
- gridPane.setHalignment(imageView, HPos.CENTER);
- gridPane.setMargin(t1, new Insets(5, 10, 0, 10));
- gridPane.setMargin(textInputArea, new Insets(0, 10, 0, 10));
- gridPane.setMargin(t2, new Insets(5, 10, 0, 10));
- gridPane.setMargin(keyInputField, new Insets(0, 10, 0, 10));
- gridPane.setMargin(textOutputArea, new Insets(5, 10, 10, 10));
- gridPane.setMargin(t3, new Insets(5, 10, 0, 10));
- Scene scene = new Scene(gridPane);
- primaryStage.setTitle("Vigenere Cipher");
- primaryStage.setScene(scene);
- primaryStage.setResizable(false);
- primaryStage.show();
- }
- }
- class VigenereCipher {
- private static String vigenere(String text, String key, boolean encrypt) {
- int iText = 0, iKey = 0;
- key = genKey(text, key).toUpperCase();
- String resulttext = "";
- while (iText < text.length()) {
- char c = Character.toUpperCase(text.charAt(iText));
- if (c >= 'A' && c <= 'Z') {
- if (encrypt) c = (char) ((c + key.charAt(iKey)) % 26 + 'A');
- else c = (char) (mod(c - key.charAt(iKey), 26) + 'A');
- if (Character.isUpperCase(text.charAt(iText))) {
- resulttext += c;
- } else {
- resulttext += Character.toLowerCase(c);
- }
- iKey++;
- } else {
- resulttext += c;
- }
- iText++;
- }
- return resulttext;
- }
- public static String encrypt(String plaintext, String key) {
- return vigenere(plaintext, key, true);
- }
- public static String decrypt(String ciphertext, String key) {
- return vigenere(ciphertext, key, false);
- }
- private static String genKey(String text, String key) {
- if (key.length() >= text.length()) return key;
- int index = 0;
- while (key.length() < text.length()) {
- if (index == key.length()) index = 0;
- key += key.charAt(index);
- index++;
- }
- return key;
- }
- private static int mod(int x, int y) {
- int result = x % y;
- if (result < 0) result += y;
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement