Implementing RSA algorithm in a HTTP Server(Authentication)
Ok, the first code is the main page of the authentication, it just shows you the buttons of Download the code in python to get "r", and the button of Authenticate, Authenticate is the link to the Cripto02.php.
The second code shows you the x value and the textfields to set "r" and your "username".
When you push "send", the php links you to the Cripto03.php code (the last one page).
The last code(Cripto03.php) just show you the values calculated and the state of authentication (Success or failed)
This is the Python, it is used to get r value, thanks to my friend Avendaño for the help (algoritmo de exponenciación modular).
I got mi d value from de past homework (the past post), this is an image of the calculates:
And this is the screen shot when i calculated "r" value.
This is the link of my work: http://alejandroave.260mb.org/triana/Triana/
I save it in the server of my friend Avendaño because i was a lot of troubles with my server, but it works anyway (i guess) XD.
Sorry for my poor english XD
Hi there, in this homework we made a HTTP authentication implementing RSA algorithm.
This homework is similar to the past homework, but in this case we implemented the RSA algorithm in a HTTP server, but both are authentications.
Ok, i'm gonna explain what i made, and what do my programs :P.
Firstly i installed LAMP in my computer, to get PHP and Apache(is the important for this homework), cause i developed almost all the homework in PHP, just the "r" generator was developed in Python.
Ok, the next are the codes of PHP, in this case, Cripto01.php, Cripto02.php and Cripto03.php.
Cripto01.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>Autentification Main</title> | |
</head> | |
<body> | |
<center> | |
<H3>Click the download button to get the Python code</H3> | |
<a href= Calculates.py target=_blank><button>Download</button></a> | |
<p> | |
<H3>Click on Authenticate to get your x value :)</H3> | |
<a href="Cripto02.php?var=32"/> | |
<p><input type="submit" VALUE="Authenticate"/></p> | |
</p> | |
</center> | |
</body> | |
</html> |
"Cripto02.php"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<HTML> | |
<HEAD> | |
<TITLE>Operations</TITLE> | |
</HEAD> | |
<BODY> | |
<CENTER> | |
Your x value is: | |
<? | |
$numero = rand(1,30); | |
echo "2"; | |
?> | |
<BR> | |
<P> | |
<H4>Hi, now you must to run the python code, and fill the value of r and username in the next fields:</H3></P> | |
<FORM ACTION="Cripto03.php" METHOD="GET"> | |
Value of r: <INPUT TYPE="text" NAME="r"><BR> | |
User name: <INPUT TYPE="text" NAME="usuario"><BR> | |
<INPUT TYPE="hidden" name="x" value="2"> <!-- no pude enviar la x --> | |
<INPUT TYPE="submit" VALUE="Send"> | |
</FORM> | |
<? | |
echo "$numero"; | |
?> | |
</CENTER> | |
</BODY> | |
</HTML> |
"Cripto03.php"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<HTML> | |
<HEAD> | |
<TITLE>Verification</TITLE> | |
</HEAD> | |
<BODY> | |
<CENTEr> | |
<H1>Verification</H1> | |
Your username is: <?php | |
$user = $_GET['usuario']; | |
echo "$user" | |
?> | |
<BR> | |
Your "r" value is: <?php | |
$rvalue = $_GET['r']; | |
echo "$rvalue"?><br> | |
<BR> | |
Este es el valor de x: <?php | |
$LaX = $_GET['x']; | |
echo "$LaX"?><br> | |
<?php | |
$archivo = file("datos.txt"); | |
$lineas = count($archivo); | |
#$lineasalto=nl2br($linea); | |
echo "$archivo[0]"; | |
for($i = 0; $i<$lineas; $i++){ | |
if(chop($archivo[$i]) == $user){ | |
break; | |
}#Fin de if | |
}#Fin de for | |
$e = $archivo[++$i]; | |
$n = $archivo[++$i]; | |
echo "$e"; | |
echo "$n"; | |
fclose($archivo); | |
#Lo siguiente es sacar valor de y | |
$exponente = pow($rvalue, $e); | |
$LaY1 = (3*($LaX)) + 5; | |
#$LaY2 = fmod($exponente,$n); | |
/* | |
Para obtener el valor de LaY2 | |
*/ | |
$LaY2 = 0; | |
$bla = 1; | |
$u = ($rvalue)%($n); | |
while($e > 0){ | |
if(($e % 2) == 1){ | |
$bla = (($bla)*($u)) % ($n); | |
}#Fin de if | |
$e = $e/2; | |
$u = ($u*$u) % $n; | |
}#Fin de while | |
$LaY2 = $bla; | |
echo "Esto es el valor de r = $rvalue<BR/>"; | |
#echo "Esto es el valor de e = $e<BR/>"; | |
#echo "Esto es el valor de y1= $LaY1<BR/>"; | |
#echo "Esto es el valor de exponente = $exponente<BR/>"; | |
echo "Esto es el valor de n = $n<BR/>"; | |
echo "Esto es el valor de y2= $LaY2<BR/>"; | |
if($LaY1 == $LaY2){ | |
echo "y values are the same :)<BR/><BR/>"; | |
echo "Authentication is success $user<BR/>"; | |
} | |
else{ | |
echo "Authentication is incorrect<BR/>"; | |
} | |
?> | |
</CENTER> | |
</BODY> | |
</HTML> |
Ok, the first code is the main page of the authentication, it just shows you the buttons of Download the code in python to get "r", and the button of Authenticate, Authenticate is the link to the Cripto02.php.
The second code shows you the x value and the textfields to set "r" and your "username".
When you push "send", the php links you to the Cripto03.php code (the last one page).
The last code(Cripto03.php) just show you the values calculated and the state of authentication (Success or failed)
This is the Python, it is used to get r value, thanks to my friend Avendaño for the help (algoritmo de exponenciación modular).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
'''*Carlos Triana | |
****Python to get "y" and "r" value | |
**** | |
''' | |
#Global variables | |
y = 0 #f(x) = x+5 | |
r = 0 #This value will be required by Server | |
d = 0 #Value of private key | |
x = 0 #This value will be provide by the server (Challenge) | |
def getY(x): #To obtain Y value | |
global y | |
y = (3*(x)) + (5) | |
def getR(): #To obtain R value | |
global r,d,n,y #Global variables, previouly declarated | |
bla = 1 | |
u = y % n | |
while (d > 0): | |
if((d % 2) == 1): | |
bla = (bla*u) % n | |
d = d/2 | |
u = (u*u) % n | |
r = bla | |
def main(): | |
global d,n #Global variables, previously declarated | |
x = int(raw_input("Set x value please: ")) | |
d = int(raw_input("Set d(private key) value please: ")) | |
n = int(raw_input("Set n value please: ")) | |
getY(x) | |
getR() | |
print "Your 'y' value is: ",y | |
print "Your 'r' value is: ",r | |
main() |
I got mi d value from de past homework (the past post), this is an image of the calculates:
And this is the screen shot when i calculated "r" value.
This is the link of my work: http://alejandroave.260mb.org/triana/Triana/
I save it in the server of my friend Avendaño because i was a lot of troubles with my server, but it works anyway (i guess) XD.
Any doubt you can leave it in comments
Sorry for my poor english XD
Bye!