-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINS_P9.java
More file actions
51 lines (42 loc) · 1.07 KB
/
Copy pathINS_P9.java
File metadata and controls
51 lines (42 loc) · 1.07 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
package ins;
/**
*
* @author Raj Dhanani
*/
class Railfence {
String plainText, cipherText;
int key, currentPointer = 0, v = -1;
StringBuffer temp[];
Railfence(int key, String plainText) {
this.plainText = plainText;
this.key = key;
init();
}
void init() {
temp = new StringBuffer[key];
for (int i = 0; i < key; i++) {
temp[i] = new StringBuffer();
}
}
void encrypt() {
cipherText = "";
System.out.println(plainText);
for (char c : plainText.toCharArray()) {
temp[currentPointer].append(c);
if (currentPointer == 0 || currentPointer == key - 1) {
v *= (-1);
}
currentPointer += v;
}
for (int i = 0; i < key; i++) {
cipherText += temp[i].toString();
}
}
}
public class INS_P9 {
public static void main(String[] args) {
Railfence r = new Railfence(4, "helloworld");
r.encrypt();
System.out.println("ENC:" + r.cipherText);
}
}