-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourSquare.java
More file actions
96 lines (79 loc) · 3.12 KB
/
Copy pathFourSquare.java
File metadata and controls
96 lines (79 loc) · 3.12 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
public class FourSquare extends ClassicalCipher
{
// Conducts FourSquare encryption and decryption.
protected String keyTR;
protected String keyBL;
/*
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/>. */
// ----------------------------------------------------------------------
FourSquare(String wordTR,String wordBL)
{ this(new Codespace(Codespace.StockAlphabet.MERGED_IJ),wordTR,wordBL); }
// ----------------------------------------------------------------------
FourSquare(Codespace cs,String wordTR,String wordBL)
{
super(cs);
if (cs.PTspace.length() != 25)
throw new IllegalArgumentException("FourSquare needs a 25 letter alphabet");
keyTR=new Keyword().new Simple(cs,wordTR).getKey();
keyBL=new Keyword().new Simple(cs,wordBL).getKey();
}
// ----------------------------------------------------------------------
public String toString()
{ return (this.getClass().getName()+" Cipher : TR Key "+keyTR+" BL Key "+keyBL+nL+
super.toString());}
// ----------------------------------------------------------------------
@Override
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=cs.PTspace.indexOf(flat.charAt(i));
int l2=cs.PTspace.indexOf(flat.charAt(i+1));
int x1=l1%5;
int y1=l1/5;
int x2=l2%5;
int y2=l2/5;
sb.append(keyTR.charAt(y1*5+x2));
sb.append(keyBL.charAt(y2*5+x1));
}
return sb.toString();
}
// ----------------------------------------------------------------------
@Override
public String decode(String CT)
{
String flat=cs.flattenToCT(CT);
StringBuilder sb=new StringBuilder();
for (int i=0;i<flat.length();i+=2) {
int l1=keyTR.indexOf(flat.charAt(i));
int l2=keyBL.indexOf(flat.charAt(i+1));
int x1=l1%5;
int y1=l1/5;
int x2=l2%5;
int y2=l2/5;
sb.append(cs.PTspace.charAt(y1*5+x2));
sb.append(cs.PTspace.charAt(y2*5+x1));
}
return sb.toString();
}
// ----------------------------------------------------------
public static void main(String [] args) {
// http://en.wikipedia.org/w/index.php?title=Four-square_cipher&oldid=565762600
FourSquare foursquare=new FourSquare(new Codespace(Codespace.StockAlphabet.NO_Q),"EXAMPLE","KEYWORD");
String target="FYGMKYHOBXMFKKKIMD";
String PT="HELPMEOBIWANKENOBI";
if (foursquare.knownTest(PT,target)) System.out.println("PASS");
else System.out.println("***** FAIL *******");
}
}