RSS

Tag Archives: Cryptography in Dynamics AX

Encryption / decryption(Cryptography) Using x++

Many times we have sensitive data that we don’t want to display to the users. One example for this case is the budget amount in the forecast budgets for the employees’ end of service. We can encryption and decryption in AX by using .Net provided classes.

Below is the example for the encryption.

public static str encrypt(str _txtToEncrypt, str _password)

{

// convert from string to byte array

ClrObject txtByteArray = System.Text.Encoding::get_ASCII().GetBytes(_txtToEncrypt);

ClrObject encryptor;

ClrObject cryptoStream;

ClrObject encryptedtxt;

System.String stringEncrypted;

ClrObject pdb = new ClrObject(‘System.Security.Cryptography.PasswordDeriveBytes’ , _password, OTH_Cryptography::passwordNasheet());

// create intance of memory stream

System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

ClrObject streamMode = ClrInterop::parseClrEnum(‘System.Security.Cryptography.CryptoStreamMode’, ‘Write’);

// define the alogorithm you would lime to use,  MD5, DES etch

ClrObject algorithm = System.Security.Cryptography.DES::Create();

// Now set the key and the IV.

algorithm.set_Key(pdb.GetBytes(32));

algorithm.set_IV(pdb.GetBytes(16));

// create intance of CryptoStream

encryptor = algorithm.CreateEncryptor();

cryptoStream = new ClrObject(‘System.Security.Cryptography.CryptoStream’ , memoryStream, encryptor, streamMode);

// Write the data and make it do the encryption

cryptoStream.Write(txtByteArray, 0, txtByteArray.get_Length());

cryptoStream.Close();

encryptedtxt = memoryStream.ToArray();

// convert from byter array to string

stringEncrypted = ClrInterop::staticInvoke(‘System.Convert’, ‘ToBase64String’, encryptedtxt);

return stringEncrypted;

}

public static str decrypt(str _cipherText, str _password)

{

ClrObject cryptoStream;

ClrObject decryptor;

ClrObject decryptedData;

System.String decrryptText;

ClrObject cipherBytes = System.Convert::FromBase64String(_cipherText);

ClrObject pdb = new ClrObject(‘System.Security.Cryptography.PasswordDeriveBytes’ , _password, OTH_Cryptography::passwordNasheet());

System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

ClrObject streamMode = ClrInterop::parseClrEnum(‘System.Security.Cryptography.CryptoStreamMode’, ‘Write’);

ClrObject algorithm = System.Security.Cryptography.DES::Create();

algorithm.set_Key(pdb.GetBytes(32));

algorithm.set_IV(pdb.GetBytes(16));

decryptor = algorithm.CreateDecryptor();

cryptoStream = new ClrObject(‘System.Security.Cryptography.CryptoStream’ , memoryStream, decryptor, streamMode);

// Write the data and make it do the decryption

cryptoStream.Write(cipherBytes, 0, cipherBytes.get_Length());

cryptoStream.Close();

decryptedData = memoryStream.ToArray();

// Now we need to turn the resulting byte array into a string.

decrryptText =  System.Text.Encoding::get_ASCII().GetString(decryptedData);

return decrryptText;

}

 

Tags: , , , , , , , , , , , ,