forked from DHEERAJHARODE/Hacktoberfest2024-Open-source-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoloumnar_cipher
71 lines (69 loc) · 1.96 KB
/
coloumnar_cipher
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
import java.io. * ;
import java.util. * ;
public class columnarcipher {
// more information on
//http://practicalcryptography.com/ciphers/columnar-transposition-cipher/
//Decrypt operation
public static void decrypt(String s, String key) {
int len = key.length();
int len2 = s.length();
String regex = "(?<=\\G.{" + Integer.toString(len2 / len) + "})";
String arr[] = s.split(regex);
HashMap < Character,
String > hm = new HashMap < Character,
String > ();
char ch[] = key.toCharArray();
Arrays.sort(ch);
String key2 = new String(ch);
String res = "";
for (int i = 0; i < len; i++) {
hm.put(key2.charAt(i), arr[i]);
}
for (int j = 0; j < len2 / len; j++) {
for (int i = 0; i < len; i++) {
res += hm.get(key.charAt(i)).charAt(j);
}
}
System.out.println(res);
}
//Encrypt operation on string
static public String encrypt(String s, String key) {
int len = key.length();
int len2 = s.length();
int rem = len2 % len;
String str = "";
HashMap < Character,
String > hm = new HashMap < Character,
String > ();
char ch[] = key.toCharArray();
Arrays.sort(ch);
String key2 = new String(ch);
int start = 0,
curr = 0;
for (int i = 0; i < len; i++) {
curr = start;
str = "";
while (curr < len2) {
str += s.charAt(curr);
curr += len;
}
start++;
if (i >= rem) str += " ";
hm.put(key.charAt(i), str);
}
String res = "";
for (int i = 0; i < len; i++)
res += (hm.get(key2.charAt(i)));
return res;
}
//whitespace added as padding
//runner class
public static void main(String args[]) {
InputStreamReader read = new InputStreamReader(System. in );
BufferedReader in =new BufferedReader(read);
String enc = encrypt("ParshwaShah", "CONSULT");
System.out.println("Encrypted String: " + enc);
System.out.print("Decrypted String: ");
decrypt(enc, "CONSULT");
}
}