-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCaeserCipher.java
162 lines (121 loc) · 5.54 KB
/
CaeserCipher.java
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
162
/*
Carmine Attanasio
Caser Cipher GUI
*/
package caeser.cipher;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class CaeserCipher extends Application {
//declaration of controls
TextField tfPlainTxt = new TextField();
TextField tfShift = new TextField();
//button to encrypt text
private Button btnEncrypt;
//Labels for app
private Label lblHeader;
private Label lblPlaintxt;
private Label lblencryptedtxt;
private Label lblencdesc;
private Label lblDescription;
Label lblShift;
private Label lblShiftVal;
private Label lblShiftValDesc;
//vbox for layout of items
private VBox vBoxInput;
private VBox vBoxOutput;
//creates JavaFX
@Override
public void start(Stage primaryStage) {
//header to describe function of program to user
lblHeader = new Label("Encrypt your text using the caeser cipher");
lblHeader.setMinWidth(1000);
lblHeader.setAlignment(Pos.CENTER);
lblHeader.setStyle("-fx-font-family: 'Comic Sans MS'; -fx-font-size: 28px; -fx-text-fill: white; -fx-background-color: slateblue; -fx-font-weight: bold;");
//initiates label for shift key
lblShift = new Label("Enter a integer for the key of the cipher");
lblShift.setMinWidth(25);
lblShift.setAlignment(Pos.CENTER);
lblShift.setStyle("-fx-font-family: 'Comic Sans MS'; -fx-font-size: 20px; -fx-text-fill: black; -fx-background-color:#FFF8DC); -fx-font-weight: bold;");
//initiates label to display shift key value
String key = tfShift.getText();
lblShiftVal = new Label(key);
lblShiftVal.setMinWidth(25);
lblShiftVal.setStyle("-fx-font-family: 'Comic Sans MS'; -fx-font-size: 20px; -fx-text-fill: black; -fx-background-color:#FFF8DC); -fx-font-weight: bold;");
//initiates label to display shift key value label
lblShiftValDesc = new Label("Shift Value: ");
lblShiftValDesc.setMinWidth(25);
lblShiftValDesc.setStyle("-fx-font-family: 'Comic Sans MS'; -fx-font-size: 20px; -fx-text-fill: black; -fx-background-color:#FFF8DC); -fx-font-weight: bold;");
//initiates label for encrypted text
lblencdesc = new Label("Here is your encrypted text:");
lblencdesc.setMinWidth(25);
lblencdesc.setStyle("-fx-font-family: 'Comic Sans MS'; -fx-font-size: 20px; -fx-text-fill: black; -fx-background-color:#FFF8DC); -fx-font-weight: bold;");
lblencryptedtxt = new Label(" ");
lblencryptedtxt.setMinWidth(100);
//initiates labels
lblPlaintxt= new Label ("Enter plain text to be encrypted");
lblPlaintxt.setStyle("-fx-font-family: 'Comic Sans MS'; -fx-font-size: 20px; -fx-text-fill: black; -fx-background-color:#FFF8DC); -fx-font-weight: bold;");
lblDescription = new Label("Plain text is encrypted using caser cipher and shift key input");
//creates encrypt button
btnEncrypt = new Button("Encrypt Text");
btnEncrypt.setStyle("-fx-text-fill: white; -fx-background-color: tan; -fx-font-weight: bold;");
btnEncrypt.setOnAction( e -> encryptText(e) );
//sizing for text fields
tfPlainTxt.setPrefWidth(300);
tfShift.setPrefWidth(25);
//makes vbox for input
vBoxInput = new VBox(30,lblPlaintxt, tfPlainTxt, lblShift ,tfShift, btnEncrypt);
vBoxInput.setPrefWidth(450);
//create vbox for results
vBoxOutput = new VBox(50,lblDescription,lblShiftValDesc, lblShiftVal,lblencdesc,lblencryptedtxt);
vBoxOutput.setPrefWidth(400);
vBoxOutput.setPrefHeight(500);
vBoxOutput.setPadding( new Insets(10) );
vBoxOutput.setStyle("-fx-background-color: lightblue; -fx-border-color: black; -fx-border-radius: 10;");
GridPane grid = new GridPane();
grid.addRow(0, lblHeader);
GridPane.setColumnSpan(lblHeader, 2);
GridPane.setHalignment(lblHeader, HPos.CENTER);
grid.addRow(1, vBoxInput, vBoxOutput);
Scene scene = new Scene(grid, 1000, 500);
primaryStage.setTitle("Caeser Cipher");
primaryStage.setResizable(false);
primaryStage.setScene(scene);
primaryStage.show();
}
/*
event handler that uses user input to encrypt text
uses user input of plain text and shift key to output encrypted text
*/
public void encryptText(ActionEvent e)
{
String plainText=tfPlainTxt.getText();
String key=tfShift.getText();
String encText="";
int keyInt =Integer.parseInt(key);
char conv ='a';
lblShiftVal.setText(key);
char[] inputConv = plainText.toCharArray(); //converts input string to char array
for(char c : inputConv) //c is key for shifting
{
c += keyInt;
conv=c;
encText+=c;
}
lblencryptedtxt.setText(encText);
}
public static void main(String[] args) {
launch(args);
}
}