NEIEP  NEIEP Online Login  

NEIEP API Encryption

When a NEIEP API Webservice requires an Encrypted parameter, we require AES-256bit Encryption with the IV value prepended to the encrypted data.

You must use the Encryption Key that NEIEP provided to you.

The example code below shows how to create the encrypted value.

C#

public static string EncryptString(string companyKey, string plainText)
{
    // This uses a random IV to encrypt and includes the IV prepended to the encrypted value that is returned

    byte[] encryptedResult;
    byte[] iv;

    using (Aes aes = Aes.Create())
    {
        aes.Key = Encoding.UTF8.GetBytes(companyKey);
        aes.GenerateIV();

        iv = aes.IV;

        ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);

        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, encryptor, CryptoStreamMode.Write))
            {
                using (StreamWriter streamWriter = new StreamWriter((Stream)cryptoStream))
                {
                    streamWriter.Write(plainText);
                }
                encryptedResult = memoryStream.ToArray();
            }
        }
    }

    return Convert.ToBase64String(iv) + Convert.ToBase64String(encryptedResult);
}