-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
117 lines (110 loc) · 3.77 KB
/
Program.cs
File metadata and controls
117 lines (110 loc) · 3.77 KB
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
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RSAalgorithm
{
class Program
{
public const string alphabet = "abcdefghigklmnopqrstuvwxyz_";
public static readonly int[] numbers = { 11, 20, 17, 22, 15, 14, 23, 9, 19, 18, 26, 32, 12, 30, 7, 21, 46, 38, 43, 10, 43, 16, 50, 24, 25, 28, 27 };
private static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
private static int f(int p, int q)
{
return (p - 1) * (q - 1);
}
private static int phi(int n)
{
int result = 1;
for (int i = 2; i < n; i++)
if (gcd(i, n) == 1)
result++;
return result;
}
private static long ModPow(long x, long y, long p)
{
if (y == 1)
return x;
else
return (((long)Math.Pow(x, y)) % p);
}
private static void Encryption()
{
Console.WriteLine("Enter p:");
var p = int.Parse(Console.ReadLine());
Console.WriteLine("Enter q:");
var q = int.Parse(Console.ReadLine());
Console.WriteLine("Enter PK:");
var PK = int.Parse(Console.ReadLine());
var N = p * q;
Console.WriteLine("N:" + N);
var SK = ModPow(PK, phi(f(p, q)) - 1, f(p, q));
Console.WriteLine("SK:" + SK);
Console.WriteLine("Enter the plain text:");
var plain = Console.ReadLine();
plain = plain.ToLower().Replace(" ", "_");
var cipherText = "";
foreach(var s in plain)
{
var index = ModPow(numbers[alphabet.IndexOf(s)], PK, N);
cipherText += index + " ";
}
Console.WriteLine("Cipher is: ");
Console.WriteLine(cipherText);
}
private static void Decryption()
{
Console.WriteLine("Enter p:");
var p = int.Parse(Console.ReadLine());
Console.WriteLine("Enter q:");
var q = int.Parse(Console.ReadLine());
Console.WriteLine("Enter PK:");
var PK = int.Parse(Console.ReadLine());
var N = p * q;
Console.WriteLine("N: " + N);
var SK = ModPow(PK, phi(f(p, q)) - 1, f(p, q));
Console.WriteLine("SK: " + SK);
Console.WriteLine("Enter cipher:");
var cipherText = Console.ReadLine();
var cipher= cipherText.Split(' ').Select(int.Parse).ToList();
var plainText = "";
foreach(var s in cipher)
{
var index = ModPow(s, SK, N);
plainText += alphabet[Array.IndexOf(numbers,(int)index)];
}
Console.WriteLine("Plain text is: ");
Console.WriteLine(plainText);
}
public static void Main()
{
bool choose = true;
while (choose)
{
Console.WriteLine("Press 1 for encryption or 2 for decryption:");
int choice = int.Parse(Console.ReadLine());
choose = false;
switch (choice)
{
case 1:
Encryption();
break;
case 2:
Decryption();
break;
default:
Console.WriteLine("Invalid choice!");
choose = true;
break;
}
}
Console.ReadKey();
}
}
}