I've a legacy php
code that encrypts with TripleDES
method as a part of a bank flow to encrypt purchase operations. This is the code:
/****** 3DES Function ******/
function encrypt_3DES($message, $key){
$l = ceil(strlen($message) / 8) * 8;
return substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
}
I want to reproduce the same code in javascript
using CriptoJS's TripleDES. This is my trial so far:
<script src=https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.2/rollups/tripledes.js></script>
<script>
function encrypt_3DES(message, key) {
// Parse input parameters
message = CryptoJS.enc.Utf8.parse(message);
key = CryptoJS.enc.Utf8.parse(key);
// Build an iv with zero values
let iv = CryptoJS.lib.WordArray.create(64/8);
let encrypted = CryptoJS.TripleDES.encrypt(message, key, {iv: iv});
// Encrypt with TripleDES
return encrypted.toString();
}
let message = "testMessage";
let key = "testKey";
let encrypted = encrypt_3DES(message, key);
// encrypted: "HLu4p18KtFfy5VjwjFkDHA=="
</script>
Taking the example values, the result is the string "HLu4p18KtFfy5VjwjFkDHA=="
.
The problem is that the results between js
and php
are slightly different, and my suspictions are pointing to the substr
operation done on the php side, because when I'm converting the result of the open_ssl
operation (without the substring), my javascript code is returning the same base64
string:
<?php
/****** 3DES Function ******/
function encrypt_3DES($message, $key)
{
$l = ceil(strlen($message) / 8) * 8;
return substr(openssl_encrypt($message . str_repeat("\0", $l - strlen($message)), 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"), 0, $l);
}
$message = "testMessage";
$key = "testKey";
$res = base64_encode(encrypt_3DES($message, $key));
$res2 = base64_encode(openssl_encrypt($message, 'des-ede3-cbc', $key, OPENSSL_RAW_DATA, "\0\0\0\0\0\0\0\0"));
/* $res: HLu4p18KtFfTr47KvI/WEw== (My goal is to get this in javascript) */
/* $res2: HLu4p18KtFfy5VjwjFkDHA== (without the substr operation, the results match) */
?>
So, my question is: Is there a way to apply the same substring to the encrypted result of the javascript's CryptoJS operation?