Skip to content

Instantly share code, notes, and snippets.

@artjomb
Created March 14, 2016 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save artjomb/50fcbf9acdfb97e1a3f8 to your computer and use it in GitHub Desktop.
Save artjomb/50fcbf9acdfb97e1a3f8 to your computer and use it in GitHub Desktop.
<?php
$d = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
$k = "k0k1k2k3k4k5k6k7"; // 16 byte AES key
$bs = 16; // 16 byte block size
$iv = mcrypt_create_iv($bs);
$ct = openssl_encrypt($d, "aes-128-ctr", $k, OPENSSL_RAW_DATA, $iv);
$dec_offset = 32;
$ct_slice = substr($ct, $dec_offset);
$iv_slice = add($iv, $dec_offset / $bs);
$d_slice = openssl_decrypt($ct_slice, "aes-128-ctr", $k, OPENSSL_RAW_DATA, $iv_slice);
var_dump($d_slice);
function add($big_num_str, $to_add){
$big_num = str_split(strrev($big_num_str));
for($i = 0; $i < count($big_num) ; $i++){
$tmp = ord($big_num[$i]) + $to_add;
$big_num[$i] = $tmp % 256;
$to_add = floor( $tmp / 256 );
}
while($to_add){
$big_num[$i++] = $to_add % 256;
$to_add = floor( $to_add / 256 );
}
for($i = 0; $i < count($big_num) ; $i++){
$big_num[$i] = chr($big_num[$i]);
}
return strrev(implode('', $big_num) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment