#1 2016-09-24 12:32:58

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

AES Encrypt in CryptoJS and decrypt in SynCrypto

I'd like to use CryptoJS to perform the encryption and for the life of me I cannot work out why this won't decrypt with SynCrypto.

My knowledge of encryption isn't brilliant but the thing I am noticing is the CryptoJS encrypted ciphertext for the exact same string/key differs every time i perform the encryption in SynCrypto/Delphi. I suspect SynCrypto won't be able to decrypt the cipher if the initialization vector and salt match with SynCrypto, which I don't know how to set this is SynCrypto. Does someone shed some light on this.

CryptoJS:

var e  =  CryptoJS.AES.encrypt("test", 
  '33743b03c28fc783b01119d8b8c6b2564108318d465a2fb4ff319010c4aa6493', 
  '{"iv":"","mode":"CBC","padding":"Pkcs7","keySize":256,"cipher":"aes","salt":""}')
  .toString(); // "U2FsdGVkX180ir2xXrGQJ//1Z8YXhvRCsoZFTscG3A4="

CryptoJS.AES.decrypt(e, 
  '33743b03c28fc783b01119d8b8c6b2564108318d465a2fb4ff319010c4aa6493')
  .toString(CryptoJS.enc.Utf8);  // "test"

SynCrypto:

var
  key : TSHA256Digest;
  aes : TAESCBC;
  s:RawByteString;
begin
  //SynCommons.HexToBin(Pointer(SHA256(my_key)), @key, 32);
  //aes := TAESCBC.Create(key, 256);

  aes := TAESCBC.Create(('33743b03c28fc783b01119d8b8c6b2564108318d465a2fb4ff319010c4aa6493'), 256);
  try
    s := StringToUTF8(edtCrypted.Text); // "pbOMZBqrGzopqhE7vcfClTtRqsqh+C0axvMv6MN8uu4="
    edtDecrypted.Text := UTF8ToString(
      aes.DecryptPKCS7(Base64ToBin(s), true)  // "test"
    );
  finally
    aes.Free;
  end;
end;

Offline

#2 2016-09-24 14:04:46

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: AES Encrypt in CryptoJS and decrypt in SynCrypto

TAES classes expect the keys to be stored as binary digest, so you have to use TAESCBC.Create(key, 256) and key created from your hexa content.

Offline

#3 2016-09-24 22:25:48

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: AES Encrypt in CryptoJS and decrypt in SynCrypto

Unfortunately, I could not decrypt the cipherText from SynCrypto sad

var
  key : TSHA256Digest;
  aes : TAESCBC;
  s    : RawByteString;
begin
  SynCommons.HexToBin(PAnsiChar(
    '33743b03c28fc783b01119d8b8c6b2564108318d465a2fb4ff319010c4aa6493'),@key,sizeof(key));

  aes := TAESCBC.Create(key, 256);
  try
    s := StringToUTF8(edtCrypted.Text);  ----> cipherText from CryptoJS

    edtDecrypted.Text := UTF8ToString(
      aes.DecryptPKCS7(Base64ToBin(s), true)
    );
  finally
    aes.Free;
  end;
end;

I suspect the iv, key and salt should match with the CryptoJS, SynCrypto generates very different output to similar config.

CryptoJS generates similar cipherText
U2FsdGVkX191ODhWOVL5TAGl0gvhK/VqGyyV90UimNg=   // test
U2FsdGVkX1+WwR/zthxXTCId8IE/CiSPmWjhXL/9YGU=    // test
U2FsdGVkX1/upKAyGuoFwDFdz5qrO6izE6TnYnunbYo=    // test
U2FsdGVkX19bNLYeAjWVjLxy0B8D89yW8gzSNEt95dU=   // test

whereas SynCrypto generates different cipherText, such as:

Xb6j9dHzEBqQk49Fnly3jvzRp3yLmeS3G5km5nJi2JM= 
xxVMU/6ytwYKZjuPVeEZP2EB6cEk403OK+WMiQAtvPA=
kpz1uR8oQTJzRIGgln+aplh+jEaJsiBKF/qaLgiQaKw=
AwJNKNx8OH37e7LQhTE2QM3WnpVMY8umnNhNwaLS4r0=
81MSSvWFN91HUeXdoVx8R2Dd0dZgOqFJomRwRIiH8+I=

Offline

#4 2016-09-25 12:25:55

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,534
Website

Re: AES Encrypt in CryptoJS and decrypt in SynCrypto

1) we recommend node-forge instead of CryptoJS
2) You should specify a encoding while calls to a CryptoJS (CryptoJS.enc.Utf8). AFAIR CryptoJS use a Unicode by default

Offline

#5 2016-09-25 14:11:28

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: AES Encrypt in CryptoJS and decrypt in SynCrypto

Note that CryptoJS generates similar strings start the same way, there's a salted prefix base64 encoded
The prefix 'Salted__' prefix is added to encrypted cipher.

U2FsdGVkX191ODhWOVL5TAGl0gvhK/VqGyyV90UimNg=   // test
U2FsdGVkX1+WwR/zthxXTCId8IE/CiSPmWjhXL/9YGU=    // test
U2FsdGVkX1/upKAyGuoFwDFdz5qrO6izE6TnYnunbYo=    // test
U2FsdGVkX19bNLYeAjWVjLxy0B8D89yW8gzSNEt95dU=   // test

I tried to insert the 'Salted__' prefix into SynCrypto to decrypt the cipherText from CryptoJS. No luck.
Anyway, I try the node-Crypto.

Any hint would be appreciated.

Offline

#6 2016-09-26 22:55:26

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: AES Encrypt in CryptoJS and decrypt in SynCrypto

I've been thrown head first into a task which I'm feeling pretty disorientated about, trying to piece it all together...

and finally I got successfuly decrypt an encrypted AES-256 string from CryptoJS using SynCrypto smile

I'm using the tool mORMot/JS/AES to generate the key and IV, I just have to copy 'n paste the key/iv into SynCrypto and you can decrypt the cipherText.

Last edited by warleyalex (2016-09-26 22:56:38)

Offline

#7 2017-12-13 09:49:24

smcha
Member
Registered: 2017-12-13
Posts: 1

Re: AES Encrypt in CryptoJS and decrypt in SynCrypto

warleyalex, I need to do exact same thing as you did, can you share your code here?

Offline

Board footer

Powered by FluxBB