Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function encryptCfb($v, $k, $iv) {
- $v = array_values(unpack('N*', $v));
- $iv = array_values(unpack('N*', $iv));
- $k = array_values(unpack('N*', $k));
- $cipher = [];
- //IV ciphering using the 128bits key
- list ($v0, $v1) = cipher($iv[0], $iv[1], $k);
- //Xoring the cipherd block with the first 64bits of data (32bits in V0 and 32 others in V1)
- $cipher[0] = $v0 ^ $v[0];
- $cipher[1] = $v1 ^ $v[1];
- //Now ciphering the latest "cipherd" data using the 128bits key
- list ($v2, $v3) = cipher($cipher[0], $cipher[1], $k);
- //Xoring the cipherd block with the second 64bits of data (32bits in V0 and 32 others in V1)
- $cipher[2] = $v2 ^ $v[2];
- $cipher[3] = $v3 ^ $v[3];
- $output = "";
- foreach ($cipher as $i) {
- $output .= pack('N', $i);
- }
- return $output;
- }
- function cipher($v0, $v1, $k) {
- $delta=0x9e3779b9;
- $sum = 0;
- $limit = $delta * 32;
- for ($i=0; $i < 32; $i++) {
- $v0 += ((($v1<<4) ^ ($v1>>5)) + $v1) ^ ($sum + $k[$sum & 3]);
- $sum += $delta;
- $v1 += ((($v0 << 4) ^ ($v0 >> 5)) + $v0) ^ ($sum + $k[($sum>>11) & 3]);
- }
- return [$v0, $v1];
- }
- $cryptModule = mcrypt_module_open('xtea', '', 'ncfb', '');
- $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_CFB);
- mcrypt_generic_init($cryptModule, $key, $iv);
- mcrypt_generic($cryptModule, $data);
Add Comment
Please, Sign In to add comment