It's not my strong point the encryption, but I need to translate some python code to PHP. Maybe someone can help me.
The Python code:
import base64
import Crypto.Cipher.AES as AES
from Crypto.Util import Counter
counter = 5
key = bytes("my_secret_key_xx")
ctr=Counter.new(128, initial_value= counter)
ivBytes = bytearray(16)
ivBytes[3] = counter % 256
ivBytes[2] = (counter >> 8) % 256
ivBytes[1] = (counter >> 16) % 256
ivBytes[0] = (counter >> 24) % 256
cipher = AES.new(key, AES.MODE_CTR, IV = bytes(ivBytes), counter=ctr)
print base64.b64encode(ivBytes);
print base64.b64encode(cipher.encrypt(bytes("hola")))
More or less I understand it, is the basic CTR encryption, but with a custom starting counter. But I don't know how to set that custom counter in PHP.
$cipher = MCRYPT_RIJNDAEL_128;
$key = "my_secret_key_xx";
$toEncrypt = "hola";
$counter = 5;
$b = array_reverse(unpack("C*", pack("L", $counter)));
$b = chr($b[0]).chr($b[1]).chr($b[2]).chr($b[3]).str_repeat("\0", 12);
$iv = $b;
$enc_data = mcrypt_encrypt($cipher, $key, $toEncrypt, "ctr", $iv);
echo ("b64Encryted-> ".base64_encode($enc_data))."
";
echo ("b64IV-> ".base64_encode($iv))."
";