|
21 | 21 | using System.Text;
|
22 | 22 | using InstagramApiSharp.Classes;
|
23 | 23 | using System.Net.Http;
|
| 24 | + |
| 25 | +#if NETSTANDARD || WINDOWS_UWP || NET461_OR_GREATER |
| 26 | +using Org.BouncyCastle.Cms; |
24 | 27 | using Org.BouncyCastle.Security;
|
25 | 28 | using Org.BouncyCastle.Crypto.Modes;
|
26 | 29 | using Org.BouncyCastle.Crypto.Engines;
|
27 | 30 | using Org.BouncyCastle.Crypto.Parameters;
|
| 31 | +#elif NET || NETCOREAPP3_1 |
| 32 | +using System.Security.Cryptography; |
| 33 | +#endif |
28 | 34 |
|
29 | 35 | namespace InstagramApiSharp
|
30 | 36 | {
|
@@ -163,9 +169,10 @@ public static string GenerateJazoest(string guid)
|
163 | 169 | return "2" + ix;
|
164 | 170 | }
|
165 | 171 |
|
| 172 | +#if NETSTANDARD || WINDOWS_UWP || NET461_OR_GREATER |
166 | 173 | static private readonly SecureRandom secureRandom = new SecureRandom();
|
167 | 174 |
|
168 |
| - public static string GetEncryptedPassword(this IInstaApi api, string password, long? providedTime = null) |
| 175 | + public static string GetEncryptedPassword(this IInstaApi api, string password, long? providedTime = null, uint version = 4) |
169 | 176 | {
|
170 | 177 | var pubKey = api.GetLoggedUser().PublicKey;
|
171 | 178 | var pubKeyId = api.GetLoggedUser().PublicKeyId;
|
@@ -201,11 +208,64 @@ public static string GetEncryptedPassword(this IInstaApi api, string password, l
|
201 | 208 | if (BitConverter.IsLittleEndian)
|
202 | 209 | Array.Reverse(encKeyIdBytes);
|
203 | 210 | encKeyIdBytes[0] = 1;
|
204 |
| - var payload = Convert.ToBase64String(encKeyIdBytes.Concat(iv).Concat(buffersSize).Concat(encryptedKey).Concat(tag).Concat(ciphertext).ToArray()); |
| 211 | + var payload = Convert |
| 212 | + .ToBase64String(encKeyIdBytes |
| 213 | + .Concat(iv) |
| 214 | + .Concat(buffersSize) |
| 215 | + .Concat(encryptedKey) |
| 216 | + .Concat(tag) |
| 217 | + .Concat(ciphertext) |
| 218 | + .ToArray()); |
| 219 | + |
| 220 | + return $"#PWD_INSTAGRAM:{version}:{time}:{payload}"; |
| 221 | + } |
| 222 | +#elif NET || NETCOREAPP3_1 |
| 223 | + private readonly static RandomNumberGenerator RnGenerator = RandomNumberGenerator.Create(); |
| 224 | + private readonly static RSACryptoServiceProvider RsaProvider = new (); |
| 225 | + |
| 226 | + public static string GetEncryptedPassword(this IInstaApi api, string password, long? providedTime = null, int version = 4) |
| 227 | + { |
| 228 | + var pubKey = api.GetLoggedUser().PublicKey; |
| 229 | + var pubKeyId = api.GetLoggedUser().PublicKeyId; |
| 230 | + byte[] randKey = new byte[32]; |
| 231 | + byte[] iv = new byte[12]; |
| 232 | + RnGenerator.GetBytes(randKey); |
| 233 | + RnGenerator.GetBytes(iv); |
| 234 | + |
| 235 | + long time = providedTime ?? DateTimeOffset.UtcNow.ToUnixTimeSeconds(); |
| 236 | + byte[] pubKEY = Convert.FromBase64String(pubKey); |
| 237 | + string decodedPubKey = Encoding.UTF8.GetString(pubKEY, 0, pubKEY.Length); |
| 238 | + decodedPubKey = decodedPubKey.Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", ""); |
| 239 | + byte[] publicKeyBytes = Convert.FromBase64String(decodedPubKey.Trim()); |
| 240 | + RsaProvider.ImportSubjectPublicKeyInfo(publicKeyBytes, out _); |
| 241 | + byte[] encryptedKey = RsaProvider.Encrypt(randKey, false); |
| 242 | + |
| 243 | + var plaintext = Encoding.UTF8.GetBytes(password); |
| 244 | + |
| 245 | + byte[] ciphertext = new byte[plaintext.Length]; |
| 246 | + byte[] tag = new byte[16]; |
| 247 | + byte[] associatedData = Encoding.UTF8.GetBytes(time.ToString()); |
| 248 | + AesGcm aesGsm = new (randKey); |
| 249 | + aesGsm.Encrypt(iv, plaintext, ciphertext, tag, associatedData); |
| 250 | + |
| 251 | + byte[] encKeyIdBytes = BitConverter.GetBytes(Convert.ToUInt16(pubKeyId)); |
| 252 | + if (BitConverter.IsLittleEndian) |
| 253 | + Array.Reverse(encKeyIdBytes); |
| 254 | + encKeyIdBytes[0] = 1; |
| 255 | + byte[] buffersSize = BitConverter.GetBytes(Convert.ToInt16(encryptedKey.Length)); |
| 256 | + var payload = Convert |
| 257 | + .ToBase64String(encKeyIdBytes |
| 258 | + .Concat(iv) |
| 259 | + .Concat(buffersSize) |
| 260 | + .Concat(encryptedKey) |
| 261 | + .Concat(tag) |
| 262 | + .Concat(ciphertext) |
| 263 | + .ToArray()); |
205 | 264 |
|
206 |
| - return $"#PWD_INSTAGRAM:4:{time}:{payload}"; |
| 265 | + return $"#PWD_INSTAGRAM:{version}:{time}:{payload}"; |
207 | 266 | }
|
208 | 267 |
|
| 268 | +#endif |
209 | 269 | public static string GetJson(this InstaLocationShort location)
|
210 | 270 | {
|
211 | 271 | if (location == null)
|
|
0 commit comments