Guest User

Untitled

a guest
Nov 22nd, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. function encryptCfb($v, $k, $iv) {
  2.  
  3. $v = array_values(unpack('N*', $v));
  4. $iv = array_values(unpack('N*', $iv));
  5. $k = array_values(unpack('N*', $k));
  6.  
  7. $cipher = [];
  8.  
  9. //IV ciphering using the 128bits key
  10. list ($v0, $v1) = cipher($iv[0], $iv[1], $k);
  11.  
  12. //Xoring the cipherd block with the first 64bits of data (32bits in V0 and 32 others in V1)
  13. $cipher[0] = $v0 ^ $v[0];
  14. $cipher[1] = $v1 ^ $v[1];
  15.  
  16. //Now ciphering the latest "cipherd" data using the 128bits key
  17. list ($v2, $v3) = cipher($cipher[0], $cipher[1], $k);
  18.  
  19. //Xoring the cipherd block with the second 64bits of data (32bits in V0 and 32 others in V1)
  20. $cipher[2] = $v2 ^ $v[2];
  21. $cipher[3] = $v3 ^ $v[3];
  22.  
  23. $output = "";
  24. foreach ($cipher as $i) {
  25. $output .= pack('N', $i);
  26. }
  27.  
  28. return $output;
  29. }
  30.  
  31. function cipher($v0, $v1, $k) {
  32.  
  33. $delta=0x9e3779b9;
  34. $sum = 0;
  35. $limit = $delta * 32;
  36.  
  37. for ($i=0; $i < 32; $i++) {
  38. $v0 += ((($v1<<4) ^ ($v1>>5)) + $v1) ^ ($sum + $k[$sum & 3]);
  39. $sum += $delta;
  40. $v1 += ((($v0 << 4) ^ ($v0 >> 5)) + $v0) ^ ($sum + $k[($sum>>11) & 3]);
  41. }
  42.  
  43. return [$v0, $v1];
  44. }
  45.  
  46. $cryptModule = mcrypt_module_open('xtea', '', 'ncfb', '');
  47. $iv_size = mcrypt_get_iv_size(MCRYPT_XTEA, MCRYPT_MODE_CFB);
  48. mcrypt_generic_init($cryptModule, $key, $iv);
  49. mcrypt_generic($cryptModule, $data);
Add Comment
Please, Sign In to add comment