-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKryha.java
129 lines (112 loc) · 5.58 KB
/
Kryha.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
public class Kryha extends ClassicalCipher
{
// Conducts encryption and decryption per a Polyalphabetic process based
// on a Kryha Cipher machine.
// See Q.E.D. 2 Hours, 41 Minutes BY LAMBROS D. CALLIMAHOS
/*
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/>. */
public String keyP;
public String keyC;
private int place; // Place in sequence
private int [] adv;
// ----------------------------------------------------------------------
Kryha(String wordP,String wordC,int [] pins)
{
this(new Codespace(Codespace.StockAlphabet.CAPITALS),wordP,wordC,pins);
}
// ----------------------------------------------------------------------
Kryha(Codespace cs,String wordP,String wordC,int [] pins)
{
super(cs);
keyP=new Keyword().new Simple(cs,wordP).getKey();
keyC=new Keyword().new Simple(cs,wordC).getKey();
place=0;
adv=new int[pins.length];
for (int i=0;i<pins.length;i++) adv[i]=pins[i];
}
// --------------------------------------------------------------
public String toString()
{ return ("Kryha Cipher : "+nL+"Plain ="+keyP+nL+
"Cipher="+keyC+super.toString());}
// ----------------------------------------------------------------------
@Override
public String encode(String PT) {
String flat=cs.flattenToPT(PT);
StringBuilder sb=new StringBuilder(flat.length());
place=0;
for (int i=0;i<flat.length();i++) {
sb.append(keyC.charAt(
(keyP.indexOf(flat.charAt(i))+place)%cs.PTspace.length()));
place=(place+adv[i%adv.length])%cs.PTspace.length();
}
return sb.toString();
}
// ----------------------------------------------------------------------
@Override
public String decode(String CT) {
String flat=cs.flattenToCT(CT);
StringBuilder sb=new StringBuilder(flat.length());
place=0;
for (int i=0;i<flat.length();i++) {
sb.append(keyP.charAt(
(cs.PTspace.length()+keyC.indexOf(flat.charAt(i))-
place)%cs.PTspace.length()));
place=(place+adv[i%adv.length])%cs.PTspace.length();
}
return sb.toString();
}
// ----------------------------------------------------------------------
public static void main(String [] args)
{
// Test challenge to Friedman in 1933
String CT="XYICPNDEAMAPDTRAXXPZXHYRYTWQXFHCDJKAHQURZPPPZQOFUVKFEMNEAONGTT"+
"XSVVUBDGJREJFHEOKVCQHFHROKUPMQPQWACOJCRLMBMEVKRVJDYNNSXUDLHNPFWMOCMJFLGP"+
"MBKHAUXLIVVQSXUNJZUKKOBAAEUQOYJIZSZUHGWGWATEJWYDIVXPEIKEECMCIRXXLAZLAINM"+
"JZXICIDKQLMMTELLFJTJUBQOLJAWMFEHEVSYCASKFONOZUMPADAPJYLPFNTRUITCBWHJHMOL"+
"CVRDEPFQACIUHCZCBXTOKCIXGOSGCMRFHJVXSVZNMUGJJSOQBJQHBQNLHRTMELYNHKUFXJDM"+
"JYCPADPPWYMGUWOIAIIGPTSFCSOKIDGGTYOAAQDRQRRMNTSHYNEXYVFCMJJKNXVTEFXAUTSE"+
"ZQSHLULYCYGXONLAWQTEJNBSMVTEHSXUYNJKXFPEPGFCMMCWZRPJYGOPZUQNVXIAXZKQMJEF"+
"WWMRQRTETPXRSUKCDLHEDLLCTJKSZMQMKNJUVPFLYHYFQREWNDZMBMPBOJXEQIZAXHNDBQQW"+
"DIXQPIFAYJGQJOFWFCDBXYNXYTWYKEQCDPDYDOZHJFCZUEDDJBFXTTVFYGHCTBGOFEHBUBZD"+
"QQTIGDYAIYFDFHABSAHYGXIBBLECQOSEMOMZVKHQSJCMJFFEVVTLWTESLAYWFYCKOXPSVNAI"+
"GOCZZKVVVJSOPENYXDDXLDCYAXMWWOCWOIIBNXTVTLIVQWXUETPSUHCSOYTPVYIKZNFVIEYP"+
"HKINCGGVIKROOSOVMGHKUNUSUNYVCFELOOWSAIYRREVNEXPESEGRPZNBMMYUZFGSXRXWMNWT"+
"LRHVFHGSXMWVREAJDGOZAGRXKJLDOGYPTYXNTMWQMYSQWLXHNGZQDMCWPYATGNZFJKWGDKAV"+
"SJMHJGWJECWTDBZNMYTNAORVHARRPDXGCAPHJNZKTLQRQJJAFFZGDXLRFFSAWSZNGLSAQBMC"+
"DYJFMBLSXEOTLFJGGLGKKRYYWDALHHJVCGYVRLYSPJVPKGWWXHFACMTRGUJEJWTAFSNZXVVW"+
"IYWOOMTLUFSBCAJRNRMPIYLWIKAOKHTMXCNIMWTFGTTDEHTDHMKKCDKEAPHIAXZYP";
String PT="THECOURTISUNABLETHEREFORETOPERCEIVETHEPRESENCEINTHEINSTANTCASE"+
"OFANYCIRCUMSTANCEWHICHMIGHTDIFFERENTIATEITFROMTHECASESINVOLVINGTHEUSEOFT"+
"HERUSSIANSEALDECIDEDBYJUDGEANDREWSINTHISSITUATIONTHECOURTISUNWILLINGTORE"+
"ACHACONCLUSIONDIAMETRICALLYOPPOSEDTOTHATARRIVEDATBYTHATABLEJURISTAFTERTH"+
"ECAREFULANDPAINSTAKINGCONSIDERATIONOFTHELAWANDTHEFACTSWHICHHISLEARNEDOPI"+
"NIONINDICATESHEGAVETOTHEACTIONSTRIEDBEFOREHIMTHECOURTACCORDINGLYHOLDSTHA"+
"TTHEREISINSUFFICIENTPROOFINTHEPRESENTRECORDTOSUSTAINAFINDINGTHATTHERUSSI"+
"ANBUSINESSORTHATITSAGENTSWEREEMPOWEREDTOAFFIXITTOPOLICIESSOASTOGIVETHEMT"+
"HEFORCEANDEFFECTOFSEALEDINSTRUMENTSTHEFIRSTCAUSEOFACTIONISTHEREFOREBARRE"+
"DBYOURSIXYEARSTATUTEOFLIMITATIONSANDITISACCORDINGLYUNNECESSARYTOCONSIDER"+
"WHETHERTHERUSSIANSTATUTEOFLIMITATIONSOFTENYEARSCOMMENCEDTORUNUPONTHEACCR"+
"UALOFTHECAUSEOFACTIONASTHEDEFENDANTCONTENDSORUPONTHECOMMENCEMENTOFTHEACT"+
"IONASTHEPLAINTIFFARGUESITISLIKEWISEUNNECESSARYTODETERMINEWHETHERTHERUNNI"+
"NGOFTHISSTATUTEWASSUSPENDEDBECAUSEOFTHECLOSINGOFTHERUSSIANCOURTSTOCONTRO"+
"VERSIESARISINGOUTOFCIVILRELATIONSHIPORIGINATINGPRIORTOTHESOVIETREVOLUTIO"+
"NTHEMOTIONTODISMISSTHECOMPLAINTISGRANTEDASTOTHEFIRSTCAUSEOFACTION";
int [] pins={6,5,9,7,6,7,5,6,7,6,8,6,10,5,6,5,7};
Kryha kryha=new Kryha("PLMJNHGIBAKETCDQXSWVURFOZY","JNFGHEACBDWYXZVURSTQLOIMPK",pins);
// Keys from 1933 challenge
// Note that Plain key Q and Z can be reversed since do not appear in plaintext
//System.out.println(kryha);
if (kryha.knownTest(PT,CT)) System.out.println("PASS");
else System.out.println("******** FAIL ***********");
}
}