-
Notifications
You must be signed in to change notification settings - Fork 0
/
VerticalTwoSquare.java
109 lines (90 loc) · 3.4 KB
/
VerticalTwoSquare.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
public class VerticalTwoSquare extends ClassicalCipher
{
// Conducts TwoSquare encryption and decryption.
protected String keyT;
protected String keyB;
/*
Copyright (C) 2019 S Combes
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
// ----------------------------------------------------------------------
VerticalTwoSquare(String wordT,String wordB)
{ this(new Codespace(Codespace.StockAlphabet.MERGED_IJ),wordT,wordB); }
// ----------------------------------------------------------------------
VerticalTwoSquare(Codespace cs,String wordT,String wordB)
{
super(cs);
if (cs.PTspace.length() != 25)
throw new IllegalArgumentException("TwoSquare needs a 25 letter alphabet");
keyT=new Keyword().new Simple(cs,wordT).getKey();
keyB=new Keyword().new Simple(cs,wordB).getKey();
}
// ----------------------------------------------------------------------
public String toString()
{ return ("Vertical Two Square Cipher : T Key "+keyT+" B Key "+keyB+nL+
super.toString());}
// ----------------------------------------------------------------------
public String encode(String PT)
{
String flat=(PT.length()%2==0)?cs.flattenToPT(PT):cs.flattenToPT(PT+"X");
StringBuilder sb=new StringBuilder();
for (int i=0;i<flat.length();i+=2) {
int l1=keyT.indexOf(flat.charAt(i));
int l2=keyB.indexOf(flat.charAt(i+1));
int x1=l1%5;
int y1=l1/5;
int x2=l2%5;
int y2=l2/5;
if (x1==x2) { // Same column - transparency
sb.append(flat.charAt(i));
sb.append(flat.charAt(i+1));
} else {
sb.append(keyT.charAt(y1*5+x2));
sb.append(keyB.charAt(y2*5+x1));
}
}
return sb.toString();
}
// ----------------------------------------------------------------------
public String decode(String CT)
{
String flat=cs.flattenToCT(CT);
StringBuilder sb=new StringBuilder();
for (int i=0;i<flat.length()-1;i+=2) {
int l1=keyT.indexOf(flat.charAt(i));
int l2=keyB.indexOf(flat.charAt(i+1));
int x1=l1%5;
int y1=l1/5;
int x2=l2%5;
int y2=l2/5;
if (x1==x2) { // Same column - transparency
sb.append(flat.charAt(i));
sb.append(flat.charAt(i+1));
} else {
sb.append(keyT.charAt(y1*5+x2));
sb.append(keyB.charAt(y2*5+x1));
}
}
return sb.toString();
}
// ----------------------------------------------------------
public static void main(String [] args) {
// http://en.wikipedia.org/w/index.php?title=Two-square_cipher&oldid=562298993
VerticalTwoSquare twosquare=
new VerticalTwoSquare(new Codespace(Codespace.StockAlphabet.NO_Q),"EXAMPLE","KEYWORD");
String target="HEDLXWSDJYANHOTKDG";
String PT="HELPMEOBIWANKENOBI";
//System.out.println(twosquare);
//System.out.println();
if (twosquare.knownTest(PT,target)) System.out.println("PASS");
else System.out.println("***** FAIL *******");
}
}