using System.Collections.Generic;
using System.Globalization;
using System.IdentityModel.Tokens.Jwt;
using System.IO.Compression;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using Microsoft.AspNetCore.Http;
using Microsoft.Identity.Client;
using Microsoft.IdentityModel.Tokens;
private const string secretKey = "046A0CAED97541FD954BEEBB53F459F5";
public static void Main()
Console.WriteLine(DecipherToken("W2MguP/QU6JR3widVObUL3ZOuLuQ6P5eH8LRmQcCaNfi5140EqV1fHmi5MbM35ciR1WUEfntVygTaW5rkxXhLnOHlTaWf83ILcZcNMPjBf9ARDr9KbsR2UlFLGdFYtfNxC7LDRdPORSyjV5iOXdrxZsYL1VfNiDbrBecIK6TcHN5FRTOEJDIamTwTb9bbj++gloB7PiyqRjnDtX4ZtTZ0F7fMfn82xbxy2GgIuU2ohE+h4HC7EGYOdVN6eJ2plY+dg7MrNCCr8KkbtEZ5SVgZRSnd+8h/mlu2Y4P+d7+XEgd5R4gXH5b65FDNVyOgRfcbmy3pNKNzYCuHjh1qvPZbkjLTOJ7MzxGulCRPpkDV4AFZplc3joQikRfSgu8ho+WjM5RURj7UuT25va6QH5jHXMcDb/b0EKuHdClQ1Pndm90BoYnsRFEm/yWi92/TFAxH2LYwiN3Oo4KLNHZsYhUSw=="));
public static string CipherToken(string token)
byte[] iv = new byte[16];
var zipped = ZipFileHelper.Zip(token);
string tokenZipped = Convert.ToBase64String(zipped);
using (Aes aes = Aes.Create())
aes.Key = Encoding.UTF8.GetBytes(secretKey);
ICryptoTransform encryptor = aes.CreateEncryptor(
using (MemoryStream memoryStream = new MemoryStream())
using (CryptoStream cryptoStream = new CryptoStream(
using (StreamWriter streamWriter = new StreamWriter(cryptoStream))
streamWriter.Write(tokenZipped);
array = memoryStream.ToArray();
string tokenCiphered = Convert.ToBase64String(array);
public static string DecipherToken(string cipheredToken)
byte[] iv = new byte[16];
byte[] buffer = Convert.FromBase64String(cipheredToken);
using (Aes aes = Aes.Create())
aes.Key = Encoding.UTF8.GetBytes(secretKey);
ICryptoTransform decryptor = aes.CreateDecryptor(
using (MemoryStream memoryStream = new MemoryStream(buffer))
using (CryptoStream cryptoStream = new CryptoStream(
using (StreamReader streamReader = new StreamReader(cryptoStream))
var decipheredToken = streamReader.ReadToEnd();
var bytes = Convert.FromBase64String(decipheredToken);
var unzippedToken = ZipFileHelper.Unzip(bytes);
public class ZipFileHelper : IDisposable
private ZipArchive zipArchive;
public ZipFileHelper(Stream stream)
this.zipArchive = new ZipArchive(
Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage));
public ZipFileHelper(byte[] byteContent) :
this(new MemoryStream(byteContent))
public static bool IsEmpty(string content)
byte[] byteContent = Encoding.Default.GetBytes(content);
return IsEmpty(byteContent);
public static bool IsEmpty(byte[] byteContent)
return IsEmpty(new MemoryStream(byteContent));
private static bool IsEmpty(Stream stream)
using (ZipArchive zip = new ZipArchive(
Encoding.GetEncoding(CultureInfo.CurrentCulture.TextInfo.OEMCodePage)))
ZipArchiveEntry zipArchiveEntry = zip.Entries.FirstOrDefault();
return zipArchiveEntry == null;
public IEnumerable<ZipArchiveEntry> GetContents()
return this.zipArchive.Entries;
public static byte[] Zip(string textToZip)
using (var memoryStream = new MemoryStream())
using (var zipArchived = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
var demoFile = zipArchived.CreateEntry("zipped.txt");
using (var entryStream = demoFile.Open())
using (var streamWriter = new StreamWriter(entryStream))
streamWriter.Write(textToZip);
return memoryStream.ToArray();
public static string Unzip(byte[] zippedBuffer)
using (var zippedStream = new MemoryStream(zippedBuffer))
using (var archive = new ZipArchive(zippedStream))
var entry = archive.Entries.FirstOrDefault();
using (var unzippedEntryStream = entry.Open())
using (var ms = new MemoryStream())
unzippedEntryStream.CopyTo(ms);
var unzippedArray = ms.ToArray();
return Encoding.Default.GetString(unzippedArray);
if (this.zipArchive != null)
this.zipArchive.Dispose();