using System.Collections.Generic;
using System.Security.Cryptography;
public static void Main()
int Code=DateTime.Now.Millisecond;
Console.WriteLine("Survey config code:"+Code);
Console.WriteLine(DistributionShorteningUrlEncryptV2('A',"91",57,164,Code,null,null ));
Console.WriteLine(DistributionShorteningUrlDecryptV2(DistributionShorteningUrlEncryptV2('A',"91",57,164,Code,null,null )));
Console.WriteLine(DistributionShorteningUrlEncryptV2('A',"91",57,10000,Code,null,null ));
Console.WriteLine(DistributionShorteningUrlDecryptV2(DistributionShorteningUrlEncryptV2('A',"91",57,10000,Code,null,null )));
int Code2=DateTime.Now.Millisecond;
Console.WriteLine("contact code:"+Code2);
Console.WriteLine(DistributionShorteningUrlEncryptV2('I', "91", 57, 164,Code, 590,Code2));
Console.WriteLine(DistributionShorteningUrlDecryptV2(DistributionShorteningUrlEncryptV2('I', "91", 57, 164,Code, 590,Code2)));
Console.WriteLine(DistributionShorteningUrlEncryptV2('I', "91", 57, 10000,Code, 999999,Code2));
Console.WriteLine(DistributionShorteningUrlDecryptV2(DistributionShorteningUrlEncryptV2('I', "91", 57, 10000,Code, 999999,Code2)));
Console.WriteLine(DistributionShorteningUrlDecryptV2("Jpx31yrMRMzmv5sNBmufKTaMAddx2ZtC55Aqr3kwRrBkctLXVS9HkD2v5AeQ"));
static byte[] DistributionShorteningUrlV2Key { get { return Convert.FromBase64String("Z3P0t9jzMxKPIQck6W4P7W1eEeCB97K3oURRHGwG0SE="); } }
class DistributionShorteningUrlV2Payload
public char D { get; set; }
public string Z { get; set; }
public int T { get; set; }
public int I { get; set; }
public int IC { get; set; }
public int? C { get; set; }
public int? CC { get; set; }
private static string Base64UrlEncode(byte[] data)
return Convert.ToBase64String(data)
private static byte[] Base64UrlDecode(string base64Url)
string padded = base64Url.Replace('-', '+').Replace('_', '/');
switch (padded.Length % 4)
case 2: padded += "=="; break;
case 3: padded += "="; break;
case 1: padded += "==="; break;
return Convert.FromBase64String(padded);
private static byte[] SerializeToCompactBytesMinified(DistributionShorteningUrlV2Payload payload)
using var ms = new MemoryStream();
using var writer = new BinaryWriter(ms);
writer.Write((byte)payload.D);
writer.Write((byte)payload.Z.Length);
writer.Write(Encoding.ASCII.GetBytes(payload.Z));
writer.Write(payload.IC);
if (payload.C.HasValue) flags |= 1;
if (payload.CC.HasValue) flags |= 2;
writer.Write(payload.C.Value);
writer.Write(payload.CC.Value);
private static DistributionShorteningUrlV2Payload DeserializeFromCompactBytesMinified(byte[] data)
using var ms = new MemoryStream(data);
using var reader = new BinaryReader(ms);
var payload = new DistributionShorteningUrlV2Payload();
payload.D = (char)reader.ReadByte();
int isdLen = reader.ReadByte();
payload.Z = Encoding.ASCII.GetString(reader.ReadBytes(isdLen));
payload.T = reader.ReadInt32();
payload.I = reader.ReadInt32();
payload.IC = reader.ReadInt32();
var flags = reader.ReadByte();
payload.C = reader.ReadInt32();
payload.CC = reader.ReadInt32();
public static string DistributionShorteningUrlEncryptV2(char UrlPrefixCode, string ISDCode, int TimeZoneId, int SurveyConfigId, int SurveyConfigIdCode, int? DistContactId, int? DistContactIdCode)
byte[] key = DistributionShorteningUrlV2Key;
var payload = new DistributionShorteningUrlV2Payload
var json = Newtonsoft.Json.JsonConvert.SerializeObject(payload);
var plaintextBytes = SerializeToCompactBytesMinified(payload);
using var aes = new AesGcm(key);
byte[] nonce = new byte[12];
RandomNumberGenerator.Fill(nonce);
var ciphertext = new byte[plaintextBytes.Length];
aes.Encrypt(nonce, plaintextBytes, ciphertext, tag);
var result = new byte[nonce.Length + ciphertext.Length + tag.Length];
Buffer.BlockCopy(nonce, 0, result, 0, nonce.Length);
Buffer.BlockCopy(ciphertext, 0, result, nonce.Length, ciphertext.Length);
Buffer.BlockCopy(tag, 0, result, nonce.Length + ciphertext.Length, tag.Length);
return Base64UrlEncode(result);
public static (char DistType, string IsdCode, int TimezoneId, int SurveyConfigId, int SurveyConfigTimeMM, int? ContactId, int? ContactTimeMM) DistributionShorteningUrlDecryptV2(string Code)
byte[] key = DistributionShorteningUrlV2Key;
var data = Base64UrlDecode(Code);
var ciphertext = data[12..^16];
var plaintext = new byte[ciphertext.Length];
using var aes = new AesGcm(key);
aes.Decrypt(nonce, ciphertext, tag, plaintext);
var payload = DeserializeFromCompactBytesMinified(plaintext);
return (payload.D, payload.Z, payload.T, payload.I, payload.IC, (int?)payload.C, payload.CC);