using System.Security.Cryptography;
public static class Program
public static void Main()
var encPass = GenerateEncPassword("db3d8e1f4ea2", "8dd9aad29d9a614c338cff479f850d3ec57c525c33b3f702ab65e9e057fc087e", "87", "9");
Console.WriteLine(encPass);
public static string GenerateEncPassword(string password, string publicKey, string keyId, string version)
var time = DateTime.UtcNow.ToTimestamp();
var keyBytes = publicKey.HexToBytes();
new Random().NextBytes(key);
var plainText = Encoding.UTF8.GetBytes(password);
var cipherText = new byte[plainText.Length];
using (var cipher = new AesGcm(key))
cipher.Encrypt(nonce: iv,
associatedData: Encoding.UTF8.GetBytes(time.ToString()));
var encryptedKey = SealedPublicKeyBox.Create(key, keyBytes);
var bytesOfLen = BitConverter.GetBytes((short)encryptedKey.Length);
var info = new byte[] { 1, byte.Parse(keyId) };
var bytes = info.Concat(bytesOfLen).Concat(encryptedKey).Concat(tag).Concat(cipherText);
var str = $"#PWD_INSTAGRAM_BROWSER:{version}:{time}:{Convert.ToBase64String(bytes)}";
public static byte[] HexToBytes(this string hex)
return Enumerable.Range(0, hex.Length / 2)
.Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16))
public static T[] Concat<T>(this T[] x, T[] y)
var z = new T[x.Length + y.Length];
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;