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
|
public class AppLoginHelper
{
private const string RsaPubKey =
"***";
private const string PublicKeyInner =
"***";
private const string WeicoPin =
"***";
private static readonly int MAX_DECRYPT_BLOCK = 128;
private static readonly int MAX_ENCRYPT_BLOCK = 117;
public AppLoginModel GetLoginParam(string username, string password)
{
var result = new AppLoginModel();
var keyBytes = Convert.FromBase64String(RsaPubKey);
var dd = DecryptByPublicKey(keyBytes, PublicKeyInner);
result.p = Convert.ToBase64String(EncryptByPublicKey(Encoding.UTF8.GetBytes(password),
Encoding.UTF8.GetString(dd)));
// s 标签计算
var decodeStr = Decode(WeicoPin);
var test1 = $"{username}{password}{decodeStr}";
var fff = MdStringOld(test1);
var a1 = fff[1];
var a2 = fff[5];
var a3 = fff[2];
var a4 = fff[10];
var a5 = fff[17];
var a6 = fff[9];
var a7 = fff[25];
var a8 = fff[27];
result.s = $"{a1}{a2}{a3}{a4}{a5}{a6}{a7}{a8}";
result.u = username;
return result;
}
/// <summary>
/// 微博 so库内 MD5加密
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
private static string MdStringOld(string str)
{
var md5 = MD5.Create();
var s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
return s.Aggregate("", (current, t) => current + t.ToString("x2"));
}
private static string Decode(string encryptedStr)
{
var dd = DecryptByPublicKey(Convert.FromBase64String(encryptedStr), PublicKeyInner);
return Encoding.UTF8.GetString(dd);
}
private static byte[] DecryptByPublicKey(byte[] encryptedData, string publicKey)
{
var keyBytes = Convert.FromBase64String(publicKey);
var paraPub = ConvertFromPublicKey(keyBytes);
var privateRsa = new RSACryptoServiceProvider();
privateRsa.ImportParameters(paraPub);
AsymmetricKeyParameter pbk = DotNetUtilities.GetRsaPublicKey(paraPub);
var c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
c.Init(false, pbk);
var inputLen = encryptedData.Length;
var offSet = 0;
var i = 0;
var outStream = new MemoryStream();
byte[] cache;
while (inputLen - offSet > 0)
{
if (inputLen - offSet > MAX_DECRYPT_BLOCK)
cache = c.DoFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
else
cache = c.DoFinal(encryptedData, offSet, inputLen - offSet);
outStream.Write(cache, 0, cache.Length);
i++;
offSet = i * MAX_DECRYPT_BLOCK;
}
var decryptedData = outStream.ToArray();
outStream.Close();
return decryptedData;
}
private static byte[] EncryptByPublicKey(byte[] data, string publicKey)
{
var keyBytes = Convert.FromBase64String(publicKey);
var paraPub = ConvertFromPublicKey(keyBytes);
var privateRsa = new RSACryptoServiceProvider();
privateRsa.ImportParameters(paraPub);
AsymmetricKeyParameter pbk = DotNetUtilities.GetRsaPublicKey(paraPub);
var c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
c.Init(true, pbk);
var inputLen = data.Length;
var offSet = 0;
var i = 0;
var outStream = new MemoryStream();
byte[] cache;
while (inputLen - offSet > 0)
{
if (inputLen - offSet > MAX_ENCRYPT_BLOCK)
cache = c.DoFinal(data, offSet, MAX_ENCRYPT_BLOCK);
else
cache = c.DoFinal(data, offSet, inputLen - offSet);
outStream.Write(cache, 0, cache.Length);
i++;
offSet = i * MAX_ENCRYPT_BLOCK;
}
var decryptedData = outStream.ToArray();
outStream.Close();
return decryptedData;
}
private static RSAParameters ConvertFromPublicKey(byte[] pubkeyBytes)
{
var keySize1024 = pubkeyBytes.Length == 162;
var keySize2048 = pubkeyBytes.Length == 294;
if (!(keySize1024 || keySize2048))
throw new ArgumentException("pem file content is incorrect, Only support the key size is 1024 or 2048");
var pemModulus = keySize1024 ? new byte[128] : new byte[256];
var pemPublicExponent = new byte[3];
Array.Copy(pubkeyBytes, keySize1024 ? 29 : 33, pemModulus, 0, keySize1024 ? 128 : 256);
Array.Copy(pubkeyBytes, keySize1024 ? 159 : 291, pemPublicExponent, 0, 3);
var para = new RSAParameters();
para.Modulus = pemModulus;
para.Exponent = pemPublicExponent;
return para;
}
}
|