Advertisement
Guest User

OTP example

a guest
Jun 16th, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.54 KB | None | 0 0
  1. <?php
  2. /*
  3. Copyright (C) 2003 Enders Web Development
  4. This program is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU General Public License as published by the Free Software
  6. Foundation; either version 2 of the License, or (at your option) any later
  7. version.
  8. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  9. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  10. PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the
  13. Free Software Foundation, Inc.,
  14. 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA.
  16. Author Email: tomender@ptd.net
  17. Author Post:
  18. J. Thomas Enders
  19. PO Box 663
  20. Gilbert, PA 18331, USA
  21. */
  22. error_reporting(E_ALL);
  23.  
  24. if(!isset($_POST['key'])) {$_POST['key'] = '';}
  25. if(!isset($_POST['plaintext'])) {$_POST['plaintext'] = '';}
  26. if(!isset($_POST['ciphertext'])) {$_POST['ciphertext'] = '';}
  27.  
  28. $parse = false;
  29. $error = '';
  30. if(isset($_POST['encrypt']) || isset($_POST['decrypt'])) {
  31.  $parse = true;
  32.  if((isset($_POST['encrypt']) || isset($_POST['decrypt']))) {
  33.    if($_POST['key'] == '') {
  34.     $error .= "ERROR: You must enter a key to encrypt or decrypt text.<br />\n";
  35.     $parse = false;
  36.    } elseif(!preg_match("^[a-z]+$/i^",$_POST['key'])) {
  37.     $error .= "ERROR: The key (" . $_POST['key'] . ") can only contain letters.<br />\n";
  38.     $parse = false;
  39.    }
  40.   }  
  41.   if(isset($_POST['encrypt']) && $_POST['plaintext'] == '') {
  42.    $error .= "ERROR: You must enter pleantext to encrypt.<br />\n";
  43.    $parse = false;
  44.   } elseif (isset($_POST['decrypt']) && $_POST['ciphertext'] == '') {
  45.    $error .= "ERROR: You must enter ciphertext to decrypt.<br />\n";
  46.    $parse = false;
  47.   }
  48.  }
  49.  if ($parse && isset($_POST['encrypt'])) {
  50.   $_POST['ciphertext'] = encrypt($_POST['key'],$_POST['plaintext']);
  51.  } elseif ($parse && isset($_POST['decrypt'])) {
  52.   $_POST['plaintext'] = decrypt($_POST['key'],$_POST['ciphertext']);
  53.  }
  54.  echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  55. ?>
  56. <!--
  57. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  58. <html xmlns="http://www.w3.org/1999/xhtml">
  59. <head>
  60.  <title>Vernam Cipher</title>
  61.  <style type="text/css">
  62.    body {
  63.     font-family: arial,helvetica,_sans;
  64.     font-size: 11pt;
  65.     color: #000000;
  66.     background-color: #FFFFEE;
  67.    }  
  68.    
  69.    div.dataScrubbing {
  70.     position: absolute;
  71.     padding: 5px;
  72.     background-color: #EEFFFF;
  73.     border-width: 1px;
  74.     border-color: #888899;
  75.     border-style: solid;
  76.     color: #990000;
  77.     width: 375px;
  78.    }
  79.  
  80.    div.inputArea {
  81.     background-color: #FFFFFF;
  82.     position: absolute;
  83.     padding: 5px;
  84.     border-width: 1px;
  85.     border-color: #000000;
  86.     border-style: solid;
  87.     width: 380px;
  88.    }
  89.  
  90.    input,textarea {
  91.     font-family: arial,helvetica,_sans;
  92.     font-size: 11pt;
  93.     color: #000000;
  94.     background-color: #EEFFFF;
  95.     border-width: 1px;
  96.     border-color: #889999;
  97.     border-style: solid;
  98.    }
  99.  </style>
  100. </head>
  101.  <body>
  102.   <h1>Vernam Cipher</h1>
  103. -->
  104.   <?php
  105.    if($error != '') {
  106.     echo '<div class="dataScrubbing">' . "\n";
  107.     echo $error;
  108.     echo "</div>\n";
  109.     echo "<div><br /><br /></div>\n";
  110.    }
  111. ?>
  112. <!--
  113. <div class="inputArea">
  114.   <form method="post" action="">
  115.     Enter the key: <input type="text" name="key" size="20" value="<?php $_POST['key'] ?>" /><br />
  116.     Plaintext:<br />
  117.     <textarea name="plaintext" cols="50" rows="5"><?php $_POST['plaintext'] ?></textarea><br />
  118.     Ciphertext:<br />
  119.     <textarea name="ciphertext" cols="50" rows="5"><?php $_POST['ciphertext'] ?></textarea><br /><br />
  120.     <input type="submit" name="encrypt" value="Encrypt" />&nbsp;&nbsp;
  121.     <input type="submit" name="decrypt" value="Decrypt" />
  122.   </form>
  123. </div>
  124. </body>
  125. </html>
  126. -->
  127. <?php
  128.  function encrypt($key,$text) {
  129.   $key = strtolower($key);
  130.   preptext($text,'enc');
  131.   $key .= $text;
  132.   $retval = '';
  133.   for($i=0;$i<strlen($text);$i++) {
  134.    $cur_char = ord(substr($text,$i,1)) - 97;
  135.    $cur_key = ord(substr($key,$i,1)) - 97;
  136.    $retval .= chr(($cur_char + $cur_key)%26 + 65);
  137.   }
  138.   return $retval;
  139.  }
  140.  
  141.  function decrypt($key,$text) {
  142.   $key = strtoupper($key);
  143.   preptext($text,'dec');  
  144.   $retval = '';
  145.   $i_inc = strlen($key);
  146.   $str_len = strlen($text);
  147.   $retval = '';
  148.   for($i=0;$i<$str_len;$i = $i + $i_inc) {
  149.    $key .= strtoupper(substr($retval,($i-$i_inc),$i_inc));
  150.    for($j=0;$j<$i_inc;$j++) {
  151.     $cur_char = ord(substr($text,($i+$j),1)) - 65;
  152.     $cur_key = ord(substr($key,($i+$j),1)) - 65;
  153.     $cur_dec = $cur_char - $cur_key;
  154.     if($cur_dec > -1) {$cur_dec = $cur_dec + 97;}
  155.     else {$cur_dec = (26 + $cur_dec)+ 97;}
  156.     if(($i+$j) < $str_len) {$retval .= chr($cur_dec);}
  157.    }
  158.   }
  159.   return $retval;
  160.  }  
  161.  function preptext(&$text,$direction = 'enc') {
  162.   $direction = strtolower($direction);
  163.    if($direction != 'enc' && $direction != 'dec') {
  164.     warn("preptext called with an inappropriate direction value.");
  165.    }
  166.    switch ($direction) {
  167.     case 'enc';
  168.      $text = strtolower($text);
  169.      $low_bound = 95;
  170.      $high_bound = 122;
  171.     break;
  172.     case 'dec';
  173.      $text = strtoupper($text);
  174.      $low_bound = 65;
  175.      $high_bound = 90;
  176.    }
  177.  
  178.    $t_len = strlen($text);
  179.    for($i=0;$i<$t_len;$i++) {
  180.     if(ord(substr($text,$i,1)) < $low_bound ||
  181.     ord(substr($text,$i,1)) > $high_bound) {
  182.      $text = substr($text,0,$i) . substr($text,($i+1));
  183.      $t_len--;
  184.     }
  185.    }
  186.  }
  187. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement