-
Notifications
You must be signed in to change notification settings - Fork 3
/
EncryptPasswordFactory.cs
162 lines (133 loc) · 5.03 KB
/
EncryptPasswordFactory.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using skadisteam.login.Models;
using skadisteam.login.Models.Json;
using System;
using System.Globalization;
using System.Numerics;
using System.Text;
namespace skadisteam.login.Factories
{
internal static class EncryptPasswordFactory
{
private const string Base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
private const string Hex = "0123456789abcdef";
internal static string Create(GetRsaKeyResponse getRsaKeyResponse, string password)
{
var encryptPasswordModel = new EncryptPasswordModel
{
PublicKeyExp = getRsaKeyResponse.PublicKeyExp,
PublicKeyMod = getRsaKeyResponse.PublicKeyMod,
Password = password
};
var encrypted = string.Empty;
while (encrypted.Length < 2 || encrypted.Substring(encrypted.Length - 2) != "==")
{
encrypted = EncryptPassword(encryptPasswordModel);
}
return encrypted;
}
private static string EncryptPassword(EncryptPasswordModel encryptPasswordModel)
{
// Convert the public keys to BigIntegers
var modulus = CreateBigInteger(encryptPasswordModel.PublicKeyMod);
var exponent = CreateBigInteger(encryptPasswordModel.PublicKeyExp);
// Original: $data = this.pkcs1pad2($data,($pubkey.modulus.bitLength()+7)>>3);
// I'm going to hardcode the bitlength, I can't figure that out right now.
var encryptedNumber = Pkcs1Pad2(encryptPasswordModel.Password, (2048 + 7) >> 3);
// And now, the RSA encryption
encryptedNumber = BigInteger.ModPow(encryptedNumber, exponent, modulus);
// Finally we convert the encrypted string back to Base16
var encryptedString = encryptedNumber.ToString("x");
// And then we decode it back
// And we put it back into Base64
encryptedString = EncodeBase64(DecodeHex(encryptedString));
return encryptedString;
}
public static string DecodeHex(string input)
{
var stringBuilder = new StringBuilder();
var i = 0;
do
{
var a = ((Hex.IndexOf(input[i++]) << 4) & 0xf0);
var b = 0;
if (i < input.Length)
b = (Hex.IndexOf(input[i++]) & 0xf);
stringBuilder.Append(new[] { (char)(a | b) });
} while (i < input.Length);
return stringBuilder.ToString();
}
public static string EncodeBase64(string input)
{
var stringBuilder = new StringBuilder();
var i = 0;
do
{
var x = 0;
int chr1 = input[i++];
int chr2, chr3;
if (input.Length > i)
{
chr2 = input.Length > i ? input[i++] : 0;
if (input.Length > i)
{
chr3 = input.Length > i ? input[i++] : 0;
}
else
{
x = 2;
chr3 = 0;
}
}
else
{
x = 1;
chr2 = 0;
chr3 = 0;
}
var enc1 = chr1 >> 2;
var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
var enc4 = chr3 & 63;
if (x == 1)
enc3 = enc4 = 64;
else if (x == 2)
enc4 = 64;
stringBuilder.Append(new[] { Base64[enc1], Base64[enc2], Base64[enc3], Base64[enc4] });
} while (i < input.Length);
return stringBuilder.ToString();
}
/// <summary>
/// Encodes the data
/// </summary>
/// <param name="data"></param>
/// <param name="keySize"></param>
/// <returns></returns>
public static BigInteger Pkcs1Pad2(string data, int keySize)
{
if (keySize < data.Length + 11)
return new BigInteger();
var buffer = new byte[256];
var i = data.Length - 1;
while (i >= 0 && keySize > 0)
{
buffer[--keySize] = (byte)data[i--];
}
// Padding, I think
var random = new Random();
buffer[--keySize] = 0;
while (keySize > 2)
{
buffer[--keySize] = (byte)random.Next(1, 256);
//buffer[--keySize] = 5;
}
buffer[--keySize] = 2;
buffer[--keySize] = 0;
Array.Reverse(buffer);
return new BigInteger(buffer);
}
public static BigInteger CreateBigInteger(string hex)
{
return BigInteger.Parse("00" + hex, NumberStyles.AllowHexSpecifier);
}
}
}