-
Notifications
You must be signed in to change notification settings - Fork 0
/
Paillier cryptosystem.py
143 lines (127 loc) · 3.58 KB
/
Paillier cryptosystem.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import math
import random
def L(u, n):
return (u - 1)//(n)
def encryption(g, m, r, n):
return pow(g, m, n*n) * pow(r, n, n*n) % (n*n)
def decryption(c, n, l, mu):
return L(pow(c, l, n*n), n) * mu % n
def gcdExtended(a, b):
if a == 0 :
return b,0,1
gcd,x1,y1 = gcdExtended(b%a, a)
x = y1 - (b//a) * x1
y = x1
return gcd,x,y
def reciprocal(a, n):
gcd, x, y = gcdExtended(a, n)
if gcd == 1:
return((x % n + n) % n)
else:
return(-1)
def homomorphismSum(a, b):
n, g, l, mu = generateKey()
# choose rand r
ra = random.randint(0, n - 1)
rb = random.randint(0, n - 1)
print("ra =", ra)
print("rb =", rb)
a1 = encryption(g, a, ra, n)
b1 = encryption(g, b, rb, n)
# multiplying encrypted numbers is equal to adding unencrypted numbers
s1 = (a1 * b1) % (n*n)
s = decryption(s1, n, l, mu)
if (s == (a + b)):
print(a, "+", b, "=", s, "\n")
else:
print("ERROR\n")
def homomorphismDiff(a, b):
n, g, l, mu = generateKey()
# choose rand r
ra = random.randint(0, n - 1)
rb = random.randint(0, n - 1)
b1 = n - b
print("ra =", ra)
print("rb =", rb)
a1 = encryption(g, a, ra, n)
b1 = encryption(g, b1, rb, n)
# multiplying encrypted numbers is equal to adding unencrypted numbers
s1 = (a1 * b1) % (n*n)
s = decryption(s1, n, l, mu)
if (s == (a - b)):
print(a, "-", b, "=", s)
else:
print("ERROR\n")
def isPrime(x):
if (x == 2):
return(True)
i = 0
while(i < 100):
a = random.randint(0, x)
if (math.gcd(a, x) != 1):
return(False)
if (pow(a, x - 1, x) != 1):
return(False)
i += 1
return(True)
def generateKey():
while(True):
# choose random options p and q
p = random.randint(10000, 90000000)
while (isPrime(p) != True):
p = random.randint(10000, 90000000)
q = random.randint(10000, 90000000)
while (isPrime(q) != 1):
q = random.randint(10000, 90000000)
n = p * q
N = n * n
f = (p - 1) * (q - 1)
# !condition!
if (math.gcd(n, f) == 1):
break
l = math.lcm(p - 1, q - 1)
g = random.randint(0, N - 1)
while (math.gcd(g, N) != 1):
g = random.randint(0, N - 1)
mu = reciprocal(L(pow(g, l, N), n), n) % n
print("private key n =", n, "g =", g)
print("public key l =", l, "mu =", mu)
return n, g, l, mu
def noiseTest():
n, g, l, mu = generateKey()
m = 1
total = 1
r = random.randint(0, n - 1)
c = encryption(g, m, r, n)
for i in range(10000):
total += i
r = random.randint(0, n - 1)
c1 = encryption(g, i, r, n)
c = (c * c1) % (n*n)
m1 = decryption(c, n, l, mu)
if (total == m1):
print("\nnoiseTest success")
else:
print("\nnoiseTest failed\n")
print("decrypted number", m1, "total", total, "\n")
def main():
n, g, l, mu = generateKey()
# random open text
m = random.randint(0, 1000)
m = 0
print("open text", m)
# choose rand r
r = random.randint(0, n - 1)
print("r =", r)
c = encryption(g, m, r, n)
print("encrypted number", c)
m1 = decryption(c, n, l, mu)
if (m == m1):
print("decrypted number", m1, "\n")
else:
print("ERROR \n")
# testing of homomorphic properties of a cryptosystem
homomorphismSum(500, random.randint(10, 300))
homomorphismDiff(500, random.randint(10, 300))
noiseTest();
main()