-
Notifications
You must be signed in to change notification settings - Fork 0
/
Crypto.java
156 lines (114 loc) · 3.55 KB
/
Crypto.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
package Cryptography;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Crypto {
public static void main(String arg[]) throws Exception
{
Run();
}
public static void Run() throws Exception
{
Scanner kb=new Scanner(System.in);
System.out.println("\n***** RailFenceCipher *****");
System.out.println();
// String clearText=JOptionPane.showInputDialog(null,"Enter message to encrypt:");
String clearText=OpenFile.pickFile();
System.out.println("Original Text: \n\n" + clearText);
System.out.println();
int key=Integer.parseInt(JOptionPane.showInputDialog(null,"Enter Key value: "));
System.out.println("Key value: "+key);
String cipherText = Cipher(clearText, key);
JOptionPane.showMessageDialog(null,"Encrypted Data "+"\n"+cipherText);
System.out.println("\nEncrypted message: \n\n"+cipherText);
String decipherText = Decipher(cipherText, key);
JOptionPane.showMessageDialog(null,"Decrypted Data "+"\n"+decipherText);
System.out.println("\nDecrypted message: \n\n"+decipherText);
}
private static char[][] buildCleanMatrix(int rows, int cols) {
char[][] result = new char[rows][];
for (int row = 0; row < result.length; row++) {
result[row] = new char[cols];
}
return result;
}
private static String buildStringFromMatrix(char[][] matrix)
{
String result = "";
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[row].length; col++)
{
if (matrix[row][col] != '\0')
{
result += matrix[row][col];
}
}
}
return result;
}
private static char[][] transpose(char[][] matrix)
{
char[][] result =
buildCleanMatrix(matrix[0].length, matrix.length);
for (int row = 0; row < matrix.length; row++)
{
for (int col = 0; col < matrix[row].length; col++)
{
result[col][row] = matrix[row][col];
}
}
return result;
}
private static String Cipher(String clearText, int key) {
String result = "";
char[][] matrix = buildCleanMatrix(key, clearText.length());
int rowIncrement = 1;
for (int row = 0, col = 0; col < matrix[row].length; col++) {
if (
row + rowIncrement == matrix.length ||
row + rowIncrement == -1
)
{
rowIncrement *= -1;
}
matrix[row][col] = clearText.charAt(col);
row += rowIncrement;
}
result = buildStringFromMatrix(matrix);
return result;
}
private static String Decipher(String cipherText, int key) {
String result = "";
char[][] matrix = buildCleanMatrix(key, cipherText.length());
int rowIncrement = 1;
int textIdx = 0;
for (
int selectedRow = 0;
selectedRow < matrix.length;
selectedRow++
)
{
for (
int row = 0, col = 0;
col < matrix[row].length;
col++
)
{
if (
row + rowIncrement == matrix.length ||
row + rowIncrement == -1
)
{
rowIncrement *= -1;
}
if (row == selectedRow) {
matrix[row][col] = cipherText.charAt(textIdx++);
}
row += rowIncrement;
}
}
matrix = transpose(matrix);
result = buildStringFromMatrix(matrix);
return result;
}
}