-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRowColumn.java
More file actions
107 lines (94 loc) · 3.22 KB
/
RowColumn.java
File metadata and controls
107 lines (94 loc) · 3.22 KB
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
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author swa
*/
public class RowColumn {
public static char e_matrix[][];
public static char d_matrix[][];
public static String key_order = "";
public static String sortString(String inputString)
{
// convert input string to char array
char tempArray[] = inputString.toCharArray();
// sort tempArray
Arrays.sort(tempArray);
String key = new String(tempArray);
String temp = "";
for(int i=0; i<key.length(); i++){
temp += String.valueOf(i);
}
for(int i=0; i<key.length(); i++){
key_order += temp.charAt(inputString.indexOf(key.charAt(i)));
}
// return new sorted string
return key;
}
public static String encrypt(String message, String key, int rows, int cols){
int x=0;
String sorted_key = sortString(key);
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
if(i==0)
e_matrix[i][j] = key.charAt(j);
else{
e_matrix[i][j] = message.charAt(x);
x++;
}
System.out.print(e_matrix[i][j] + " ");
}
System.out.println();
}
String encrypted = "";
for(int y=0; y<key.length(); y++){
char val = sorted_key.charAt(y);
int col=0;
for(int i=0; i<cols; i++){
if(e_matrix[0][i] == val)
col = i;
}
for(int j=1; j<rows; j++){
encrypted += e_matrix[j][col];
}
}
return encrypted;
}
public static String decrypt(String message, String key, int rows, int cols){
String decrypted = "";
int x=0;
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
d_matrix[i][j] = message.charAt(x);
x++;
System.out.print(d_matrix[i][j] + " ");
}
System.out.println();
}
for(int i=0; i<cols; i++){
for(int y=0; y<key.length();y++){
decrypted += d_matrix[Integer.parseInt(String.valueOf(key_order.charAt(y)))][i];
}
}
return decrypted;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the plaintext: ");
String message = sc.nextLine();
System.out.println("Enter the key(digits): ");
String key = sc.next();
int cols = key.length();
if(message.length()%cols!=0){
while(message.length()%cols!=0)
message += " ";
}
int rows = message.length()/cols;
e_matrix = new char[rows+1][cols];
String encrypted = encrypt(message,key,rows+1,cols);
System.out.println("The encrypted string is : " + encrypted);
d_matrix = new char[cols][rows];
String decrypted = decrypt(encrypted, key, cols, rows);
System.out.println("The decrypted string is: " + decrypted);
}
}