jueves, 20 de septiembre de 2012

HTTP RSA Implementation

Hi, for this week we needed to implement the algorithm rsa in a web service, for the activity I choose php for WS.

The dinamic between server and user is the next:
  • The server has e and n, this keys are public.
  • The user has n and d, where d is the private key.
  • When a user open the page, server generate a x random number.
  • The user use this number for generate a response(r), the response is generate for a script, provided for the server.
  • Then the user put this r in the service and the server compare r with a num produced.
  • If this is correct the users are welcome, else the users are denied.
Important points:
  • For generate r (user), need two functions f(x)  &  fastmodexp(x, y, mod).
    • where in my case f(x) return (x*2)+5, this can be a other ecuation.
    • fasmodexp is f(x)**d mod n
      • d is the private key.
  • In the autentification for the server we need to implement the same f(x), but this help us  to compare, use fasmodexp(r, e, n), where r is the response of the user and e & n is the public key.

Code:
home.php
<?php
function getUsuarios($usuario)
{
include 'parametrosLogin/loginConnection.php';
$link_C = "SELECT usuario FROM rsa_cripto";
$link_CR = mysql_query($link_C, $conexion) or die(mysql_error());
$link_C_rows = mysql_num_rows($link_CR);
if ($link_C_rows> 0) {
while ($link_C_Rrows = mysql_fetch_assoc($link_CR)) {
$usuario = $link_C_Rrows['usuario'];
echo "<option value=$usuario >$usuario</option>";
}
}
}
?>
<html>
<head>
</head>
<body>
<!--<form action="home.php" method="post">-->
<h1>RSA</h1>
<form action="rsaAutentification.php" method="post">
<a href="#">Script.py</a><br/>
<label>Your x: </label>
<input type="text" name="x" size = "10" value="<?php echo rand(0, 10)?>" required="required"/>
<p>
<h4>Autentification</h4>
<label>User: </laberl>
<select name="user" required="required">
<option value="">---</option>
<?php getUsuarios(); ?>
</select><br/>
<label>Respuesta:</label>
<input type="text" name="respuesta" size = "10" required="required"/> </br>
</p>
<input type="submit" value="submit" /> </br>
</form>
</body>
</html>
view raw gistfile1.php hosted with ❤ by GitHub
rsaAutentification.php
<a href="home.php">Regresar</a><br/>
<?php
validar($_POST['user'], $_POST['respuesta'], $_POST['x']);
function validar($usuario, $r, $x)
{
$lista = getUser($usuario);
if($lista)
{
$e = $lista[0];
$n = $lista[1];
$y = f($x);
$num = fastmodexp($r, $e, $n);
if($y == $num)
{
echo "<h3>Welcome $usuario</h3>";
echo "You can see 9gag<br/><br/>";
echo "<iframe src='http://www.9gag.com' width='900' height='900'></iframe>";
}
else echo "Fail..!";
echo "<br/><br/>";
}
else
{
echo "<h1>Usuario invalido<h1>";
die();
}
}
function getUser($usuario)
{
include 'parametrosLogin/loginConnection.php';
$link_C = "SELECT e, n FROM rsa_cripto where usuario = '".$usuario."'";
$link_CR = mysql_query($link_C, $conexion) or die(mysql_error());
$link_C_rows = mysql_num_rows($link_CR);
if ($link_C_rows> 0) {
while ($link_C_Rrows = mysql_fetch_assoc($link_CR)) {
$e = $link_C_Rrows['e'];
$n = $link_C_Rrows['n'];
}
$lista = array($e, $n);
return $lista;
}
else return False;
}
function f($x)
{
return ($x*2)+5;
}
function fastmodexp($x, $y, $mod)
{
$p = 1;
$aux = $x;
while($y > 0){
if ($y % 2 == 1){
$p = ($p * $aux) % $mod;
}
$aux = ($aux * $aux) % $mod;
$y = $y >> 1;
}
return ($p);
}
?>
view raw gistfile1.php hosted with ❤ by GitHub
validation.py
def main(x, d, n):
print f(x)
r = fastmodexp(f(x), d, n)
print 'tu valor de r es:', r
def f(x):
return (x*2)+5
def fastmodexp(x, y, mod):
p = 1
aux = x
while y > 0:
if y % 2 == 1:
p = (p * aux) % mod
aux = (aux * aux) % mod
y = y >> 1
return p
x = int(raw_input("Dame x: "))
n = int(raw_input("Dame n: "))
d = int(raw_input("Dame d: "))
main(x, d, n)
view raw gistfile1.py hosted with ❤ by GitHub
Tabla Usuarios
create table rsa_cripto(id int not null auto_increment primary key, usuario varchar(20), e int(30), n int(30));
view raw gistfile1.sql hosted with ❤ by GitHub

result:





WebService:
You can check the program in the next link: ....

Note:
Php is a pussy,  in my case when I use a big numbers the autentification fail, but checking in python the autentification is correct.


1 comentario: