1

I have two scripts, both using the same algorithm to encrypt a string with same key and IV. But result is different. Is openssl_encrypt still using a different padding scheme than node or am I missing something else hee?

Node

const crypto = require('crypto');

var passphrase = '29486a7a37664140';
var iv = '76e69938cdf5bb64';
var text = '1234567890123456';

var cipher = crypto.createCipheriv('aes-128-ctr', passphrase, iv)
var crypted = cipher.update(text,'utf8','hex')
crypted += cipher.final('hex');

var result = crypted + ':' + iv;

console.log('crypted: (' + crypted.length + ' chars)', crypted);
// crypted: (32 chars) b94107e56900ec8270a847bbf457eaa6

PHP

<?php

$encrypt_method = "AES-128-CTR";
$passphrase = '29486a7a37664140';
$iv = '76e69938cdf5bb64';
$text = '1234567890123456';

$encrypted = openssl_encrypt( $text, $encrypt_method, $passphrase, 0, $iv );

echo 'crypted (' . strlen($encrypted) . ' chars): ' . $encrypted;
// crypted (24 chars): uUEH5WkA7IJwqEe79Ffqpg==
?>

1 Answer 1

1

Okay, well… stupid. Encoding issue:

changing my node part to use bas64 works

var cipher = crypto.createCipheriv('aes-128-ctr', passphrase, iv)
var crypted = cipher.update(text,'utf8','base64')
crypted += cipher.final('base64');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.