java - Spring 4 Encryptors vs cryptojs -
i processing encryption using spring 4 encryptors class way:
string salt = keygenerators.string().generatekey(); textencryptor textencryptor = encryptors.text("my_secret_key", salt); textencryptor.encrypt(json); in client javascript trying decrypt using cryptojs
var uncrypted = cryptojs.aes.decrypt(serverencrypted, "my_secret_key"); $("#encrypted-data").val(uncrypted); i guessing need revert hex encoding part, don't understand needs done.
i tried parsing before decrypting, nothing out of it.
cryptojs.enc.hex.parse(serverencrypted) the spring documentation says text method encrypt in hex value in "standard" encryption method , 256-bit aes using pkcs #5's pbkdf2
[search progress]
this how spring creates key:
pbekeyspec keyspec = new pbekeyspec(password.tochararray(), salt, 1024, 256); secretkey secretkey = newsecretkey("pbkdf2withhmacsha1", keyspec); secretkeyspec secretkey = new secretkeyspec(secretkey.getencoded(), "aes"); and crypting:
cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding"); cipher.init(cipher.encrypt_mode, secretkey, new ivparameterspec(iv)); iv 8 bytes secured random array , prepended encrypted data.
after research, find out solution, how combine spring 4 encryptors , cryptojs. spring use in encryptors 256-bit aes using pkcs #5's pbkdf2 provided in documentation - spring crypto docs iv size equals 16 bytes. info crucial when creating code in cryptojs. thing remember use same salt , iv during whole process of encryption/decryption one message. armed info can start coding (in example encrypt message in cryptojs , decrypt in spring): cryptojs
const keysize = 256; const ivsize = 128; const iterations = 1024; function encrypt(msg, pass) { const salt = cryptojs.lib.wordarray.random(128 / 8); const key = cryptojs.pbkdf2(pass, salt, { keysize: keysize / 32, iterations: iterations }); const iv = cryptojs.lib.wordarray.random(ivsize / 8); const encrypted = cryptojs.aes.encrypt(msg, key, { iv: iv, padding: cryptojs.pad.pkcs7, mode: cryptojs.mode.cbc }); // salt, iv hex 32 in length // append them ciphertext use in decryption return salt.tostring() + iv.tostring() + encrypted.ciphertext.tostring(); } as can see result of encryption concatenation of salt + iv + encryptedmessage. important in spring code found key created 256 length , in 1024 iterations:
pbekeyspec keyspec = new pbekeyspec(password.tochararray(), hex.decode(salt), 1024, 256); now prepared result can take our spring code , decrypt:
private static string decrypt(string encrypteddata, string keystr) { final string salt = encrypteddata.substring(0, 32); final string encryptedpart = encrypteddata.substring(32); final textencryptor textencryptor = encryptors.text(keystr, salt); return textencryptor.decrypt(encryptedpart); } spring aes encryptor cut part iv , use decryption process itself. of course has hex encoded, that's how spring textencrytpors works.
the reverse procedure (encrypt in spring , decrypt in cryptojs) pretty same. in cryptojs have extract salt , iv:
const keysize = 256; function decrypt (encryptedmessage, pass) { var salt = cryptojs.enc.hex.parse(encryptedmessage.substr(0, 32)); var iv = cryptojs.enc.hex.parse(encryptedmessage.substr(32, 32)) var encrypted = encryptedmessage.substring(64); var key = cryptojs.pbkdf2(pass, salt, { keysize: keysize / 32, iterations: iterations }); var decrypted = cryptojs.aes.decrypt(encrypted, key, { iv: iv, padding: cryptojs.pad.pkcs7, mode: cryptojs.mode.cbc }) return decrypted; } i hope :)
cheers!
Comments
Post a Comment