using System.Security.Cryptography;
public static void Main()
Console.WriteLine(TokenClaimsFactory.Create("9223372034854775845", "502a9211-392e-4993-8d7e-31734ae2ee7b"));
public static class TokenClaimsFactory
public static string Create(string tenantId, string vesselId)
return new CryptoLib(LiveKeys.KEY, LiveKeys.IV).Encrypt(string.Format("{0};{1}", tenantId, vesselId));
public static string Create(string tenantId)
return new CryptoLib(LiveKeys.KEY, LiveKeys.IV).Encrypt(string.Format("{0};{1}", tenantId, Guid.Empty));
public CryptoLib(string keyText, string ivText)
key = Convert.FromBase64String(keyText);
IV = Convert.FromBase64String(ivText);
public CryptoLib(byte[] key, byte[] IV)
public string Encrypt(string plainText)
var bytes = EncryptStringToBytes(plainText, key, IV);
return Convert.ToBase64String(bytes);
private static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
using (RijndaelManaged rijAlg = new RijndaelManaged())
ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
swEncrypt.Write(plainText);
encrypted = msEncrypt.ToArray();
public string Decrypt(string cipherText)
var bytes = Convert.FromBase64String(cipherText);
return DecryptStringFromBytes(bytes, key, IV);
private string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
using (RijndaelManaged rijAlg = new RijndaelManaged())
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
plaintext = srDecrypt.ReadToEnd();
public static class LiveKeys
public static string KEY = "jFlMaLZr4edBGfBc9roIsLbycXHSt59estUQWM3ky1y=";
public static string IV = "rVEgRypocle9TI9bCALIVE==";