Skip to content

Encryption

All communication between AnnA and the Client travels with 3DES symmetric encryption using CBC (Cipher Block Chaining) mode with block size of 64 and key size of 192 bytes

Below we have some examples on how to implement the methods of encryption and decryption:

public static string Encrypt3DES(string data, string key, string iv)
{
byte[] bytes = Encoding.UTF8.GetBytes(data);
byte[] decodedKey = Convert.FromBase64String(key);
byte[] decodedIv = Convert.FromBase64String(iv);
var cryptoServiceProvider = new TripleDESCryptoServiceProvider()
{
Key = decodedKey,
IV = decodedIv,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
byte[] encrypted = cryptoServiceProvider.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length);
cryptoServiceProvider.Clear();
return Convert.ToBase64String(encrypted);
}
public static string Decrypt3DES(string data, string key, string iv)
{
byte[] encryptedBytes = Convert.FromBase64String(data);
byte[] decodedKey = Convert.FromBase64String(key);
byte[] decodedIv = Convert.FromBase64String(iv);
var cryptoServiceProvider = new TripleDESCryptoServiceProvider()
{
Key = decodedKey,
IV = decodedIv,
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7
};
byte[] bytes = cryptoServiceProvider.CreateDecryptor().TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
cryptoServiceProvider.Clear();
return Encoding.UTF8.GetString(bytes);
}
public static string GenerateIV()
{
var cryptoServiceProvider = new TripleDESCryptoServiceProvider();
cryptoServiceProvider.GenerateIV();
return Convert.ToBase64String(cryptoServiceProvider.IV);
}