javascript - Why the decrypted key is not same as encrypted key? -


i wrote function encrypt or decrypt word. why decrypted key not same encrypted key?

function cryptage(action, password) {     var method = "aes-256-cbc";     if (action == "encrypt") {         var cipher = crypto.createcipher(method, password)         var crypted = cipher.update(password, 'utf8', 'hex')         crypted += cipher.final('hex');         return crypted;     } else if (action == "decrypt") {         var decipher = crypto.createdecipher(method, password)         decipher.setautopadding(false);         var decrypted = decipher.update(password, 'hex', 'utf8')         decrypted += decipher.final('utf8');         return decrypted;     } } 

when call var crypted = cryptage("encrypt", "test"), returns

cc1fbd73cb93106c3358636ff619bdbd

when call cryptage("decrypt", crypted), returns exactly

te�~��iӠ~{ }m

but no text, why ?

tl;dr => read the fine manual

the nodejs crypto module expects provide 2 things when create either cipher or decipher instance via 2 parameter overload of createcipher/createdecipher: method - name of actual openssl cipher use, , password, fed evp_bytestokey , result keybytes actual cipher instance

a new implementation using should inevitable lead punishment autor now... insecure , should replaced pbkdf2 example... know if you'd have read documentation... anyways ... want know why function not return desired output ... reason simple ...

you provide same value password , data , upon decryption want provide encryption result... can not work

have @ simplified encryption , decryption function/formula

encrypt(data,key) => ciphertext
decrypt(ciphertext,key) => data

this pretty straight forward , valid symetric ciphers (like aes, including modes of operation , keysizes, so... yes... aes 256 bit key in cipher-block-chaining mode)

so what's matter in case? this:
declare key , data same...
encrypt(key,key) => ciphertext
decrypt(ciphertext,ciphertext) => garbage

please note successful decryption key parameter encrypt , decrypt function must same, fails case

so approach mean need same original password decryption key parameter ... yes... need same value want decrypt ... input... decrypt function ...


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -