Skip to content

Commit 3ddd8a6

Browse files
committed
v1.8.1 [Public Version]
- [Add] .NET6.0 and .NET8.0 support - [Add] RemoveBioLinksAsync to AccountProcessor - [Add] AddOrUpdateBioLinkAsync to AccountProcessor - [Remove] csrftoken and cookies usage - [Exclude] BouncyCastle library for .NET6.0 or .NET8.0 framworks
1 parent 64153f9 commit 3ddd8a6

File tree

3 files changed

+65
-5
lines changed

3 files changed

+65
-5
lines changed

src/InstagramApiSharp/Classes/Android/DeviceInfo/ApiRequestMessage.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using InstagramApiSharp.Helpers;
44
using Newtonsoft.Json;
55
using InstagramApiSharp.API.Versions;
6-
using Org.BouncyCastle.Asn1.X500;
76
namespace InstagramApiSharp.Classes.Android.DeviceInfo
87
{
98
public class ApiRequestMessage

src/InstagramApiSharp/Helpers/ExtensionHelper.cs

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@
2121
using System.Text;
2222
using InstagramApiSharp.Classes;
2323
using System.Net.Http;
24+
25+
#if NETSTANDARD || WINDOWS_UWP || NET461_OR_GREATER
26+
using Org.BouncyCastle.Cms;
2427
using Org.BouncyCastle.Security;
2528
using Org.BouncyCastle.Crypto.Modes;
2629
using Org.BouncyCastle.Crypto.Engines;
2730
using Org.BouncyCastle.Crypto.Parameters;
31+
#elif NET || NETCOREAPP3_1
32+
using System.Security.Cryptography;
33+
#endif
2834

2935
namespace InstagramApiSharp
3036
{
@@ -163,9 +169,10 @@ public static string GenerateJazoest(string guid)
163169
return "2" + ix;
164170
}
165171

172+
#if NETSTANDARD || WINDOWS_UWP || NET461_OR_GREATER
166173
static private readonly SecureRandom secureRandom = new SecureRandom();
167174

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)
169176
{
170177
var pubKey = api.GetLoggedUser().PublicKey;
171178
var pubKeyId = api.GetLoggedUser().PublicKeyId;
@@ -201,11 +208,64 @@ public static string GetEncryptedPassword(this IInstaApi api, string password, l
201208
if (BitConverter.IsLittleEndian)
202209
Array.Reverse(encKeyIdBytes);
203210
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());
205264

206-
return $"#PWD_INSTAGRAM:4:{time}:{payload}";
265+
return $"#PWD_INSTAGRAM:{version}:{time}:{payload}";
207266
}
208267

268+
#endif
209269
public static string GetJson(this InstaLocationShort location)
210270
{
211271
if (location == null)

src/InstagramApiSharp/InstagramApiSharp.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ v1.3.3.0
289289
<PackageIcon>insta.png</PackageIcon>
290290
<NeutralLanguage>en-US</NeutralLanguage>
291291
<PackageLicenseFile>LICENSE.txt</PackageLicenseFile>
292+
<PackageReadmeFile>readme.md</PackageReadmeFile>
292293
</PropertyGroup>
293294

294295
<ItemGroup>
@@ -316,7 +317,7 @@ v1.3.3.0
316317

317318
<ItemGroup>
318319
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
319-
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" />
320+
<PackageReference Include="Portable.BouncyCastle" Version="1.9.0" Condition="'$(TargetFramework)' != 'net6.0' AND '$(TargetFramework)' != 'net8.0'"/>
320321
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform" Version="6.2.14" Condition="'$(TargetFramework)' == 'uap10.0'" />
321322
<PackageReference Include="System.Net.Http" Version="4.3.4" Condition="'$(TargetFramework)' == 'net461'" />
322323
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />

0 commit comments

Comments
 (0)