-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransposition
104 lines (89 loc) · 2.54 KB
/
transposition
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
/*
* Example input:
* attackpostponeduntiltwoam
* 4312567
*/
import java.util.*;
public class transpoCipher {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
//use to fill empty spots in last row
String alphaL = "abcdefghijklmnopqrstuvwxyz";
List<String> alpha = new ArrayList<String>();
alpha = Arrays.asList(alphaL);
//get message
System.out.println("Enter the message: ");
String str = reader.nextLine();
//str.replaceAll("\\s","");
//get key
ArrayList<Integer> key = new ArrayList<Integer>(7);
System.out.println("Enter the key: ");
char[] tempK = reader.next().toCharArray();
for(int t : tempK){
key.add(Character.getNumericValue(t));
}
//print out the key
System.out.print("the key is: ");
for(int x : key){
System.out.print(x + " ");
}
System.out.println();
//if not enough letters to fill last row, add in alphabet letters
int numRows = str.length() / 7;
if(str.length() % 7 > 0){
numRows++;
for(int i = str.length() % 7; i < 7; i++){
str += "x";
}
}
//create 2D array to store values from message into rows and columns
//then print it out
char [][] block = new char[numRows][7];
System.out.println("plaintext:");
int access = 0;
for(int c = 0; c < numRows; c++){
for(int r = 0; r < 7; r++){
block [c][r] = str.charAt(access);
System.out.print(block[c][r] + " ");
access++;
}
System.out.println();
}
//go through the 2D array of plaintext to output the columns by order
//of the key
System.out.print("ciphertext: ");
String ciphertext = "";
for(int i = 0; i < key.size(); i++){
int column = key.indexOf(i+1);
for(int cs = 0; cs < numRows; cs++){
for(int rs = 0; rs < 7; rs++){
//if at the right index, print out char
if(rs == column){
ciphertext += (block[cs][rs]);
}
}
}
}
System.out.println(ciphertext);
//now time to decipher: given the key and the ciphertext
int counter = 0;
String decipher = "";
ArrayList<ArrayList<String>> tempR = new ArrayList<ArrayList<String>>();
while(counter < ciphertext.length()){
ArrayList<String> temp = new ArrayList<String>();
for(int i = 0; i < 4; i++){
temp.add(Character.toString(ciphertext.charAt(counter)));
counter++;
}
tempR.add(temp);
}
for(int i = 0; i < key.size(); i++){
int patt = key.indexOf(i+1);
ArrayList<String> temp = tempR.get(i);
tempR.set(i, tempR.get(patt));
tempR.set(patt, temp);
decipher += tempR.get(i);
}
System.out.print("decryption: " + str);
}
}