-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathColumnar.cs
108 lines (96 loc) · 2.93 KB
/
Columnar.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
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SecurityLibrary
{
public class Columnar : ICryptographicTechnique<string, List<int>>
{
public List<int> Analyse(string plainText, string cipherText)
{
throw new NotImplementedException();
}
public string Decrypt(string cipherText, List<int> key)
{
cipherText = cipherText.ToUpper();
int x = cipherText.Length;
if (x % key.Count == 0)
{
}
else
{
x += key.Count;
}
double col = x / key.Count;
int c = (int)(col);
string finalll = "";
char[,] enc = new char[c, key.Count];
int k = 0;
int tmp = 0;
for (int i = 0; i < key.Count; i++)
{
k = key.IndexOf(i + 1);
for (int j = 0; j < col; j++)
{
if (tmp < cipherText.Length)
{
enc[j, k] = cipherText[tmp];
tmp++;
}
}
}
for (int i = 0; i < col; i++)
{
for (int j = 0; j < key.Count; j++)
{
finalll = finalll + enc[i, j];
}
}
return finalll.ToUpper();
}
public string Encrypt(string plainText, List<int> key)
{
int columns = key.Count;
int rows = (int)Math.Ceiling((double)plainText.Length / columns);
if (plainText.Length != rows * columns)
{
int x = (rows * columns) - plainText.Length;
string appender = new string('x',x);
plainText += appender;
}
List<List<char>> table = new List<List<char>>();
Dictionary<int, string> cip = new Dictionary<int, string>();
string CT = "";
for (int i = 0; i < rows; i++)
{
table.Add(new List<char>());
}
int counter = 0;
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns && counter<plainText.Length; j++)
{
table[i].Add(plainText[counter]);
counter++;
}
}
for (int i = 0; i < columns; i++)
{
string tmp = "";
for (int j = 0; j < rows; j++)
{
tmp += table[j][i];
cip[key[i] ] = tmp;
}
}
for (int i = 1; i <= cip.Count; i++)
{
CT += cip[i];
}
Console.WriteLine(CT);
return CT.ToUpper();
}
}
}