Cryptography API in X++


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.

Class_Cryptography.xpo

Comments (4)

  1. Developer says:

    Hi, Is there a way to use hardware devices for digitally signing stuff in X++? I need to use smart card to digitally sign files, but I cannot get X++ to work. I'm using the DLL files that are in the AX references, for example System.Security.Cryptography.CspParameters and System.Security.Cryptography.RSACryptoServiceProvider.

  2. michel says:

    Hi,

    how can we develop the this functionality in the form level this means we have a form .In this form the data should be decrypt format and  the Table of the form data should be in Encrypt format.

  3. Paja says:

    Nice test.

    I find the example in the SysEmailParameters form very helpful too.

  4. Cody P says:

    Big thanks, this demonstration of the interops was a life saver.

    Thanks Again.

Skip to main content