-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathRailFence.cs
78 lines (69 loc) · 2.34 KB
/
RailFence.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SecurityLibrary
{
public class RailFence : ICryptographicTechnique<string, int>
{
public int Analyse(string plainText, string cipherText)
{
cipherText = cipherText.ToLower();
List<int> possibleKeys = new List<int>();
char sec = cipherText[1];
for (int i = 0; i < plainText.Length; i++)
{
if (plainText[i] == sec) possibleKeys.Add(i);
}
for (int i = 0; i < possibleKeys.Count; i++)
{
Console.WriteLine(possibleKeys[i].ToString());
string s = Encrypt(plainText, possibleKeys[i]).ToLower();
Console.WriteLine(cipherText + " " + s);
if (String.Equals(cipherText, s))
{
Console.WriteLine(possibleKeys[i]);
return possibleKeys[i];
}
}
return -1;
}
public string Decrypt(string cipherText, int key)
{
cipherText = cipherText.ToLower();
int PTLength = (int) Math.Ceiling((double)cipherText.Length / key);
return Encrypt(cipherText, PTLength).ToLower();
}
public string Encrypt(string plainText, int key)
{
String.Join(plainText,plainText.Split(' '));
Console.WriteLine(plainText);
List<List<char>> table = new List<List<char>>();
int each = (int)Math.Ceiling((double)plainText.Length/key);
int counter = 0;
string CT = "";
for (int i = 0; i < key; i++)
{
table.Add(new List<char>());
}
for (int i = 0; i < each; i++)
{
for (int j = 0; j < key && j<plainText.Length; j++)
{
table[j].Add(plainText[counter]);
counter++;
if(counter == plainText.Length) break;
}
}
for (int i = 0; i < table.Count; i++)
{
for (int j = 0; j < table[i].Count; j++)
{
CT += table[i][j];
}
}
return CT.ToUpper();
}
}
}