Search code examples
javascriptphpopensslaescryptojs

I want to decrypt the text that in encrypted in php using openssl_encrypt


Php encrypt function is like

 public function encrypt($value)
    {

        $key ="ATob9yt5i9ajBPaw2GkTm8QfkCG9sUnW9cn0ndZmulK=";
        $iv = chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0)
            . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0)
            . chr(0x0) . chr(0x0) . chr(0x0) . chr(0x0);
        $d = base64_decode($key);

        $value = \openssl_encrypt($value,
            $this->cipher, $d  , 0,$iv
        );

        return $value;
    }

I have no idea how to decrypt this in js. I'm currently using CryptoJs. The most confusing part is the iv and base64_decode($key).

My current implementation in js is like below

function encrypt(value){

    let key = new Buffer("ATob9yt5i9ajBPaw2GkTm8QfkCG9sUnW9cn0ndZmulK=", 'base64');
    let iv = CryptoJS.enc.Utf8.parse(String.fromCharCode(0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0));

    let encrypted = CryptoJS.AES.encrypt(value,CryptoJS.enc.Utf8.parse(key.toString()), {
        iv:iv,
        mode: CryptoJS.mode.CBC,
    });
}

I'm so confuse with iv key and decoding the key in base64.


Solution

  • The problem is key. It will be resolved if you parse the key with CryptoJS.enc.Base64.parse(key). Base64.parse will transform the key into word array that is what CryptoJS accept as input.