encrypt in jquery and decrypt in php

Solutions on MaxInterview for encrypt in jquery and decrypt in php by the best coders in the world

showing results for - "encrypt in jquery and decrypt in php"
Giacomo
16 Oct 2020
1function encrypt_decrypt($string, $action = 'encrypt')
2{
3    $encrypt_method = "AES-256-CBC";
4    $secret_key = 'AA74CDCC2BBRT935136HH7B63C27'; // user define private key
5    $secret_iv = '5fgf5HJ5g27'; // user define secret key
6    $key = hash('sha256', $secret_key);
7    $iv = substr(hash('sha256', $secret_iv), 0, 16); // sha256 is hash_hmac_algo
8    if ($action == 'encrypt') {
9        $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
10        $output = base64_encode($output);
11    } else if ($action == 'decrypt') {
12        $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
13    }
14    return $output;
15}
16 
17echo "Your Encrypted password is = ". $pwd = encrypt_decrypt('spaceo', 'encrypt');
18echo "Your Decrypted password is = ". encrypt_decrypt($pwd, 'decrypt');
19
Michelle
14 Jun 2019
1The problem is that in the CryptoJS code a password is used to derive the key and the IV to be used for AES encryption, but mcrypt only uses the key to encrypt/decrypt. This information needs to be passed to php. Since you don't want to transmit the password, you have to derive the key and IV in the same way in php.
2
3The following code derives the key and IV from a password and salt. It is modeled after the code in my answer here (for more information).
4
5function evpKDF($password, $salt, $keySize = 8, $ivSize = 4, $iterations = 1, $hashAlgorithm = "md5") {
6    $targetKeySize = $keySize + $ivSize;
7    $derivedBytes = "";
8    $numberOfDerivedWords = 0;
9    $block = NULL;
10    $hasher = hash_init($hashAlgorithm);
11    while ($numberOfDerivedWords < $targetKeySize) {
12        if ($block != NULL) {
13            hash_update($hasher, $block);
14        }
15        hash_update($hasher, $password);
16        hash_update($hasher, $salt);
17        $block = hash_final($hasher, TRUE);
18        $hasher = hash_init($hashAlgorithm);
19
20        // Iterations
21        for ($i = 1; $i < $iterations; $i++) {
22            hash_update($hasher, $block);
23            $block = hash_final($hasher, TRUE);
24            $hasher = hash_init($hashAlgorithm);
25        }
26
27        $derivedBytes .= substr($block, 0, min(strlen($block), ($targetKeySize - $numberOfDerivedWords) * 4));
28
29        $numberOfDerivedWords += strlen($block)/4;
30    }
31
32    return array(
33        "key" => substr($derivedBytes, 0, $keySize * 4),
34        "iv"  => substr($derivedBytes, $keySize * 4, $ivSize * 4)
35    );
36}
37The salt is generated during encryption in CryptoJS and needs to be sent to php with the ciphertext. Before invoking evpKDF the salt has to be converted to a binary string from hex.
38
39$keyAndIV = evpKDF("Secret Passphrase", hex2bin($saltHex));
40$decryptPassword = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, 
41        $keyAndIV["key"], 
42        hex2bin($cipherTextHex), 
43        MCRYPT_MODE_CBC, 
44        $keyAndIV["iv"]);
45If only encryptedPassword.toString() was sent to the server, then it is necessary to split the salt and actual ciphertext before use. The format is a proprietary OpenSSL-compatible format with the first 8 bytes being "Salted__", the next 8 bytes being the random salt and the rest is the actual ciphertext. Everything together is Base64-encoded.
46
47function decrypt($ciphertext, $password) {
48    $ciphertext = base64_decode($ciphertext);
49    if (substr($ciphertext, 0, 8) != "Salted__") {
50        return false;
51    }
52    $salt = substr($ciphertext, 8, 8);
53    $keyAndIV = evpKDF($password, $salt);
54    $decryptPassword = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, 
55            $keyAndIV["key"], 
56            substr($ciphertext, 16), 
57            MCRYPT_MODE_CBC, 
58            $keyAndIV["iv"]);
59
60    // unpad (PKCS#7)
61    return substr($decryptPassword, 0, strlen($decryptPassword) - ord($decryptPassword[strlen($decryptPassword)-1]));
62}
63The same can be achieved with the OpenSSL extension instead of Mcrypt:
64
65function decrypt($ciphertext, $password) {
66    $ciphertext = base64_decode($ciphertext);
67    if (substr($ciphertext, 0, 8) != "Salted__") {
68        return false;
69    }
70    $salt = substr($ciphertext, 8, 8);
71    $keyAndIV = evpKDF($password, $salt);
72    $decryptPassword = openssl_decrypt(
73            substr($ciphertext, 16), 
74            "aes-256-cbc",
75            $keyAndIV["key"], 
76            OPENSSL_RAW_DATA, // base64 was already decoded
77            $keyAndIV["iv"]);
78
79    return $decryptPassword;
80}
queries leading to this page
encryption and decryption password in phpphp encrypt and decrypt ctrhow to encrypt and decrypt message in phphow to encrypt and decrypt password in php using hashhow to make encryption in php string and decryptcrypt php decryptphp decrypt aes 2f 2fdecrypt function function decryptthis 28 24data 29 7b phpbcrypt encryption phpencrypt and decrypt phpphp decrypt string encrypted in codeinger 4crypt 3a 3adecrypt phpencrypt and decrypt in jqueryencrypt and decrypt function in phpdecrypt data phpencrypt value in phpphp encrypt and decrypt datahow does php deal with incorect cipher keysphp encrypt encryptphp encrypt and decrypt textpassword decrypt phphow encrypt and decrypt password in phpphp function to encrypt and decryptciphertext in function doesn 27t work but out of function does phpencryption phphash encrypt in php decrypt in jsencryption and decryption in php examplecrypt and decrypt url to 32 characters phpphp script to encrypt an inputencryption and decryption phpandroid encrypt decrypt in php encrypted datahow to decrypt in phpsimple encrypt and decrypt in phpcrypt and decrypt phphow to decrypt in all input data function in phpphp encrypt decrypt textencrypt and decrypt get values in phpsimple php encrypt decrypthow to decrypt encrypted php codephp encryption and python decryption codephp encrypt decrypt exampleencrypt in jquery and decrypt in phpencryption in phpphp encryption decryptiondecrypt password com encrypt phpphp encrypt decrypt for urlhow to encrypt and decrypt strings in phpjavascript encrypt and php decryptphp encrypt decrypt string simplehow to encrypt in phpencrypt and decrypt password in php mysqlphp encrypt decrypt with keyphp encrypt and decrypt with public keyphp encrypt and decrypt functionjsencrypt decrypt with public key in phpphp ecrypt and decrypt passwordsencripted to text in phphow to encrypt decrypt string phphow to encrypt and decrypt id in phphow to encrypt and decrypt password in phpencrypt 2fdecrypt data phppass decryptor in phphow to decrypt a string in php encryptiondecrypt encrypted file phpphp crypt to stringphp decrypt and encrypt stringandroid php encrypt decryptfast random encryption in phpphp encrypt decrypt with saltphp encrypt decrypt functionphp openssl encrypt decrypt stringencrypt phpencrypt data on javascript and decrypt it on phpohow to encrypt and decrypt string in phpdecrypt php passwordencryption and decryption of data in phpsah1 decrypt in phpcrypt decrypt from js to phpencrypt in php decrypt nodejsencrypt data with javascript and decrypt data with phpbest encrypt decrypt phpencrypt and decrypt file phpfunction encrypt decrypt phpencrypt and decrypt id in phphow to incrypt decrypt in phpphp encrypt decrypt javascriptphp crypt function decryptphp openssl decrypt 28 29php encrypt and decrypt back to texthow to encrypt data php and automatically decrypts themmake an encryption and decryption in phpphp encrypt decrypt strong easy php encryption decryptionhow to use encrypt and decrypt method in php with ajaxphp encrypt message crypt 28 29 decryptcore php password encryption and decryptionword encrypter and decrypter phpphp encrypt and decryptphp encrypt and decrypt functionsphp file decryptphp crypt decryptsecret key encrypt and decrypt in phpencryptit phpencrypt decrypt password phpphp encrypt decrypt lengthaes encrypt decrypt in php with saltphp encrypt decryptphp text encrypt decryptencrypting and decrypting in phpencrypt decrypt php classpgp encrpt decryptsimple encrypt decrypt hash protect phpmake custom encrypt decrypt in phpencrypted to plain text in not working phphow to securely encrypt and decrypt in phphash decrypt phpencrypt a string using decrypt key in phpopenssl encrypt and decrypt phpencrypt and decrypt data in php php hash encrypt and decrypt phpmcrypt decrypt phphow decrypt php encrypted string in nodejsencrypt in php and decrypt in jqueryencrypt decrypt file phpencrypt decrypt php javascriptphp online decryptphp strong encryptions for stringsdecrypt phpphp encrypterphp openssl encrypt decrypt exampleencrypt and decryp using cbc in phpencrypt and decrypt password in core phpjavascript encrypt php decrypthow to encrypt and decrypt integer in phpencrypt in php and decrypt in jsencrypt decrypt id in phpphp encryption and decryption codeencrypt and decrypt string in phphow to decrypt in php input data usingphp openssl encrypt and decrypt examplepassword encrypt and decrypt phpphp file encryption and decryptionpassword encryption and decryption in phpencrypt in c 23 and decrypt in phpencrypt decrypt phpphp password encrypt and decryptrijndael 256 encrypt 2fdecrypt between php 3fphp basic encryption decryption functionencrypt using jquery and decypt using phpencrypt decrypt php onlinedecrypt crypt function in phpphp encrypt valuephp encrypt and decrypt query stringbcrypt encrypt and decrypt php aes encryption and decryption in phpphp how to encrypt text with a salthow to decrypt php passwordphp encrypt and decrypt arraycrypto encrypt and decrypt in phpphp best encryption and decryptionencrypt data and decrypt data using phpphp aes encrypt 2f decryptdecrypt crypt function phphash encrypt decrypt phpencrypt variable in jquery decrypt with phpencrypt and decrypt a string in phpdecrypt crypt password phpencrypt with javascript decrypt with phpencrypt and decrypt using phphow to encryot decrypt php with keywhat other methods can i use to encrypt and decrypt a string using phphow to decrypt an encrypted code phpcrypt decrypt string phpopenssl encrypt java decrypt phpopenssl decrypt phpphp simple encrypt and decrypthow to decrypt phpencryption and decryption in php class phphow to decrypt php hashphp mcrypt decryptphp encrypt decrypt vs rceopenssl encrypt decrypt phpaes 256 php save encryption key as textphp simple 10 encrypt and decryptphp encrypt decrypt stringphp encrypt and decrypt fuctionshow to encrypt and decrypt password in php mysqlphp encrypt decrypt integerencrypt decrypt password phpencrypt and decrypt text messages in pure php codehow to encrypt and decrypt in phpdecode and encode messages decrypt and encrypt phpencrypt decrypt php 7encrypt decrypt from js to phpjsencrypt php decryptstr encrypt phpdecrypt with phpdecrypt to encrypt php onlinebcrypt decrypt phpaes decryption in phpphp response encrypted payload how to decryptencrypt in js and decrypt in phpphp crypt 26 decrypt stringphp decryptpassword encrypt decrypt phpencrypt php decrypt javascriptphp code for aes encryption and decryptionhow to hash and unhash a string in phphow to encrypt and decrypt data using pgp javascriptphp mysql encrypt decrypt dataencrypt password phphow to encrypt decrypt in phpencrypy messages phpsalt based encrypt and decrypt using phpencryption decryption in phpphp encrypt thr javascript codeencrypt and decrypt php 7encrypt decrypt text phpencryption and decryption algorithm in phpphp string encryption and decryptionsimple js php encryptphp encrypt 2fdecryptphp encrypt 2fdecrypt with saltopenssl encrypt and decrypt string phpencrypt and decrypt number in phpencrypt and decrypt c 23 phpciphertext in fucntion doesnt work but out fo fucmtion does phprijndael 256 cbc 7 paddingencrypt string in php and decrypt in javascriptencrypt and decrypt password in phpencrypt javascript decrypt phpcrypt encrypt phphow to encrypt and decrypt a password in phpphp decrypt string with keydecrypt encrypt phpjava encrypt decrypt in phpphp encrypt and decrypt passworddecrypt 28 29 phpall php crypt decryptcore php password encryption and decryption codedecrypt php codephp rijndael encrypt 2fdecryptencrypt with php decrypt with javascriptphp to javascript string encryption decryptionsecure way of encrypt 2fdecrypt password in phpssl encryption php exampleopenssl decrypt phpencrypt in php and decrypt in javascript using keyopenssl encrypt phpdecrypt php code onlinephp two way encrypt decryptphp encode with keyphp ecrypt and decryptjs ecrypt php decryptencrypt decrypt in phpencrypt in php and decrypt in c 23encrypt decrypt function in phpencryption in javascript and decryption in phppassword decryption in phpbcrypt encrypt decrypt phphow to make text encrypt and decrypt phpencrypt decryt phpsimple encryption and decryption in phpphp encrypt decrypt filesencrypt and decrypt password phpphp how to encrypt and decrypt dataphp encrypt decrypt tutorialjs encrypt php decryptencrypt in php and decrypt in phpopenssl decrypt from php php code encrypt decryptphp encrypt decrypt passwordencrypt in javascript decrypt in phpphp simple password encrypt decryptphp encrypt decrypt intencryption and decryption in phpencryption decryption phpencrypting and decrypting php php encrypt decryptdecrypt php functionhow to re encrypt phpencrypt phpphp in encript by text not workingphp encrypt and decrypt an objectencrypt decrypt 28php code to encrypt and decrypt passwordjs encrypt php decrypt rsaencrypting and decrypting data in phpphp encrypt decrypt idphp encrypt message encrypt 28 29 decryptphp7 encrypt decrypt stringhow to encrypt and decrypt a php string 3fphp encrypt decrypt string with keyencryption and decryption php projectphp crypt encrypt decrypthow to encrypt and decrypt a string in phpphp mcrypt decrypt examplephp encrypt and decrypt stringphp simple encrypt and decrypt algorithmhow to aes encrypted value in php and decrypt with cryptojswhat is the best way to encrypt and decrypt password in phpmessage encrypt and decrypt in phpphp encryptstring with key decrypt in nodjesencrypt in javascript and decode in phpencrypt in php and decrypt in javaencrypt decrypt url phpdecrypt and encypt in phphow to do encryotion and decryption in phpaes encryption phpphp encryptedrijndael encryption php 128 by 16dcrypt and encrypt in php 24ciphertext 3d base64 encode 28 24ciphertext 29 3b add salted hash with this in phpcara encrypt decrypt phpdecrypt and encrypt php5 php decryptmake encrypted of length 32 phpphp how to decrypt passwordphp encrypt aes and java decryptencrypt in javascript and decrypt in phpdecrypt php function simplephp encrypt stringreplace php encrypt decryptencrypt password in windows form 2c decrypt in phpcrypto js decrypt in phphow to encrypt password and then decrypt password in phpphp encryption and decryptionphp hash encrypt decryptencrypt with java and decrypt with phpphp string encryption and decryption 3fencrypt text like phptools to perform aes 256 encryption phpmcrypt encrypt php 7hpw decrypt encrypted password in phpphp code encrypt to decrypt onlinedecrypt function in phpdecrypt crypt 28 29 in phpsimple encrypt decrypt phpwhen i decrypt php it gives me something elsedecrypt php encrypted data in nodejsphp simple encryption decryptionphp password decrypthow to make a php encryption and decryption codeencrypt decrypt phpjavascript encryption decryption to phphow to encrypt decrypt data in phpaes ciphertext decryption information in php with iv and salt lengthencrypt and decrypt file php libraryphp encrypt decrypt onlineencrypt file in folder php and decryptencrypt data using jquery and decode in phpphp password encryption and decryptionphp simple encrypt decrypthow to encrypt and decrypt variable in phpjs sha256 encrypt and decrypt in phpphp encryptionencrypted decrypt in phpphp encrypthow to encrypt and decrypt password phpsimple string encrypt decrupt phpdecrypt in phpencrypt and decrypt in phpfunction encrypt decrypt password phpencrpt decrypt phpphp mcrypt encrypt php 7how to encrypt string and decrypt string in phphow to encrypt and decrypt string in phppassword encrypt and decrypt in phpencrypt in jquery and decrypt in php