using System.Security.Cryptography;
public byte[] Key { get; set;}
public byte[] Iv { get; set;}
using (var myAes = Aes.Create())
public KeyInfo(string key, string iv)
Key = Convert.FromBase64String(key);
Iv = Convert.FromBase64String(iv);
public KeyInfo(byte[] key, byte[] iv)
public class EncryptionService
private readonly KeyInfo _keyInfo;
public EncryptionService(KeyInfo keyInfo = null)
public EncryptionService()
public string Encrypt(string input)
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(input);
var encrypted = EncryptRijndael(toEncryptArray, _keyInfo.Key, _keyInfo.Iv);
public string Decrypt(string cipherText)
byte[] toDecryptArray = hexStrToBytes(cipherText);
var decrypted = DecryptRijndael(toDecryptArray, _keyInfo.Key, _keyInfo.Iv);
public string TryDecrypt(string cipherText)
return this.Decrypt(cipherText);
private string EncryptRijndael(byte[] toEncryptArray, byte[] key, byte[] iv)
RijndaelManaged rDel = new RijndaelManaged();
rDel.Mode = CipherMode.CBC;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
return BytesToHexStr(resultArray);
private string DecryptRijndael(byte[] cipherText, byte[] key, byte[] iv)
RijndaelManaged rDel = new RijndaelManaged();
rDel.Mode = CipherMode.CBC;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(cipherText, 0, cipherText.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
public string DecryptJava(string toDecrypt, string key, string iv)
byte[] toDecryptArray = hexStrToBytes(toDecrypt);
RijndaelManaged rDel = new RijndaelManaged();
rDel.Key = Convert.FromBase64String(key); ;
rDel.Mode = CipherMode.CBC;
rDel.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = rDel.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
return UTF8Encoding.UTF8.GetString(resultArray);
private byte[] getIV(String iv)
byte[] iv1 = Encoding.UTF8.GetBytes(iv);
byte[] iv2 = new byte[16];
System.Array.Copy(iv1, 0, iv2, 0, (iv1.Length > iv2.Length ? iv2.Length : iv1.Length));
private String BytesToHexStr(byte[] bytes)
return BitConverter.ToString(bytes, 0).Replace("-", string.Empty).ToUpper();
private byte[] hexStrToBytes(String str)
int length = str.Length / 2;
byte[] rawData = new byte[length];
for (int i = 0; i < length; i++)
int high = Convert.ToInt32(str.Substring(i * 2, 1), 16);
int low = Convert.ToInt32(str.Substring(i * 2 + 1, 1), 16);
int value = (high << 4) | low;
rawData[i] = (byte)value;
public static void Main()
var _keyInfo = new KeyInfo("yHr+ONeYDFnAj6PvHFr+IRbmjUiZ4PMyE+qIKri+vs0=", "Hxlz5fLyv5KLmTg729grdA==");
var encrypt = new EncryptionService(_keyInfo);
var computedString = "https://sciant-shijidatavaultapi.azurewebsites.net/api/v1/vaults/525M68/tokens|20210126084945666";
Console.WriteLine(encrypt.Encrypt(computedString));
Console.WriteLine("-------------------------------------------------------------------------------------------------------------------------------------------");