Encryption

Encrypting information

In order to pass data between our server and yours we use the Advanced Encryption Standard (AES 256), which is a symmetric encryption algorithm and one of the most secure currently available.

For example, to retrieve the PIN, you must retrieve the encrypted pin data by using the ViewPin API method. After receiving the data, the data is decrypted using AES256 algorithm with the appropriate IV and secret key.

Note: You receive the Encryption/Decryption keys in the response of the Login method as a Security key parameter.

Contis will pass information such as a virtual payment card CVV (security code) in encrypted format. Any encrypted information can be decrypted using a unique key provided to you by Contis.

Encrypt the data

To encrypt the data, use the AES256 algorithm. The C# code (shown below) helps you to encrypt the data. This function is used to encrypt data using the Security Key.

C# code


    public byte[] FromHexString(String hex)
    {
        byte[] bts = new byte[hex.Length / 2];
        for (int i = 0; i < bts.Length; i++)
	    {
		    bts[i] = (byte)Convert.ToInt32(hex.Substring(2 * i, 2), 16);
	    }
        return bts;
    }

    public string Encrypt(string PlainValue, string securityKey)
    {
        byte[] iv = FromHexString(securityKey.Remove(32, 64));
        byte[] key = FromHexString(securityKey.Remove(0, 32));
        return AES256Encrypt(PlainValue, key, iv);
    }

    public string AES256Encrypt(string clearText, byte[] key, byte[] iv)
    {
    try
	    {
		    AesCryptoServiceProvider aesCipher = new AesCryptoServiceProvider();
                    byte[] plainText = System.Text.Encoding.Unicode.GetBytes(clearText);
		    ICryptoTransform encryptor = aesCipher.CreateEncryptor(key, iv);
                    using (MemoryStream msEncrypt = new MemoryStream())
		    {
                   //Defines a stream that links data streams to cryptographic transformations   
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
			    {
				    csEncrypt.Write(plainText, 0, plainText.Length);
                   //Writes the final state and clears the buffer   
				    csEncrypt.FlushFinalBlock();
                    byte[] cipherBytes = msEncrypt.ToArray();
                    string encryptedData = Convert.ToBase64String(cipherBytes);
                    return encryptedData;
			    }
		    }
	    }
        catch (Exception)
	    {
        return string.Empty;
	    }
    }
    

Node.js


    const crypto = require('crypto');
     // Constants
    const text = 123;
    const encryptedText = 'Y953TncvQcu96LO67mYQCg==';
    const key = Buffer.from('e9de8858a76c406eb2cdde4a33f6e1b2', 'hex');
    const iv = Buffer.from('86ee3efccfb94506a7dfcfd04e9720bc46634d7679db40b1afa94cfe2d2f2018', 'hex');

     // Encrypting
    const cipher = crypto.createCipheriv('aes-256-cbc' key, iv);
    let encrypted = cipher.update(
     text.toString(),
    'utf16le',
    'base64'
     );
     encrypted += cipher.final('base64');
     console.log('Encrypted value: ${encrypted}');
     

Example

    
    PlainCard: 4763580507487320 
           
    SecurityKey:  e9de8858a76c406eb2cdde4a33f6e1b286ee3efccfb94506a7dfcfd04e9720bc46634d7679db40b1afa94cfe2d2f2018  
        
    IV: e9de8858a76c406eb2cdde4a33f6e1b2 
         (First 32 character of security key)
        
    KEY: 86ee3efccfb94506a7dfcfd04e9720bc46634d7679db40b1afa94cfe2d2f2018 
        (Last 64 character of security key) 
        
    ENCRYPTED value: c6T1M9hIAKr1K0qGCo7Ft5L4VupBuTbtYdhZ8zXHwAmzF2vREyDyMW/SzhHts0pA
        
    

Note: Use the CBC mode of the AES256 encryption and UTF-16 (Unicode) as the encoding. 

Decrypt the data

To decrypt the data, use the AES256 algorithm. The C# code (shown below) helps you to decrypt the data.

 This function is used to decrypt data using the Security Key


    public byte[] FromHexString(String hex)
        {
        byte[] bts = new byte[hex.Length / 2];
        for (int i = 0; i < bts.Length; i++)
	        {
		        bts[i] = (byte)Convert.ToInt32(hex.Substring(2 * i, 2), 16);
	        }
        return bts;
        }

    public string Decrypt(string PlainValue, string securityKey)
        {
        byte[] iv = FromHexString(securityKey.Remove(32, 64));
        byte[] key = FromHexString(securityKey.Remove(0, 32));
        return AES256Decrypt(PlainValue, key, iv);
        }

    public string AES256Decrypt(string encrpytedText, byte[] key, byte[] iv)
        {
        try
	        {
		    AesCryptoServiceProvider aesCipher = new AesCryptoServiceProvider();
                    byte[] encryptedData = Convert.FromBase64String(encrpytedText);
		    ICryptoTransform decryptor = aesCipher.CreateDecryptor(key, iv);
                    using (MemoryStream msDecrypt = new MemoryStream(encryptedData))
		    {
                   //Defines the cryptographic stream for decryption.The stream contains decrypted data   
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
			{
                    byte[] plainText = new byte[encryptedData.Length];
                    int decryptedCount = csDecrypt.Read(plainText, 0, plainText.Length);
                    string decryptedData = Encoding.Unicode.GetString(plainText, 0, decryptedCount);
                    return decryptedData;
			        }
		        }
	        }
                catch (Exception)
	        {
                return string.Empty;
	        }
        }

Node.js


    const crypto = require('crypto');
    // Constants
    const text = 123;
    const encryptedText = 'Y953TncvQcu96LO67mYQCg==';
    const key = Buffer.from('e9de8858a76c406eb2cdde4a33f6e1b2', 'hex');
    const iv = Buffer.from('86ee3efccfb94506a7dfcfd04e9720bc46634d7679db40b1afa94cfe2d2f2018', 'hex');
    // Decrypting
    const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
    let decrypted = decipher.update(encryptedText, 'base64', 'utf16le');
    decrypted += decipher.final('utf16le');
    console.log('Decrypted value: ${decrypted}');
    

Example

    
    Encrypted value:  c6T1M9hIAKr1K0qGCo7Ft5L4VupBuTbtYdhZ8zXHwAmzF2vREyDyMW/SzhHts0pA
            
    SecurityKey: e9de8858a76c406eb2cdde4a33f6e1b286ee3efccfb94506a7dfcfd04e9720bc46634d7679db40b1afa94cfe2d2f2018  
        
    IV: e9de8858a76c406eb2cdde4a33f6e1b2 
         (First 32 character of security key)
        
    KEY: 86ee3efccfb94506a7dfcfd04e9720bc46634d7679db40b1afa94cfe2d2f2018 
        (Last 64 character of security key) 
        
    Decrypted value: 4763580507487320
    
Note: Use the CBC mode of the AES256 encryption and UTF-16 (Unicode) as the encoding.