In version 3.0 the class TextBuffer had two methods: Encrypt and Decrypt. For version 4.0 the Encrypt method has been removed, and the Decrypt method renamed to DecryptOld.

The change happened for security reasons.

If you require to encrypt and decrypt strings in Dynamics AX 4.0, you can use the functionality provided in the .Net System.Security.Cryptography namespace.

By doing so, you should be aware of the dangers, including:

  • A key is required to do encryption and decryption. Your encrypted data is never more secure than the key - so avoid storing the key (including hard coding it in X++) instead prompt the user for the key.
  • Use the Encryption algorithm as it is intended. Do not double-encrypt, it may make it easier to break the code.

To help you get going, I've created an X++ Cryptography class. It uses the implementation of the encryption algorithm Rijndael from .Net. The class is attached to this thread.

Here is an example on how to use it:

static void main(Args _args)
{
    Dialog dialog = new Dialog("Cryptography Demo");
    DialogField dfText = dialog.addField(typeid(description), "Text to encrypt");
    DialogField dfKey  = dialog.addField(typeid(description), "Key to encrypt with");
    str encryptedString;
    str decryptedString;    
    if (dialog.run())
    {
        encryptedString = Cryptography::Encrypt(dfText.value(), dfKey.value());
        decryptedString = Cryptography::Decrypt(encryptedString, dfKey.value());        
        info(strfmt("Encrypted string: %1", encryptedString));
        info(strfmt("Decrypted string: %1", decryptedString));
    }

A big thank you to Ivan Medvedev for providing the C# implementation that I rewrote to X++. You can find the original article here - including more security considerations: http://www.dotnetthis.com/Articles/Crypto.htm 

This posting is provided "AS IS" with no warranties, and confers no rights.