fork download
  1. <?php
  2.  
  3. // Define the Polynomial class
  4. class Polynomial {
  5. public $coefficients;
  6.  
  7. public function __construct($coefficients) {
  8. $this->coefficients = is_array($coefficients) ? $coefficients : [];
  9. }
  10.  
  11. public function order() {
  12. return count($this->coefficients) - 1;
  13. }
  14.  
  15. public function evaluate($x) {
  16. $result = 0;
  17. foreach ($this->coefficients as $power => $coefficient) {
  18. $result += $coefficient * pow($x, $power);
  19. }
  20. return $result;
  21. }
  22. }
  23.  
  24. // Define the LagrangeInterpolation class
  25. class LagrangeInterpolation {
  26. private $x;
  27. private $y;
  28.  
  29. public function __construct($x, $y) {
  30. $this->x = $x;
  31. $this->y = $y;
  32. }
  33.  
  34. public function evaluate($x) {
  35. $n = count($this->x);
  36. $result = 0;
  37. for ($i = 0; $i < $n; $i++) {
  38. $term = $this->y[$i];
  39. for ($j = 0; $j < $n; $j++) {
  40. if ($j != $i) {
  41. $term *= ($x - $this->x[$j]) / ($this->x[$i] - $this->x[$j]);
  42. }
  43. }
  44. $result += $term;
  45. }
  46. return $result;
  47. }
  48. }
  49.  
  50. function root_finding_initial_values($f) {
  51. // Sample coefficients for Polynomial for demonstration
  52. $poly1 = new Polynomial([1, -1]); // Example polynomial x - 1
  53. $degree1 = $poly1->order();
  54.  
  55. if (1 <= $degree1 && $degree1 < 10) {
  56. $nth_root = $degree1 / 2;
  57. } elseif (10 <= $degree1 && $degree1 <= 15) {
  58. $nth_root = $degree1 / 3;
  59. } else {
  60. $nth_root = $degree1;
  61. }
  62.  
  63. $constant = abs($poly1->evaluate(0));
  64. $constant_nth_root = pow($constant, 1 / $nth_root);
  65. $initial_one_false_position = -round($constant_nth_root);
  66. $initial_two_false_position = round($constant_nth_root);
  67.  
  68. return [$initial_one_false_position, $initial_two_false_position];
  69. }
  70.  
  71. function hybrid_method($f, $a, $b, $tol=1e-6, $max_iter=100000) {
  72. $start_time = time();
  73. $a1 = $a;
  74. $a2 = $a;
  75. $b1 = $b;
  76. $b2 = $b;
  77. for ($i = 0; $i < $max_iter; $i++) {
  78. $xT1 = ($b + 2*$a) / 3;
  79. $xT2 = (2*$b + $a) / 3;
  80. $fa = $f($a);
  81. $fb = $f($b);
  82.  
  83. if ($fb - $fa == 0) {
  84. throw new Exception("Division by zero in false position calculation");
  85. }
  86.  
  87. $xF = $a - ($fa * ($b - $a)) / ($fb - $fa);
  88. $x = $xT1;
  89.  
  90. if (abs($f($xT2)) < abs($f($x))) {
  91. $x = $xT2;
  92. }
  93. if (abs($f($xF)) < abs($f($x))) {
  94. $x = $xF;
  95. }
  96. if (abs($f($x)) <= $tol) {
  97. $end_time = time();
  98. $execution_time = $end_time - $start_time;
  99. return [$x, $f($x), $i, $execution_time, $a, $b];
  100. }
  101.  
  102. if ($fa * $f($xT1) < 0) {
  103. $b1 = $xT1;
  104. } elseif ($f($xT1) * $f($xT2) < 0) {
  105. $a1 = $xT1;
  106. $b1 = $xT2;
  107. } else {
  108. $a1 = $xT2;
  109. }
  110. if ($fa * $f($xF) < 0) {
  111. $b2 = $xF;
  112. } else {
  113. $a2 = $xF;
  114. }
  115. $a = max($a1, $a2);
  116. $b = min($b1, $b2);
  117. }
  118. throw new Exception("Hybrid method did not converge within the maximum number of iterations");
  119. }
  120.  
  121. // Generate random text of a specific length
  122. $length = 80;
  123. $random_text = '';
  124. $characters = array_merge(range('a', 'z'), range('A', 'Z'), range(0, 9), str_split('!@#$%^&*()'));
  125. for ($i = 0; $i < $length; $i++) {
  126. $random_text .= $characters[array_rand($characters)];
  127. }
  128.  
  129. // Encode the plaintext using UTF-8 encoding
  130. $plain_bytes = utf8_encode($random_text);
  131.  
  132. // Convert the bytes to a list of integers representing the individual byte values
  133. $digits = array_map('ord', str_split($plain_bytes));
  134.  
  135. // Randomly select an odd degree between 1 and 9
  136. $degree = rand(1, 9);
  137.  
  138. // Generate randomised polynomial through Lagrange Interpolation
  139. $x = range(1, $degree + 2); // x values for interpolation
  140. $y = array_slice($digits, 0, $degree + 2); // y values for interpolation
  141. $poly = new LagrangeInterpolation($x, $y);
  142.  
  143. // Generate scrambled polynomial
  144. $f = function($x) use ($poly, $digits) {
  145. return $poly->evaluate($x) - $digits[0];
  146. };
  147.  
  148. // Guess initial values using root_finding_initial_values function
  149. list($init_a, $init_b) = root_finding_initial_values($f);
  150.  
  151. try {
  152. // Apply the Hybrid method
  153. list($x, $fx, $i, $execution_time, $a, $b) = hybrid_method($f, $init_a, $init_b);
  154.  
  155. function encrypt_file($file_path, $key, $iv) {
  156. $plaintext = file_get_contents($file_path);
  157. $cipher = "aes-256-cfb8";
  158. $ciphertext = openssl_encrypt($plaintext, $cipher, $key, OPENSSL_RAW_DATA, $iv);
  159.  
  160. file_put_contents($file_path . '.enc', $ciphertext);
  161. }
  162.  
  163. $key = pack('Q', round($x)) * 2;
  164. $iv = openssl_random_pseudo_bytes(16);
  165. $file_path = "Data.txt";
  166. encrypt_file($file_path, $key, $iv);
  167. echo "key: " . bin2hex($key) . "\n";
  168. echo "iv: " . bin2hex($iv);
  169. } catch (Exception $e) {
  170. echo 'Error: ' . $e->getMessage();
  171. }
  172. echo gettype ($iv);
  173. ?>
  174.  
  175.  
  176. // your code goes here
Success #stdin #stdout #stderr 0.02s 26536KB
stdin
Standard input is empty
stdout
key: 30
iv: 0c883276dfe82b34ae890b48bbe869bfstring

// your code goes here
stderr
PHP Warning:  A non-numeric value encountered in /home/P9qo4c/prog.php on line 163
PHP Warning:  file_get_contents(Data.txt): failed to open stream: No such file or directory in /home/P9qo4c/prog.php on line 156
PHP Warning:  file_put_contents(Data.txt.enc): failed to open stream: Permission denied in /home/P9qo4c/prog.php on line 160