简体   繁体   中英

decrypt string in AES using php

I am currently working on encryption and decryption. I have encrypted my api key using https://medium.com/@amitasaurus/encrypting-decrypting-a-string-with-aes-js-1d9efa4d66d7 like below

            var api_key = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
            var d = new Date();
            var n = d.getTime();
            var final_key = api_key+'/'+n;
            var encrypted = CryptoJS.AES.encrypt('encryption', final_key);
            var encrypted_key = encrypted.toString();

and passed the encrypted key to the server side. I used

<?php
$key = pack("H*", "0123456789abcdef0123456789abcdef");
$iv =  pack("H*", "abcdef9876543210abcdef9876543210");
$encrypted = base64_decode('U2FsdGVkX19gHSzwsrc5H9K6rqDYr2E8oYoVNSp8INU=');
$decrypt_string = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $encrypted, MCRYPT_MODE_CBC, $iv);
echo $decrypt_string;

?> for decrypting the encrypted string. When i print decrypted string , it is like this 9Һ دa< 5*| աT ; 놻 V [ } ID- } 硵 Any suggestions to print as decoded string?

mcrypt defaults to zero padding. That means that, no matter what kind of ciphertext and key combination you are using, that the unpadding will not fail . Instead, it just returns invalid, randomized plaintext.

CryptoJS by default uses OpenSSL key derivation from a given password. Your decryption will return randomized plaintext as long as you cannot mimic the final AES key value that is generated by CryptoJS.

Modern modes such as GCM include an authentication tag with the ciphertext so that the validity of the ciphertext / key combination is ensured, or a verification error will be generated. Note that CBC mode is absolutely not secure when directly used for transport mode security.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM