-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRSAUtils.cs
139 lines (119 loc) · 4.06 KB
/
RSAUtils.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Numerics;
using System.Security.Cryptography;
namespace TikTok
{
// Source: https://stackoverflow.com/a/44441955
internal class RSAUtils
{
public static BigInteger GetBigInteger(byte[] Input)
{
byte[] B = new byte[Input.Length + 1];
Buffer.BlockCopy(Input, 0, B, 1, Input.Length);
Array.Reverse(B);
return new BigInteger(B);
}
public static byte[] GetBytes(BigInteger Input, int Len)
{
byte[] Bytes = Input.ToByteArray();
Len = Bytes.Length;
Array.Resize(ref Bytes, Len);
Array.Reverse(Bytes);
return Bytes;
}
public static BigInteger ModInverse(BigInteger Exp, BigInteger Mod)
{
BigInteger N = Mod;
BigInteger E = Exp;
BigInteger T = 0;
BigInteger A = 1;
while (E != 0)
{
BigInteger Q = N / E;
BigInteger Val;
Val = T;
T = A;
A = Val - Q * A;
Val = N;
N = E;
E = Val - Q * E;
}
if (T < 0)
{
T = T + Mod;
}
return T;
}
public static RSAParameters RecoverRSAParameters(BigInteger n, BigInteger e, BigInteger d)
{
using (RandomNumberGenerator RNG = RandomNumberGenerator.Create())
{
BigInteger k = d * e - 1;
BigInteger two = 2;
BigInteger t = 1;
BigInteger r = k / two;
while (r.IsEven)
{
t++;
r /= two;
}
byte[] Buf = n.ToByteArray();
if (Buf[Buf.Length - 1] == 0)
{
Buf = new byte[Buf.Length - 1];
}
BigInteger nMinusOne = n - 1;
bool Done = false;
BigInteger y = BigInteger.Zero;
for (int i = 0; i < 100 && !Done; i++)
{
BigInteger g;
do
{
RNG.GetBytes(Buf);
g = GetBigInteger(Buf);
}
while (g >= n);
y = BigInteger.ModPow(g, r, n);
if (y.IsOne || y == nMinusOne)
{
i--;
continue;
}
for (BigInteger j = 1; j < t; j++)
{
BigInteger x = BigInteger.ModPow(y, two, n);
if (x.IsOne)
{
Done = true;
break;
}
if (x == nMinusOne)
{
break;
}
y = x;
}
}
BigInteger p = BigInteger.GreatestCommonDivisor(y - 1, n);
BigInteger q = n / p;
BigInteger dp = d % (p - 1);
BigInteger dq = d % (q - 1);
BigInteger inverseQ = ModInverse(q, p);
int modLen = Buf.Length;
int halfModLen = (modLen + 1) / 2;
return new RSAParameters
{
Modulus = GetBytes(n, modLen),
Exponent = GetBytes(e, -1),
D = GetBytes(d, modLen),
P = GetBytes(p, halfModLen),
Q = GetBytes(q, halfModLen),
DP = GetBytes(dp, halfModLen),
DQ = GetBytes(dq, halfModLen),
InverseQ = GetBytes(inverseQ, halfModLen),
};
}
}
}
}