Skip to content

Commit 75cf1d5

Browse files
committed
playerfair added
1 parent ce6f492 commit 75cf1d5

18 files changed

+94
-6
lines changed

β€ŽSecurityLibrary/MainAlgorithms/PlayFair.cs

+81-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
ο»Ώusing System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Text;
56
using System.Threading.Tasks;
@@ -68,20 +69,94 @@ public KOMatrices KOFunc(HashSet<char> Mkey)
6869
return komatrix;
6970
}
7071

72+
public List<string> divideIt(string x)
73+
{
74+
List<string> largeString = new List<string>();
75+
int chunk = 100;
76+
int xLength = x.Length;
77+
for (int i = 0; i < xLength; i += chunk)
78+
{
79+
if (i + chunk > xLength) chunk = xLength - i;
80+
largeString.Add(x.Substring(i, chunk));
81+
82+
}
83+
84+
return largeString;
85+
}
86+
7187
public string Decrypt(string cipherText, string key)
7288
{
73-
HashSet<char> Mkey = ModifiedKey(key);
74-
string CT = "";
75-
return "";
89+
cipherText = cipherText.ToLower();
90+
List<string> smallSegs = new List<string>();
91+
bool flag = false;
92+
if (cipherText.Length > 100)
93+
{
94+
smallSegs = divideIt(cipherText);
95+
flag = true;
96+
}
97+
98+
KOMatrices matrix = KOFunc(ModifiedKey(key));
99+
string FPT = "";
100+
for (int j = 0; j < smallSegs.Count || !flag ; j++)
101+
{
102+
if (flag)
103+
{
104+
cipherText = smallSegs[j];
105+
}
106+
int CTLength = cipherText.Length;
107+
string PT = "";
108+
flag = true;
109+
for (int i = 0; i < CTLength; i += 2)
110+
{
111+
char c1 = cipherText[i], c2 = cipherText[i + 1];
112+
if (matrix.KM[c1].Item2 == matrix.KM[c2].Item2)
113+
{
114+
PT += matrix.OM[(matrix.KM[c1].Item1 + 4) % 5][matrix.KM[c1].Item2];
115+
PT += matrix.OM[(matrix.KM[c2].Item1 + 4) % 5][matrix.KM[c2].Item2];
116+
}
117+
else if (matrix.KM[c1].Item1 == matrix.KM[c2].Item1)
118+
{
119+
PT += matrix.OM[matrix.KM[c1].Item1][(matrix.KM[c1].Item2 + 4) % 5];
120+
PT += matrix.OM[matrix.KM[c2].Item1][(matrix.KM[c2].Item2 + 4) % 5];
121+
}
122+
else
123+
{
124+
PT += matrix.OM[matrix.KM[c1].Item1][matrix.KM[c2].Item2];
125+
PT += matrix.OM[matrix.KM[c2].Item1][matrix.KM[c1].Item2];
126+
}
127+
}
128+
129+
if (PT[PT.Length - 1] == 'x')
130+
{
131+
PT = PT.Remove(PT.Length - 1);
132+
}
133+
134+
for (int i = 0; i < PT.Length; i++)
135+
{
136+
if (PT[i] == 'x')
137+
{
138+
if (PT[i - 1] == PT[i + 1])
139+
{
140+
if (i+1<PT.Length && (i+1)%2==0)
141+
{
142+
PT = PT.Remove(i, 1);
143+
}
144+
}
145+
}
146+
}
147+
148+
FPT += PT;
149+
}
150+
151+
Console.WriteLine(FPT);
152+
return FPT;
76153
}
77154

78155

