Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 02b38b0

Browse files
railfence algorithm added
1 parent 395413a commit 02b38b0

14 files changed

+118
-6
lines changed

‎SecurityLibrary/MainAlgorithms/Columnar.cs

Lines changed: 62 additions & 2 deletions
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;
@@ -15,12 +16,71 @@ public List<int> Analyse(string plainText, string cipherText)
1516

1617
public string Decrypt(string cipherText, List<int> key)
1718
{
18-
throw new NotImplementedException();
19+
List<List<char>> table = new List<List<char>>();
20+
Dictionary<int, string> cip = new Dictionary<int, string>();
21+
string PT = "";
22+
cipherText = cipherText.ToLower();
23+
int columns = key.Count;
24+
int rows = (int)Math.Ceiling((double)cipherText.Length / columns);
25+
for (int i = 0; i < rows; i++)
26+
{
27+
table.Add(new List<char>());
28+
}
29+
for (int i = 0; i < columns; i++)
30+
{
31+
for (int j = 0; j < rows; j++)
32+
{
33+
34+
}
35+
}
36+
37+
return "";
1938
}
2039

2140
public string Encrypt(string plainText, List<int> key)
2241
{
23-
throw new NotImplementedException();
42+
int columns = key.Count;
43+
int rows = (int)Math.Ceiling((double)plainText.Length / columns);
44+
if (plainText.Length != rows * columns)
45+
{
46+
int x = (rows * columns) - plainText.Length;
47+
string appender = new string('x',x);
48+
plainText += appender;
49+
}
50+
List<List<char>> table = new List<List<char>>();
51+
Dictionary<int, string> cip = new Dictionary<int, string>();
52+
string CT = "";
53+
for (int i = 0; i < rows; i++)
54+
{
55+
table.Add(new List<char>());
56+
}
57+
58+
int counter = 0;
59+
for (int i = 0; i < rows; i++)
60+
{
61+
for (int j = 0; j < columns && counter<plainText.Length; j++)
62+
{
63+
table[i].Add(plainText[counter]);
64+
counter++;
65+
}
66+
}
67+
68+
for (int i = 0; i < columns; i++)
69+
{
70+
string tmp = "";
71+
for (int j = 0; j < rows; j++)
72+
{
73+
tmp += table[j][i];
74+
cip[key[i] ] = tmp;
75+
}
76+
}
77+
78+
for (int i = 1; i <= cip.Count; i++)
79+
{
80+
CT += cip[i];
81+
}
82+
Console.WriteLine(CT);
83+
return CT.ToUpper();
2484
}
2585
}
2686
}

‎SecurityLibrary/MainAlgorithms/RailFence.cs

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,69 @@ public class RailFence : ICryptographicTechnique<string, int>
1010
{
1111
public int Analyse(string plainText, string cipherText)
1212
{
13-
throw new NotImplementedException();
13+
cipherText = cipherText.ToLower();
14+
List<int> possibleKeys = new List<int>();
15+
char sec = cipherText[1];
16+
for (int i = 0; i < plainText.Length; i++)
17+
{
18+
if (plainText[i] == sec) possibleKeys.Add(i);
19+
}
20+
21+
for (int i = 0; i < possibleKeys.Count; i++)
22+
{
23+
Console.WriteLine(possibleKeys[i].ToString());
24+
string s = Encrypt(plainText, possibleKeys[i]).ToLower();
25+
Console.WriteLine(cipherText + " " + s);
26+
if (String.Equals(cipherText, s))
27+
{
28+
Console.WriteLine(possibleKeys[i]);
29+
return possibleKeys[i];
30+
}
31+
}
32+
33+
return -1;
34+
1435
}
1536

1637
public string Decrypt(string cipherText, int key)
1738
{
18-
throw new NotImplementedException();
39+
cipherText = cipherText.ToLower();
40+
int PTLength = (int) Math.Ceiling((double)cipherText.Length / key);
41+
return Encrypt(cipherText, PTLength).ToLower();
42+
1943
}
2044

2145
public string Encrypt(string plainText, int key)
2246
{
23-
throw new NotImplementedException();
47+
String.Join(plainText,plainText.Split(' '));
48+
Console.WriteLine(plainText);
49+
List<List<char>> table = new List<List<char>>();
50+
int each = (int)Math.Ceiling((double)plainText.Length/key);
51+
int counter = 0;
52+
string CT = "";
53+
for (int i = 0; i < key; i++)
54+
{
55+
table.Add(new List<char>());
56+
}
57+
58+
for (int i = 0; i < each; i++)
59+
{
60+
for (int j = 0; j < key && j<plainText.Length; j++)
61+
{
62+
table[j].Add(plainText[counter]);
63+
counter++;
64+
if(counter == plainText.Length) break;
65+
}
66+
}
67+
68+
for (int i = 0; i < table.Count; i++)
69+
{
70+
for (int j = 0; j < table[i].Count; j++)
71+
{
72+
CT += table[i][j];
73+
}
74+
}
75+
return CT.ToUpper();
2476
}
2577
}
26-
}
78+
}
1.5 KB
Binary file not shown.
2 KB
Binary file not shown.
1.5 KB
Binary file not shown.
2 KB
Binary file not shown.

‎SecurityPackage.v12.suo

0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /