Skip to content

Instantly share code, notes, and snippets.

@huoshan12345
Last active April 10, 2024 23:15
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save huoshan12345/44b1b4927b21d4ec21d1cbd61ea659da to your computer and use it in GitHub Desktop.
Save huoshan12345/44b1b4927b21d4ec21d1cbd61ea659da to your computer and use it in GitHub Desktop.
Instagram enc_password generator in C#
// Packages need to be installed:
// Sodium.Core
// System.Security.Cryptography.Algorithms
// The values of publicKey,keyId,version are from shared_data.
// You need to call https://www.instagram.com/data/shared_data/ to get shared_data first
public static string GenerateEncPassword(string password, string publicKey, string keyId, string version)
{
var time = DateTime.UtcNow.ToTimestamp(); // Unix timestamp
var keyBytes = publicKey.HexToBytes(); // Convert a hex string to a byte array
var key = new byte[32];
new Random().NextBytes(key);
var iv = new byte[12];
var tag = new byte[16];
var plainText = password.ToBytes(); // ToBytes = Encoding.UTF8.GetBytes
var cipherText = new byte[plainText.Length];
using (var cipher = new AesGcm(key))
{
cipher.Encrypt(nonce: iv,
plaintext: plainText,
ciphertext: cipherText,
tag: tag,
associatedData: time.ToString().ToBytes()); // GetBytes = Encoding.UTF8.GetBytes
}
var encryptedKey = SealedPublicKeyBox.Create(key, keyBytes);
var bytesOfLen = ((short)encryptedKey.Length).ToBytes(); // ToBytes = BitConverter.GetBytes(short);
var info = new byte[] { 1, byte.Parse(keyId) };
var bytes = info.Concat(bytesOfLen).Concat(encryptedKey).Concat(tag).Concat(cipherText); // Concat means that concat two array
// expected: #PWD_INSTAGRAM_BROWSER:10:1595671654:ARBQAFWLYGkTT9UU0dyUCkaGTRFu0PH5Ph5s86DUAbZ+B9xon8cKmnqQGaUo7bB4NHCMKQRY69b9LwaJZ1rDw1OFM0LEGtI+KbDuDC0QnfJM6o1no0XPOl73RJoUZ/OfN5nE2q/IdqX0NFinS0faRf8=
var str = $"#PWD_INSTAGRAM_BROWSER:{version}:{time}:{bytes.ToBase64()}"; // ToBase64 = Convert.ToBase64String
return str;
}
public static byte[] HexToBytes(this string hex)
{
return Enumerable.Range(0, hex.Length / 2)
.Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16))
.ToArray();
}
public static T[] Concat<T>(this T[] x, T[] y)
{
var z = new T[x.Length + y.Length];
x.CopyTo(z, 0);
y.CopyTo(z, x.Length);
return z;
}
private static readonly DateTime _jan1St1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static long ToTimestamp(this DateTime d)
{
return (long)(d.ToUniversalTime() - _jan1St1970).TotalSeconds;
}
@zemelLeong
Copy link

请问现在还能用嘛?我刚尝试了下好像不行,返回如下数据
{"user": false, "authenticated": false, "status": "ok"}

@zemelLeong
Copy link

可以用的 谢谢大佬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment