Translate

Sunday, September 2, 2012

Encrypt/Decrypt in AX 2012

Encrypt/Decrypt in AX 2012


Create a class, make it has property set to RunOn = Server
                                           OR
Create server static method

and use the below code.

static void Job2(Args _args)
{
    CryptoApi cryptoApi;
    Container cont,cont1;
    ContainerClass cc;
    ;
    
   /* Salt is like a password, While encrypting and descrypting the phrase, the CryptoAPI class has to instantiated with same salt(99999999999). The phrases/words are encrypted & decrypted based on the salt. */
    cryptoApi = new CryptoApi(99999999999);
    cc = new ContainerClass(["test123"]);
    cont = CryptoApi.encrypt(cc.toBlob()); // The encrypt method requires BLOB as a parameter
    cont1 = ContainerClass::blob2Container(CryptoApi.decrypt(cont));
    info(Strfmt("Encrypted:%1",BinData::dataToString(cont)));
    info(con2str(cont1));
}

5 comments:

  1. Hi Mukesh,

    I have written two seperate methods for both Encrypt & Decrypt.

    Encryption is going on correctly, and then storing the encrypted value in a string field in a Table.

    But for decrypting, I have written this piece of code mentioned below:

    CryptoApi cryptoApi = new CryptoApi(12345);

    Container cont;
    container cont1;

    Password password;
    Tablename tableName;

    cont1 = conIns(cont1, 1, BinData::stringToData(tableName::find().UserPassword));

    if (!cont1)
    {
    return '';
    }

    cont = ContainerClass::blob2Container(CryptoApi.decrypt(tinCont));

    return con2Str(cont);

    but seems like it is not working properly and thorwing error "Problem in Encrypt and Decrypt".

    Can you suggest some solutions?
    I am using Ax 2012 version.


    Thanks
    Pranav Gupta

    ReplyDelete
    Replies
    1. Hi Pranav,

      Try such logic (works for me):

      CryptoApi cryptoApi;
      Container cont;
      ContainerClass cc;
      str ret;

      #define.Key(12345)

      cryptoApi = new CryptoApi(#Key);

      if (_password == '')
      return _password;

      if (_encrypt)
      {
      cc = new ContainerClass([_password]);
      cont = cc.toBlob();
      cont = cryptoApi.encrypt(cont);

      ret = BinData::dataToString(cont);

      if (strEndsWith(ret, '\n'))
      ret = strRem(ret, '\n');

      return ret;
      }
      else
      {
      cont = BinData::stringToData(_password);
      cont = ContainerClass::blob2Container(cryptoAPI.decrypt(cont));

      ret = con2Str(cont);
      return ret;
      }

      Delete
  2. Thanks again! You are so right that habits are learned and enforced by social behaiours and so the right social support can be invaluable when changing habits!

    PIC Bonus Singapore

    ReplyDelete
  3. Hi pavel,

    Thanks for your reply, I had already solve the issue with the below logic:

    Now instead of storing the encrypted value in a string field, I am directly storing it in the container type field.

    Now the decryption functionality is working properly.



    Thanks & Regards

    Pranav Gupta

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete

Recent Posts