form.php

  1. <?php
  2. ini_set('display_errors', 1);
  3.  
  4. require_once "CypherMCrypt.php";
  5. require_once "CypherOpenSSL.php";
  6.  
  7. $encoded = $decoded = $method = $action = "";
  8. $method = getMethod();
  9. $action = getAction();
  10. $encoded = getEncoded();
  11. $decoded = getDecoded();
  12. switch ($action) {
  13. case 'encode':
  14. switch ($method) {
  15. case 'MCrypt': $encoded = CypherMCrypt::encode($encoded); break;
  16. case 'OpenSSL': $encoded = CypherOpenSSL::encode($encoded); break;
  17. }
  18. break;
  19. break;
  20. case 'decode':
  21. switch ($method) {
  22. case 'MCrypt': $decoded = CypherMCrypt::decode($decoded); break;
  23. case 'OpenSSL': $decoded = CypherOpenSSL::decode($decoded); break;
  24. }
  25. break;
  26. }
  27.  
  28. function getMethod() {
  29. $METHODS = array("MCrypt", "OpenSSL");
  30. $method = empty($_POST['method'])? '': $_POST['method'];
  31. if (in_array($method, $METHODS) == FALSE) {
  32. $method = isPHP7()? "OpenSSL" : "MCrypt";
  33. }
  34. return $method;
  35. }
  36. function isPHP7() {
  37. return version_compare(phpversion(), '7.0.0') >= 0;
  38. }
  39. function getAction() {
  40. $ACTIONS = array("encode", "decode");
  41. $action = empty($_POST['action'])? '': $_POST['action'];
  42. if (in_array($action, $ACTIONS) == FALSE) $action = "";
  43. return $action;
  44. }
  45. function getEncoded() {
  46. if (empty($_POST['encoded'])) return '';
  47. $encoded = $_POST['encoded'];
  48. return $encoded;
  49. }
  50. function getDecoded() {
  51. if (empty($_POST['decoded'])) return '';
  52. $decoded = $_POST['decoded'];
  53. return $decoded;
  54. }
  55. function printOptionMethods($default) {
  56. $METHODS = array(
  57. "MCrypt"=> "PHP 5",
  58. "OpenSSL"=> "PHP 7",
  59. );
  60. foreach ($METHODS as $method=>$php) {
  61. $selected = $method == $default ? ' selected':'';
  62. echo "<option$selected value=\"$method\">$method ($php)</option>";
  63. }
  64. }
  65.  
  66.  
  67. ?><!DOCTYPE html>
  68. <html>
  69. <head>
  70. <meta charset="utf-8">
  71. <title>Cypher form</title>
  72. <style>
  73. body { background-color: #fafafa; }
  74. fieldset { display: inline-block; vertical-align: top; }
  75. </style>
  76. </head>
  77. <body>
  78. <h1>Cypher form</h1>
  79. <form method="POST">
  80. <p>
  81. <label>Method</label><br>
  82. <select name="method"><?php printOptionMethods($method); ?></select>
  83. </p>
  84. <fieldset>
  85. <label>Plain text</label><br>
  86. <textarea name="encoded" cols="100" rows="10"><?=htmlentities($decoded)?></textarea>
  87. <br><button type="submit" name="action" value="encode">Encode</button>
  88. </fieldset>
  89. <fieldset>
  90. <label>Base64</label><br>
  91. <textarea name="decoded" cols="100" rows="10"><?=htmlentities($encoded)?></textarea>
  92. <br><button type="submit" name="action" value="decode">Decode</button>
  93. <br><label>Hex</label><br><?= bin2hex(base64_decode($encoded)) ?>
  94. </fieldset>
  95. </form>
  96. </body>
  97. </html>
  98.  

Proinf.net