[url=http://guide-to-the-galaxy.co.nf/diffie-hellman/]Diffie-Hellman App[/url]
Finally, Programmed this! :)
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.
2. Now, we support alphanumeric private keys.
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.
4. p and g are as described in the other thread
***********************************************************************
[b]Source[/b]
index.php
[code]
<?php
function numeralise($s)
{
$i = '0';
$j = 0;
foreach(str_split($s) as $ch)
{
$i = bcadd($i, bcmul((string)ord($ch),bcpow(256,(string)$j)));
$j = $j + 1;
}
return $i;
}
function denumeralise($s)
{
$str = '';
while ($s!=0)
{
$t = bcmod($s,'256');
$str = $str.chr((int)$t);
$s = bcdiv($s,'256',0);
}
return $str;
}
function rc4($key, $data)
{
// Store the vectors "S" has calculated
static $SC;
// Function to swaps values of the vector "S"
$swap = create_function('&$v1, &$v2', '
$v1 = $v1 ^ $v2;
$v2 = $v1 ^ $v2;
$v1 = $v1 ^ $v2;
');
$ikey = crc32($key);
if (!isset($SC[$ikey])) {
// Make the vector "S", basead in the key
$S = range(0, 255);
$j = 0;
$n = strlen($key);
for ($i = 0; $i < 255; $i++) {
$char = ord($key[$i % $n]);
$j = ($j + $S[$i] + $char) % 256;
$swap($S[$i], $S[$j]);
}
$SC[$ikey] = $S;
} else {
$S = $SC[$ikey];
}
// Crypt/decrypt the data
$n = strlen($data);
$data = str_split($data, 1);
$i = $j = 0;
for ($m = 0; $m < $n; $m++) {
$i = ($i + 1) % 256;
$j = ($j + $S[$i]) % 256;
$swap($S[$i], $S[$j]);
$char = ord($data[$m]);
$char = $S[($S[$i] + $S[$j]) % 256] ^ $char;
$data[$m] = chr($char);
}
return implode('', $data);
}
if (!($_GET['thread']))
{?>
<html>
<head>
<title>Enter Thread Name</title>
</head>
<form action='index.php' method='get'>
Thread: <input type="text" name="thread">
<input type="submit" value="Submit">
</form> <?php
}
else{
$name = $_GET['thread'];
$f = fopen($name.md5($name),'a+');
if ($_GET['s'])
{
$s = $_GET['s'];
$s = denumeralise($s);
$message = $_GET['message'];
if ($_GET['encrypt'])
{
fwrite($f,base64_encode(rc4($s,$message)));
fwrite($f,"<br>\n");
fwrite($f,"<br>\n");
echo "Message Entered<br>";
echo "<a href='http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']."'>Click here to go back</a>";
exit;
}
else
{
echo rc4($s,base64_decode($message));
echo "<br><a href='http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']."'>Click here to go back</a>";
exit;
}
}
$A = fgets($f);
echo "Alice's Public Key: ". $A.'<br>';
if (!($A))
{if ($_GET['a'])
{
$p = '179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007';
$g = '2';
$a = numeralise($_GET['a']);
$A = bcpowmod($g,$a,$p);
fwrite($f,$A);
fwrite($f,"\n");
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";
//header("Location: http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']);
exit;
}
else
{?>
<html>
<head>
<title>Enter Alice's Private Key</title>
</head>
<form action="index.php" method='get'>
Alice's Private Key: <input type="text" name="a">
<input type="hidden" value="<?php echo $_GET['thread']; ?>" name="thread">
<input type="submit" value="Submit">
</form>
<?php exit;
}
} $B = fgets($f);
echo "Bob's Public Key: ".$B.'<br>';
if (!($B))
{if ($_GET['b'])
{
$p = '179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007';
$g = '2';
$b = numeralise($_GET['b']);
$B = bcpowmod($g,$b,$p);
fwrite($f,$B);
fwrite($f,"\n");
//header("Location: http://guide-to-the-galaxy.co.nf/diffie-hellman/index.php?thread=".$_GET['thread']);
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>";
exit;
}
else
{?>
<html>
<head>
<title>Enter Bob's Private Key</title>
</head>
<form action="index.php" method='get'>
Bob's Private Key: <input type="text" name="b">
<input type="hidden" value="<?php echo $_GET['thread']; ?>" name="thread">
<input type="submit" value="Submit">
</form>
<?php exit;
}
}
?><html>
<head>
<title>Compute Shared Secret and View Messages</title>
</head>
<br><b>Compute Shared Secret</b><br>
<form action="computeAB.php" method='get'>
Partner's Public Key: <input type="text" name="A"><br>
Your Private Key: <input type="text" name="b">
<input type="submit" value="Submit">
</form>
<br><b>Share a message</b><br>
<form action="index.php" method='get'>
Shared Secret: <input type="text" name="s"><br>
Message: <input type="text" width="10" height="5" name="message">
<input type="hidden" value="<?php echo $_GET['thread']; ?>" name="thread">
<input type="hidden" value="1" name="encrypt">
<input type="submit" value="Submit">
</form>
<br><b>Read a message</b><br>
<form action="index.php" method='get'>
Shared Secret: <input type="text" name="s"><br>
Message: <input type="text" width="10" height="5" name="message">
<input type="hidden" value="<?php echo $_GET['thread']; ?>" name="thread">
<input type="submit" value="Submit">
</form>
<br><br><br> <b>All Messages</b><br>
<?php
while (!feof($f))
{
echo fgetc($f);
}
fclose($f);
} ?>
<hr><pre>Chlamydomonas Labs</pre></html>
[/code]
computeAB.php //Contains more code than necessary because I am foolish
[code]
<?php
function numeralise($s)
{
$i = '0';
$j = 0;
foreach(str_split($s) as $ch)
{
$i = bcadd($i, bcmul((string)ord($ch),bcpow(256,(string)$j)));
$j = $j + 1;
}
return $i;
}
function rc4($key, $data)
{
// Store the vectors "S" has calculated
static $SC;
// Function to swaps values of the vector "S"
$swap = create_function('&$v1, &$v2', '
$v1 = $v1 ^ $v2;
$v2 = $v1 ^ $v2;
$v1 = $v1 ^ $v2;
');
$ikey = crc32($key);
if (!isset($SC[$ikey])) {
// Make the vector "S", basead in the key
$S = range(0, 255);
$j = 0;
$n = strlen($key);
for ($i = 0; $i < 255; $i++) {
$char = ord($key[$i % $n]);
$j = ($j + $S[$i] + $char) % 256;
$swap($S[$i], $S[$j]);
}
$SC[$ikey] = $S;
} else {
$S = $SC[$ikey];
}
// Crypt/decrypt the data
$n = strlen($data);
$data = str_split($data, 1);
$i = $j = 0;
for ($m = 0; $m < $n; $m++) {
$i = ($i + 1) % 256;
$j = ($j + $S[$i]) % 256;
$swap($S[$i], $S[$j]);
$char = ord($data[$m]);
$char = $S[($S[$i] + $S[$j]) % 256] ^ $char;
$data[$m] = chr($char);
}
return implode('', $data);
}
$p = '179769313486231590770839156793787453197860296048756011706444423684197180216158519368947833795864925541502180565485980503646440548199239100050792877003355816639229553136239076508735759914822574862575007425302077447712589550957937778424442426617334727629299387668709205606050270810842907692932019128194467627007';
$g = '2';
if ($_GET['a'])
{
$a = $_GET['a'];
$a = str_replace(' ', '', $a);
$A = bcpowmod($g, $a, $p);
echo $A;
}
elseif ($_GET['A'])
{
$b = numeralise($_GET['b']);
$A = $_GET['A'];
$A = str_replace(' ', '', $A);
$b = str_replace(' ', '', $b);
$s = bcpowmod($A, $b, $p);
echo $s;
echo "<br>Press the back button in your browser to go back";
}
elseif ($_GET['s'])
{
$message=$_GET['message'];
$s = $_GET['s'];
echo base64_encode(rc4($s,$message));
echo '<br>';
echo rc4($s,base64_decode($message));
}
?>
[/code]