79156
public string Encrypt(string plainText, string key)
80157
{
81158
string CT = "";
82-
83-
HashSet<char> Mkey = ModifiedKey(key);
84-
KOMatrices KOkey = KOFunc(Mkey);
159+
KOMatrices KOkey = KOFunc(ModifiedKey(key));
85160
for (int i = 0; i < plainText.Length - 1; i+=2)
86161
{
87162
if (plainText[i] == plainText[i + 1])
Binary file not shown.
Binary file not shown.
Binary file not shown.

β€ŽSecurityLibrary/obj/Debug/SecurityLibrary.csproj.FileListAbsolute.txt

+5
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@ E:\FCIS\Year 4\Term2\Computer Security Methods\Computer Security algorithms\Secu
88
E:\FCIS\Year 4\Term2\Computer Security Methods\Computer Security algorithms\SecurityLibrary\obj\Debug\SecurityLibrary.pdb
99
E:\FCIS\Year 4\Term2\Computer Security Methods\Computer Security algorithms\SecurityLibrary\bin\Debug\SecurityLibrary.dll
1010
E:\FCIS\Year 4\Term2\Computer Security Methods\Computer Security algorithms\SecurityLibrary\bin\Debug\SecurityLibrary.pdb
11+
E:\Projects\Computer-Security-algorithms\SecurityLibrary\bin\Debug\SecurityLibrary.dll
12+
E:\Projects\Computer-Security-algorithms\SecurityLibrary\bin\Debug\SecurityLibrary.pdb
13+
E:\Projects\Computer-Security-algorithms\SecurityLibrary\obj\Debug\SecurityLibrary.csprojResolveAssemblyReference.cache
14+
E:\Projects\Computer-Security-algorithms\SecurityLibrary\obj\Debug\SecurityLibrary.dll
15+
E:\Projects\Computer-Security-algorithms\SecurityLibrary\obj\Debug\SecurityLibrary.pdb
Binary file not shown.
Binary file not shown.

β€ŽSecurityPackage.v12.suo

-1.5 KB
Binary file not shown.

β€ŽSecurityPackageTest/PlayfairTest.cs

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public void PlayfairTestDec4()
9999
{
100100
PlayFair algorithm = new PlayFair();
101101
string plain = algorithm.Decrypt(largeCipher, largeKey);
102+
Console.WriteLine(largePlain);
102103
Assert.IsTrue(plain.Equals(largePlain, StringComparison.InvariantCultureIgnoreCase));
103104
}
104105

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

β€ŽSecurityPackageTest/obj/Debug/SecurityPackageTest.csproj.FileListAbsolute.txt

+7
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,10 @@ E:\FCIS\Year 4\Term2\Computer Security Methods\Computer Security algorithms\Secu
1212
E:\FCIS\Year 4\Term2\Computer Security Methods\Computer Security algorithms\SecurityPackageTest\obj\Debug\SecurityPackageTest.csprojResolveAssemblyReference.cache
1313
E:\FCIS\Year 4\Term2\Computer Security Methods\Computer Security algorithms\SecurityPackageTest\obj\Debug\SecurityPackageTest.dll
1414
E:\FCIS\Year 4\Term2\Computer Security Methods\Computer Security algorithms\SecurityPackageTest\obj\Debug\SecurityPackageTest.pdb
15+
E:\Projects\Computer-Security-algorithms\SecurityPackageTest\bin\Debug\SecurityPackageTest.dll
16+
E:\Projects\Computer-Security-algorithms\SecurityPackageTest\bin\Debug\SecurityPackageTest.pdb
17+
E:\Projects\Computer-Security-algorithms\SecurityPackageTest\bin\Debug\SecurityLibrary.dll
18+
E:\Projects\Computer-Security-algorithms\SecurityPackageTest\bin\Debug\SecurityLibrary.pdb
19+
E:\Projects\Computer-Security-algorithms\SecurityPackageTest\obj\Debug\SecurityPackageTest.csprojResolveAssemblyReference.cache
20+
E:\Projects\Computer-Security-algorithms\SecurityPackageTest\obj\Debug\SecurityPackageTest.dll
21+
E:\Projects\Computer-Security-algorithms\SecurityPackageTest\obj\Debug\SecurityPackageTest.pdb
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)