100% found this document useful (1 vote)
513 views4 pages

Fiat Shamir Python Code

This document describes Python code implementing a Fiat-Shamir encryption scheme between a client and server. The client generates a private key and random value, computes x as the square of the random value modulo a number n received from the server, and sends x and other values to the server. The server runs a thread to calculate values for the client based on n, which is generated as the product of two prime numbers p and q input to the server. It verifies the client's identity by checking if the square of a value received from the client is equal to the product of other values modulo n.

Uploaded by

sankalpvairat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
513 views4 pages

Fiat Shamir Python Code

This document describes Python code implementing a Fiat-Shamir encryption scheme between a client and server. The client generates a private key and random value, computes x as the square of the random value modulo a number n received from the server, and sends x and other values to the server. The server runs a thread to calculate values for the client based on n, which is generated as the product of two prime numbers p and q input to the server. It verifies the client's identity by checking if the square of a value received from the client is equal to the product of other values modulo n.

Uploaded by

sankalpvairat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Fiat Shamir Ecryption Code in PythonClient 1-->

import socket
import random
import math
def Main():
#ra=input("Enter the value of ra")
port=5000
host='10.50.7.176';
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);
s.connect((host,port));
s.send("n")
no=s.recv(1024)
n=int(no)
print "n",n
s_pvt=random.randrange(1, n-1, 1)
r=random.randrange(1, n-1, 1)
x=math.pow(r,2)%n
s.close()
port=3000;
address="10.50.7.176"
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM);
s.bind((address,port));
s.listen(5);
conn, add = s.accept();
s_pub=conn.recv(1024)
conn.send('ok')
x_c=conn.recv(1024)
x_c1=int(x_c)
c=random.randrange(0,1, 1)
conn.send(str(c))
y=conn.recv(1024)
y1=int(y)
sq=(math.pow(y1,2))%n
o=(x_c1*math.pow(int(s_pub),c))%n
if(sq==o):
print "Identity approved:"
Main()

Client 2-->
import socket
import random
import math
def Main():
#ra=input("Enter the value of ra")
port=5000
host='10.50.7.176';
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);
s.connect((host,port));
s.send("n")
no=s.recv(1024)
n=int(no)
print "n",n
s_pvt=random.randrange(1, n-1, 1)
r=random.randrange(1, n-1, 1)
x=int(math.pow(r,2)%n)
s.close()
port=3000
host='10.50.7.176';
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM);
s.connect((host,port));
s.send(str(s_pvt))
res=s.recv(1024)
if(res=='ok'):
s.send(str(x))
c=s.recv(1024)
c1=int(c)
y=int(r*math.pow(s_pvt,c1)%n)
s.send(str(y))
Main()

Server -->
import socket
import threading
def check_prime(n):
if(n<=1):
print "Prime number can not be -ve,zero or 1. Enter the number grater than 4000: "
return False
if(n==2 or n==3):
print "Prime number entered but number should be grater than 4000"
return False
#if(n<4000):
#print "Enter the number grater than 4000:"
#return False
if(n%2==0):
print "Even number can not be prime."
i=2
while((i*i)<=n):
#print i
if(n%i==0):
print "Not a prime number"
return False;
else:
i=i+1
return True
def calculate_au_bu(th,conn,n):
ra=conn.recv(1024)
#ra=int(ra)
#au=(a+(b*ra))%prime_no
#bu=(b+(c*ra))%prime_no
if(ra=='n'):
conn.send(str(n))
#msg=conn.recv(1024)
#if msg=='bu':
#conn.send(str(bu))
def Main():
#prime_no=input("Please enter the prime number:")
#while (check_prime(prime_no)!=True):
#Main()
print "Enter the value of p"
p=input()
while (check_prime(p)!=True):
Main()
#a=a%prime_no
print "Enter the value of q"
q=input()
while (check_prime(q)!=True):
Main()
n=p*q

port=5000;
address="10.50.7.176"
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM);
s.bind((address,port));
s.listen(5);
while True:
conn, add = s.accept();
th1 = threading.Thread(target=calculate_au_bu,args=("RetdThread",conn,n))
th1.start()
s.close()
Main()

You might also like