-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathCeaser.cs
74 lines (65 loc) · 2.09 KB
/
Ceaser.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SecurityLibrary
{
public class Ceaser : ICryptographicTechnique<string, int>
{
public string alphabet = "abcdefghijklmnopqrstuvwxyz";
public int letterNum(char letter) // O(1)
{
for (int i = 0; i < 26; i++)
{
if (letter == alphabet[i]) return i;
}
return -1;
}
public string Encrypt(string plainText, int key)
{
int PTLength = plainText.Length;
string CT = "";
for (int i = 0; i < PTLength; i++) // O(N)
{
if (char.IsLetter(plainText[i]))
{
int letterIndx = ((key + letterNum(plainText[i]))%26);
CT += char.ToUpper(alphabet[letterIndx]);
}
else
{
CT += plainText[i];
}
}
return CT;
}
public string Decrypt(string cipherText, int key)
{
cipherText = cipherText.ToLower();
int CTLength = cipherText.Length;
string PT = "";
for (int i = 0; i < CTLength; i++) // O(N)
{
if (char.IsLetter(cipherText[i]))
{
int letterIndx = ((letterNum(cipherText[i]) - key) % 26);
if(letterIndx < 0) letterIndx += 26;
PT += alphabet[letterIndx];
}
else
{
PT += cipherText[i];
}
}
return PT;
}
public int Analyse(string plainText, string cipherText) // O(1)
{
if (plainText.Length != cipherText.Length) return -1;
int letterPN = letterNum(plainText[0]);
int letterCN = letterNum(char.ToLower(cipherText[0]));
return ((letterCN - letterPN) < 0) ? (letterCN - letterPN) + 26 : (letterCN - letterPN) % 26;
}
}
}