Max Hernandez

Laberinto - Ejemplo de Canvas en HTML5

jueves, 20 de septiembre de 2012

RSA-based digital signatures


The homework for this week is implementing a RS-Authentication using a web service and this is what i did.

My implementation 

This class allows user to connect with a computer called server and receive a challenge value used to authenticate with this. Once the client send its challenge ciphered it is compared with the value calculated with the private keys saved in the server database.

Server.py
#! /usr/bin/python
# -*- coding: latin-1 -*-

from rsa import RSA
import cgi, cgitb
import random

def open_keys(nameFile, username):
    fl = open(nameFile, "r")
    for i in fl:
        i = i.split(",")
        if i[0].lower() == username.lower() :
            fl.close()
            return int(i[1]), int(i[2])
    fl.close()
    return False

def save_value(nameFile, value):
    fl = open(nameFile, "w")
    fl.write(str(value))
    fl.close()

def load_value(nameFile):
    fl = open(nameFile, "r")
    value = fl.read()
    fl.close()
    return int(value)

def standar_function(x):
    return 3*x+20;

def verify(user, y):
    original_value = load_value("value.dat")
    if original_value == y:
        return True
    else:
        return False

def main():
    entrada = cgi.FieldStorage()
    print "Content-type: text\n"
    if entrada.has_key("x") and entrada.has_key("user"):
        rsa = RSA()
        try:
            n, e = open_keys( "users.dat", str(entrada["user"].value) )
        except:
            print "False"
            return 
        y = rsa.descifrar(n, e, int(entrada["x"].value))
        print str( verify(str(entrada["user"].value), y) )

    else:
        x = random.randint(1, 1000)
        save_value("value.dat", standar_function(x))
        print x

main()

This code is what connect with the server. I implemented this part in javascript language because is easy to share in the web and although is a Web-script the private keys do not get out of the computer in any time.

users.dat
Max, 233273, 73109
elisa, 124837172984042613022163, 124837172984042613022163

cliente.html
function standar(x){
    return (3*x)+20;
}

function pot_bin(x, n, mod){
    if(n == 1){
        return x;
    }else if( n%2 == 0){
        return Math.pow(pot_bin(x, n/2, mod), 2)%mod;
    }else{
        return (x*pot_bin(x, n-1, mod))%mod;
    }
}

function start(form){
  $.post( "http://localhost/cgi-bin/servidor_crip.py", {},
      function( data ) {
          y = standar(parseInt(data));
          n = parseInt( document.forms[form].elements["n"].value)
          d = parseInt( document.forms[form].elements["d"].value)
          user = String( document.forms[form].elements["user"].value)

          num = pot_bin(y, d, n).toPrecision(100);
          num = num.substring(0, num.indexOf("."));
          $.post( "http://localhost/cgi-bin/servidor_crip.py", {"x":num, "user":user},
              function( data ) {
                  if( data.indexOf("True") != -1 ){
                       document.getElementById("res").innerHTML=("

The user:'"+user+"' has been authenticated.

"); }else if( data.indexOf("False") != -1 ){ document.getElementById("res").innerHTML=("

The user:'"+user+"' has not been authenticated.

"); }else{ document.getElementById("res").innerHTML=("

Error with the server.

"); } } ); } ); }

Changes made in old RSA code:

    def exp_bin(self, x, n, module):
        if n == 1:
            return x
        elif n%2 == 0:
            return ((self.exp_bin(x, n/2))**2)%module
        else:
            return (x*self.exp_bin(x, n-1))%module


Service running

This is the interface of the client running:

 This is a case when the user and the private keys are not in the database or are wrong.



In this case the keys and user are correct and the user is Authenticated.


References:
http://elisa.dyndns-web.com/~elisa/teaching/comp/sec/hash.pdf

2 comentarios:

  1. Thanks. I have been searching for this code. Actually I am learning about digital signature and how these signature are created. You have explained about the most popular form of digital signature which are based on RSA algorithm.
    digital signature Microsoft

    ResponderBorrar