En esta semana se trabajo realizando un webservice, para esto utilice PHP y mysql
El siguiente es el código para la conexión de la base de datos, en donde use mi contraseña.
This file contains hidden or 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
<?php | |
$host="localhost"; | |
$user="root"; | |
$password="blanka"; | |
conn= mysql_connect (host, user, password) or die ("<b>No se puede conectar con la base de datos\n</b>\n"); | |
$db="validacion"; | |
if(!(mysql_select_db(db,conn))){ | |
echo "No se puede conectar a la base de datos"; | |
} | |
?> |
This file contains hidden or 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
<?php | |
require("conn.php"); | |
?> | |
<html> | |
<head> | |
<script> | |
function generar(inf,sup){ | |
numP = sup - inf; | |
rnd = Math.random() * numP; | |
rnd = Math.round(rnd); | |
document.formulario.challengen.value=parseInt(inf) + rnd; | |
} | |
</script> | |
</head> | |
<a href="script.py">Descargar script</a> | |
<form name="formulario" method="POST" action="validar.php" > | |
<b>Challenge:</b><input type="text" name="challengen"><input type="button" value="Generate" onclick="generar(0,100)"> | |
<br><b>User:</b><select name="user"> | |
<?php | |
$query="SELECT user FROM users"; | |
result=mysql_query(query,$conn); | |
while(row=mysql_fetch_array(result)){ | |
echo "<option value='".row["user"]."'>".row["user"]."</option>"; | |
} | |
?> | |
</select> | |
<br><b>Response:</b><input type="text" name="response"> | |
<br><input type="submit" value="submit"> | |
<?php | |
function f($x){ | |
return (x*7*x); | |
} | |
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); | |
} | |
if(isset(_POST['response']) and isset(_POST['user']) and isset($_POST['challengen'])){ | |
usuario = _POST['user']; | |
x = _POST['challengen']; | |
r = _POST['response']; | |
query="SELECT E,N From users WHERE user = '". usuario."'"; | |
result=mysql_query(query,$conn); | |
publica = mysql_fetch_array(result); | |
e = publica["E"]; | |
n = publica["N"]; | |
y = f(x); | |
num = fastmodexp(r, e, n); | |
if (y == num){ | |
echo "<strong><h2>Yes, it was ". $usuario ." :)</h2></strong>"; | |
} else { | |
echo "<strong><h2>No, it wasn't ". $usuario ." :(</h2></strong>"; | |
} | |
} ?> | |
</form> | |
</html> |
This file contains hidden or 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
def function(x): | |
return x*3*11*7 | |
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 | |
def main(): | |
x = int(raw_input("Dame x: ")) | |
d = int(raw_input("Dame d: ")) | |
n = int(raw_input("Dame n: ")) | |
y = function(x) | |
r = fastmodexp(y, d, n) | |
print " r = ",r | |
main() |
Código de la base de datos:
This file contains hidden or 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
-- Database: `validacion` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `users` | |
-- | |
CREATE TABLE IF NOT EXISTS `users` ( | |
`user` varchar(50) NOT NULL, | |
`E` bigint(20) NOT NULL, | |
`N` bigint(20) NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=latin1; | |
-- | |
-- Dumping data for table `users` | |
-- | |
INSERT INTO `users` (`user`, `E`, `N`) VALUES | |
('abraham', 1307909, 14287901), | |
('pepe', 17, 3233), | |
('cecy', 59, 5767), | |
('blanka', 79, 1067); |
Primero que nada tenemos la interfaz :P, en donde nos da la opción de descargar el script, para poder correrlo.
Ya descargado le picamos en Generate para generar nuestra x que nos salio 38:
Ahora vamos a correr el script y nos pide x, en este caso ponemos la que se nos genero arriba, la d y la n son de la clave privada que esa nos la dieron al momento de crear un usuario en mi caso blanka.
Nos arroja un resultado que es r=response, lo pondremos donde viene la R que significa response.
Le damos a submit y si soy yo :D
Y por ejemplo si pongo un usuario q no es correcto ya que no tengo sus claves privadas me sale q no soy :P
Publica y etiqueta ANTES DE LAS 09:30. 9 pts ya que PHP no maneja bien claves largas.
ResponderEliminar