You are not logged in.
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
Unfortunately, I could not decrypt the cipherText from SynCrypto
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
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
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
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
warleyalex, I need to do exact same thing as you did, can you share your code here?
Offline