Advertisement
agnishom

diffie-hellman-app

Sep 25th, 2013
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.71 KB | None | 0 0
  1. [url=http://guide-to-the-galaxy.co.nf/diffie-hellman/]Diffie-Hellman App[/url]
  2. Finally, Programmed this! :)
  3.  
  4. 1. Each conversation is stored in a seperate file(in cyphertext) with a unique thread id. To create a new conversation or join one, enter the desired thread_id.
  5. 2. Now, we support alphanumeric private keys.
  6. 3. I've seen to it that the RC4 crypter handles not only decimal digits but also other kind of bytes when the shared_secret is entered.
  7. 4. p and g are as described in the other thread
  8. ***********************************************************************
  9. [b]Source[/b]
  10.  
  11. index.php
  12. [code]
  13. <?php
  14.   function numeralise($s)
  15.   {
  16.     $i = '0';
  17.     $j = 0;
  18.     foreach(str_split($s) as $ch)
  19.             {
  20.               $i = bcadd($i, bcmul((string)ord($ch),bcpow(256,(string)$j)));
  21.               $j = $j + 1;
  22.             }
  23.      return $i;
  24.   }
  25.   function denumeralise($s)
  26.   {
  27.     $str = '';
  28.     while ($s!=0)
  29.     {
  30.       $t = bcmod($s,'256');
  31.       $str = $str.chr((int)$t);
  32.       $s = bcdiv($s,'256',0);
  33.     }
  34.     return $str;
  35.   }
  36.   function rc4($key, $data)
  37. {
  38.     // Store the vectors "S" has calculated
  39.     static $SC;
  40.     // Function to swaps values of the vector "S"
  41.     $swap = create_function('&$v1, &$v2', '
  42.        $v1 = $v1 ^ $v2;
  43.        $v2 = $v1 ^ $v2;
  44.        $v1 = $v1 ^ $v2;
  45.    ');
  46.     $ikey = crc32($key);
  47.     if (!isset($SC[$ikey])) {
  48.         // Make the vector "S", basead in the key
  49.         $S    = range(0, 255);
  50.         $j    = 0;
  51.         $n    = strlen($key);
  52.         for ($i = 0; $i < 255; $i++) {
  53.             $char  = ord($key[$i % $n]);
  54.             $j     = ($j + $S[$i] + $char) % 256;
  55.             $swap($S[$i], $S[$j]);
  56.         }
  57.         $SC[$ikey] = $S;
  58.     } else {
  59.         $S = $SC[$ikey];
  60.     }
  61.     // Crypt/decrypt the data
  62.     $n    = strlen($data);
  63.     $data = str_split($data, 1);
  64.     $i    = $j = 0;
  65.     for ($m = 0; $m < $n; $m++) {
  66.         $i        = ($i + 1) % 256;
  67.         $j        = ($j + $S[$i]) % 256;
  68.         $swap($S[$i], $S[$j]);
  69.         $char     = ord($data[$m]);
  70.         $char     = $S[($S[$i] + $S[$j]) % 256] ^ $char;
  71.         $data[$m] = chr($char);
  72.     }
  73.     return implode('', $data);
  74. }
  75.   if (!($_GET['thread']))
  76.   {?>
  77.     <html>
  78.       <head>
  79.       <title>Enter Thread Name</title>
  80.       </head>
  81.       <form action='index.php' method='get'>
  82.         Thread: <input type="text" name="thread">
  83.       <input type="submit" value="Submit">
  84.       </form> <?php
  85.      
  86.       }
  87.       else{
  88.         $name = $_GET['thread'];
  89.         $f = fopen($name.md5($name),'a+');
  90.         if ($_GET['s'])
  91.         {
  92.           $s = $_GET['s'];
  93.           $s = denumeralise($s);
  94.           $message = $_GET['message'];
  95.           if ($_GET['encrypt'])
  96.           {
  97.             fwrite($f,base64_encode(rc4($s,$message)));
  98.             fwrite($f,"<br>\n");
  99.             fwrite($f,"<br>\n");
  100.             echo "Message Entered<br>";
  101.             echo "<a href='http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']."'>Click here to go back</a>";
  102.             exit;
  103.           }
  104.           else
  105.           {
  106.             echo rc4($s,base64_decode($message));
  107.                         echo "<br><a href='http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']."'>Click here to go back</a>";
  108.             exit;
  109.           }
  110.         }
  111.         $A = fgets($f);
  112.         echo "Alice's Public Key: ". $A.'<br>';
  113.         if (!($A))
  114.         {if ($_GET['a'])
  115.         {
  116.           $p = '179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007';
  117.           $g = '2';
  118.           $a = numeralise($_GET['a']);
  119.           $A = bcpowmod($g,$a,$p);
  120.           fwrite($f,$A);
  121.           fwrite($f,"\n");
  122.           echo "Now Please Allow Bob to enter his private key. Give Bob this <a href='http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']."'>link </a> or the thread name";
  123.           //header("Location: http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']);
  124.           exit;
  125.          }
  126.          else
  127.          {?>
  128.       <html>
  129.       <head>
  130.       <title>Enter Alice's Private Key</title>
  131.       </head>
  132.       <form action="index.php" method='get'>
  133.         Alice's Private Key: <input type="text" name="a">
  134.         <input type="hidden" value="<?php echo $_GET['thread']; ?>" name="thread">
  135.       <input type="submit" value="Submit">
  136.       </form>
  137.       <?php exit;
  138. }
  139.         } $B = fgets($f);
  140.         echo "Bob's Public Key: ".$B.'<br>';
  141.         if (!($B))
  142.         {if ($_GET['b'])
  143.         {
  144.           $p = '179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007';
  145.           $g = '2';
  146.           $b = numeralise($_GET['b']);
  147.           $B = bcpowmod($g,$b,$p);
  148.           fwrite($f,$B);
  149.           fwrite($f,"\n");
  150.           //header("Location: http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']);
  151.           echo "Now you are ready to compute the shared secret from <a href='http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']."'>here</a>";
  152.           exit;
  153.          }
  154.          else
  155.          {?>
  156.       <html>
  157.       <head>
  158.       <title>Enter Bob's Private Key</title>
  159.       </head>
  160.       <form action="index.php" method='get'>
  161.         Bob's Private Key: <input type="text" name="b">
  162.         <input type="hidden" value="<?php echo $_GET['thread']; ?>" name="thread">
  163.       <input type="submit" value="Submit">
  164.       </form>
  165.       <?php exit;
  166. }
  167.         }
  168.   ?><html>
  169.       <head>
  170.       <title>Compute Shared Secret and View Messages</title>
  171.       </head>
  172.         <br><b>Compute Shared Secret</b><br>
  173.       <form action="computeAB.php" method='get'>
  174.         Partner's Public Key: <input type="text" name="A"><br>
  175.         Your Private Key: <input type="text" name="b">
  176.       <input type="submit" value="Submit">
  177.       </form>
  178.         <br><b>Share a message</b><br>
  179.       <form action="index.php" method='get'>
  180.         Shared Secret: <input type="text" name="s"><br>
  181.         Message: <input type="text" width="10" height="5" name="message">
  182.         <input type="hidden" value="<?php echo $_GET['thread']; ?>" name="thread">
  183.         <input type="hidden" value="1" name="encrypt">
  184.       <input type="submit" value="Submit">
  185.       </form>
  186.           <br><b>Read a message</b><br>
  187.       <form action="index.php" method='get'>
  188.         Shared Secret: <input type="text" name="s"><br>
  189.         Message: <input type="text" width="10" height="5" name="message">
  190.         <input type="hidden" value="<?php echo $_GET['thread']; ?>" name="thread">
  191.       <input type="submit" value="Submit">
  192.       </form>
  193.         <br><br><br> <b>All Messages</b><br>
  194.         <?php
  195.         while (!feof($f))
  196.   {
  197.   echo fgetc($f);
  198.   }
  199. fclose($f);
  200.   } ?>
  201.         <hr><pre>Chlamydomonas Labs</pre></html>
  202. [/code]
  203.  
  204. computeAB.php //Contains more code than necessary because I am foolish
  205. [code]
  206. <?php
  207.   function numeralise($s)
  208.   {
  209.     $i = '0';
  210.     $j = 0;
  211.     foreach(str_split($s) as $ch)
  212.             {
  213.               $i = bcadd($i, bcmul((string)ord($ch),bcpow(256,(string)$j)));
  214.               $j = $j + 1;
  215.             }
  216.      return $i;
  217.   }
  218.   function rc4($key, $data)
  219. {
  220.     // Store the vectors "S" has calculated
  221.     static $SC;
  222.     // Function to swaps values of the vector "S"
  223.     $swap = create_function('&$v1, &$v2', '
  224.        $v1 = $v1 ^ $v2;
  225.        $v2 = $v1 ^ $v2;
  226.        $v1 = $v1 ^ $v2;
  227.    ');
  228.     $ikey = crc32($key);
  229.     if (!isset($SC[$ikey])) {
  230.         // Make the vector "S", basead in the key
  231.         $S    = range(0, 255);
  232.         $j    = 0;
  233.         $n    = strlen($key);
  234.         for ($i = 0; $i < 255; $i++) {
  235.             $char  = ord($key[$i % $n]);
  236.             $j     = ($j + $S[$i] + $char) % 256;
  237.             $swap($S[$i], $S[$j]);
  238.         }
  239.         $SC[$ikey] = $S;
  240.     } else {
  241.         $S = $SC[$ikey];
  242.     }
  243.     // Crypt/decrypt the data
  244.     $n    = strlen($data);
  245.     $data = str_split($data, 1);
  246.     $i    = $j = 0;
  247.     for ($m = 0; $m < $n; $m++) {
  248.         $i        = ($i + 1) % 256;
  249.         $j        = ($j + $S[$i]) % 256;
  250.         $swap($S[$i], $S[$j]);
  251.         $char     = ord($data[$m]);
  252.         $char     = $S[($S[$i] + $S[$j]) % 256] ^ $char;
  253.         $data[$m] = chr($char);
  254.     }
  255.     return implode('', $data);
  256. }
  257.   $p = '179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007';
  258.   $g = '2';
  259.   if ($_GET['a'])
  260.            {
  261.              $a = $_GET['a'];
  262.              $a = str_replace(' ', '', $a);
  263.              $A = bcpowmod($g, $a, $p);
  264.              echo $A;
  265.            }
  266.   elseif ($_GET['A'])
  267.   {
  268.     $b = numeralise($_GET['b']);
  269.     $A = $_GET['A'];
  270.     $A = str_replace(' ', '', $A);
  271.     $b = str_replace(' ', '', $b);
  272.     $s = bcpowmod($A, $b, $p);
  273.     echo $s;
  274.     echo "<br>Press the back button in your browser to go back";
  275.   }
  276.   elseif ($_GET['s'])
  277.   {
  278.     $message=$_GET['message'];
  279.     $s = $_GET['s'];
  280.     echo base64_encode(rc4($s,$message));
  281.     echo '<br>';
  282.     echo rc4($s,base64_decode($message));
  283.   }    
  284. ?>
  285. [/code]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement