Hello i’m trying to decrypt a base64 string with php and i can’t figure it out.
I am able to decrypt it with JavaScript using CryptoJS with the code below
var data = 'encrypted_url'; var key = "my_token"; function decryptByDES(cipherTextString, keyString) { var keyHex = CryptoJS.enc.Utf8.parse(keyString); var decrypted = CryptoJS.DES.decrypt({ ciphertext: CryptoJS.enc.Base64.parse(cipherTextString) }, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); return decrypted.toString(CryptoJS.enc.Utf8); } console.log(decryptByDES(data, key);
What is the equivalent of this code in PHP?
Advertisement
Answer
To decode and decode from base64 a string can use base64_encode() :
<?php $str = 'This is an encoded string'; echo base64_encode($str); ?>
and now to decode:
<?php $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='; echo base64_decode($str); ?>
Now can apply this with any function.
Now this function could do the same using php 7.1:
<?php $str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw=='; $key = '12345678'; function decryptByDES($str, $key) { $str = base64_decode($str); $key = base64_decode($key); $str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB); $str = str_replace("x0", '', $str); return $str; } ?>