-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utils.cs
85 lines (69 loc) · 1.78 KB
/
Utils.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RSATutor
{
public struct PublicKey
{
public PublicKey(ulong e_, ulong n_)
{
e = e_;
n = n_;
}
public ulong e;
public ulong n;
}
public struct PrivateKey
{
public PrivateKey(ulong d_, ulong n_)
{
d = d_;
n = n_;
}
public ulong d;
public ulong n;
}
class Utils
{
//https://github.com/amughalbscs16/RSA-Implementation/blob/master/RSA%20in%20CPP/main.cpp
public static ulong PowMod(ulong baseVal, ulong powerVal, ulong modVal)
{
ulong answer = 1;
for (ulong i = 0; i < powerVal; ++i)
{
answer *= baseVal;
answer %= modVal;
}
return answer;
}
public static ulong[] Encrypt(byte[] input, ulong e, ulong n)
{
ulong[] output = new ulong[input.Length];
for(int i = 0; i < input.Length; ++i)
{
output[i] = PowMod(input[i], e, n);
}
return output;
}
public static byte[] Decrypt(ulong[] input, ulong d, ulong n)
{
byte[] output = new byte[input.Length];
for (int i = 0; i < input.Length; ++i)
{
output[i] = (byte)PowMod(input[i], d, n);
}
return output;
}
public static string ToString(byte[] input)
{
string str = "";
foreach (byte b in input)
{
str += string.Format("0x{0:X2} ", b);
}
return str;
}
}
}