-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
64 lines (59 loc) · 1.93 KB
/
Program.cs
File metadata and controls
64 lines (59 loc) · 1.93 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
using System;
using System.Linq;
using System.Numerics;
namespace PublicPrivateKeyEncryption
{
class Program
{
static void Main()
{
int p = 17; // 1st large prime (larger)
int q = 13; // 2nd large prime
int pubKey1 = p*q;
int pubKey2 = 11;// < pubKey1 - 65537 is commonly used
int privKey = CalculateExtendedEuclideanAlgorithm(pubKey2, (p - 1) * (q - 1));
Console.WriteLine("input\tcipher\tplain");
foreach(char input in "this is the text")
{
var cipherText = BigInteger.Pow(input, pubKey2) % pubKey1;
var plainText = (char)(BigInteger.Pow(cipherText, privKey) % pubKey1);
Console.WriteLine($"{input}\t{cipherText}\t{plainText}");
if (input != plainText) throw new ApplicationException($"{input} != {plainText}");
}
Console.ReadLine();
}
/// <summary>
/// From http://amir-shenodua.blogspot.com/2012/06/extended-gcd-algorithm-extended.html
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
static int CalculateExtendedEuclideanAlgorithm(int a, int b)
{
if (a < b) //if A less than B, switch them
{
int temp = a;
a = b;
b = temp;
}
int r = b, q = 0;
int x0 = 1, y0 = 0;
int x1 = 0, y1 = 1;
int x = 0, y = 0;
while (r > 1)
{
r = a % b;
q = a / b;
x = x0 - q * x1;
y = y0 - q * y1;
x0 = x1;
y0 = y1;
x1 = x;
y1 = y;
a = b;
b = r;
}
return new[] { r, x, y }.Where(z => z > 1).Min();
}
}
}