Skip to content

Commit eb5a03b

Browse files
committed
initial commit
1 parent 296007f commit eb5a03b

28 files changed

+370
-0
lines changed

__pycache__/client.cpython-38.pyc

1.55 KB
Binary file not shown.

__pycache__/config.cpython-36.pyc

362 Bytes
Binary file not shown.

__pycache__/config.cpython-38.pyc

377 Bytes
Binary file not shown.

__pycache__/db.cpython-38.pyc

1.48 KB
Binary file not shown.

__pycache__/env.cpython-36.pyc

558 Bytes
Binary file not shown.

__pycache__/env.cpython-38.pyc

566 Bytes
Binary file not shown.

__pycache__/fm.cpython-38.pyc

876 Bytes
Binary file not shown.

__pycache__/modulo.cpython-36.pyc

478 Bytes
Binary file not shown.

__pycache__/modulo.cpython-38.pyc

490 Bytes
Binary file not shown.

__pycache__/paillier.cpython-36.pyc

2.67 KB
Binary file not shown.

__pycache__/paillier.cpython-38.pyc

2.36 KB
Binary file not shown.

__pycache__/primes.cpython-36.pyc

801 Bytes
Binary file not shown.

__pycache__/primes.cpython-38.pyc

807 Bytes
Binary file not shown.

__pycache__/server.cpython-38.pyc

922 Bytes
Binary file not shown.

client.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import fm
2+
from paillier import PublicKey, PrivateKey, encrypt, decrypt
3+
import env
4+
import server
5+
6+
7+
pub_key = PublicKey(env.n, env.g)
8+
priv_key = PrivateKey(env.p, env.q)
9+
10+
def insert_employee():
11+
print('*** Insert employee ***')
12+
id = int(input('Id: '))
13+
name = fm.convertTextToBigInt(input('Name: '))
14+
salary = int(input('Salary: '))
15+
bonus = int(input('Bonus: '))
16+
record = {
17+
'id': id,
18+
'name': encrypt(name, pub_key),
19+
'salary': encrypt(salary, pub_key),
20+
'bonus': encrypt(bonus, pub_key)
21+
}
22+
server.insert('employees', record)
23+
print("Successfully insert!")
24+
print(record)
25+
26+
def select_employee():
27+
print('*** Select employee ***')
28+
id = int(input('Id: '))
29+
record = server.select('employees', id)
30+
name = fm.convertBigIntToText(decrypt(int(record[1]), pub_key, priv_key))
31+
salary = decrypt(int(record[2]), pub_key, priv_key)
32+
bonus = decrypt(int(record[3]), pub_key, priv_key)
33+
print('(id, name, salary, bonus) = ')
34+
return (id, name, salary, bonus)
35+
36+
def select_employee_earning():
37+
''' Get information (id, name, earning = salary + bonus) '''
38+
print('*** Compute the total earning (salary + bonus) ***')
39+
id = int(input('Id: '))
40+
earning_cp = server.compute_add('employees', id, 'salary', 'bonus')
41+
earning = decrypt(earning_cp, pub_key, priv_key)
42+
return earning
43+
44+

config.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class DB_INFO():
2+
HOST = 'localhost'
3+
USER = 'pauker'
4+
PASSWORD = 'Canh1997'
5+
DATABASE = 'vkr'

db.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import pymysql
2+
from config import DB_INFO
3+
4+
class DB:
5+
def __init__(self, table_name):
6+
self.table_name = table_name
7+
self.db = pymysql.connect(host=DB_INFO.HOST, user=DB_INFO.USER, password=DB_INFO.PASSWORD, database=DB_INFO.DATABASE)
8+
self.cursor = self.db.cursor()
9+
10+
def insert(self, record):
11+
# record as dictionary, for example record = { 'id': '163388383', 'firstname': 'Canh', 'lastname': 'Pham'}
12+
cols = ','.join([key for key in record.keys()])
13+
values = ','.join(["'%s'" % record[key] for key in record.keys()])
14+
self.cursor.execute("insert into %s(%s) values(%s)" % (self.table_name, cols, values))
15+
self.db.commit()
16+
self.db.close()
17+
18+
def select(self, record_id, cols_fm):
19+
self.cursor.execute("select %s from %s where id=%d" % (cols_fm, self.table_name, record_id))
20+
record = self.cursor.fetchone()
21+
return record
22+
23+
def update(self, record):
24+
pass
25+
26+
# id = input('id: ')
27+
# fn = input('fn: ')
28+
# ln = input('ln: ')
29+
# people_table = DB('people')
30+
# people_table.create({
31+
# 'id': id,
32+
# 'firstname': fn,
33+
# 'lastname': ln
34+
# })

env.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
n = 7596238295992501343379984544921854753427452278211909359118871971667995208239296651618176087957127116930070597713367091167783114499854597260903224370068527
2+
g = 7596238295992501343379984544921854753427452278211909359118871971667995208239296651618176087957127116930070597713367091167783114499854597260903224370068528
3+
l = 7596238295992501343379984544921854753427452278211909359118871971667995208239121722500271097772284204892417914243128252357026642344249010237270160072879960
4+
m = 1999609890005243941104387215857132045082910494179242740406745949914304042451544646303689119220714376581379362826108686102169857326572572901658889022530870
5+
6+
7+
p = 94800141871526686020399314340878561844414115309419113448886350995911249316411
8+
q = 80128976033463498822512723311804908394424695447053042156700672637153047872157

fm.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
def convertTextToBin(text):
2+
return ''.join(format(ord(x), 'b').zfill(8) for x in text)
3+
4+
def convertTextToBigInt(text):
5+
text_as_bin = convertTextToBin(text)
6+
return int(text_as_bin, 2)
7+
8+
def convertBigIntToText(num):
9+
num_as_bin = bin(num)[2:]
10+
text = ''
11+
while num_as_bin != '':
12+
last_8_bit = num_as_bin[-8:]
13+
num_as_bin = num_as_bin[:-8]
14+
text += chr(int(last_8_bit, 2))
15+
return text[::-1]

main.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import paillier
2+
3+
a = paillier.generateNewKeys()
4+
print(a)

modulo.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import random
2+
3+
def invmod(num, mod):
4+
if mod == 1:
5+
raise ValueError('Modulo can not be 1.')
6+
7+
r0 = num
8+
r1 = mod
9+
s0 = 1
10+
s1 = 0
11+
12+
while r1:
13+
q = r0 // r1
14+
[r0, r1] = [r1, r0 - q * r1]
15+
[s0, s1] = [s1, s0 - q * s1]
16+
17+
if r0 == 1:
18+
return s0
19+
else:
20+
raise ValueError('%d has no inverse mod %d' % (num, mod))

paillier.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import primes
2+
import random
3+
import modulo
4+
import math
5+
import time
6+
7+
import env
8+
9+
10+
class PublicKey:
11+
def __init__(self, n = None, g = None):
12+
self.n = n
13+
self.g = g
14+
self.n_sq = n * n if n else None
15+
def generate(self, p, q):
16+
self.n = p * q
17+
self.g = self.n + 1
18+
self.n_sq = self.n * self.n
19+
def show(self):
20+
print('n = %d' % self.n)
21+
print('g = %d' % self.g)
22+
23+
class PrivateKey:
24+
def __init__(self, p, q):
25+
self.l = (p - 1) * (q - 1)
26+
n = p * q
27+
self.m = modulo.invmod(self.l, n)
28+
def show(self):
29+
print('l = %d' % self.l)
30+
print('m = %d' % self.m)
31+
32+
33+
def generateNewKeys(size):
34+
p =primes.generatePrime(size >> 1)
35+
q = primes.generatePrime(size >> 1)
36+
print(p)
37+
print(q)
38+
pub_key = PublicKey()
39+
pub_key.generate(p, q)
40+
pri_key = PrivateKey(p, q)
41+
return [pub_key, pri_key]
42+
43+
def encrypt(plain, pub_key: PublicKey):
44+
r = random.randint(1, pub_key.n)
45+
# c = g^m * r^n % n^2 = ((g^m % n^2) * (r^n % n^2)) % n^2 = (c1 * c2) % n^2
46+
c1 = pow(pub_key.g, plain, pub_key.n_sq)
47+
c2 = pow(r, pub_key.n, pub_key.n_sq)
48+
cipher = c1 * c2 % pub_key.n_sq
49+
return cipher
50+
51+
def decrypt(cipher, pub_key: PublicKey, pri_key: PrivateKey):
52+
x = pow(cipher, pri_key.l, pub_key.n_sq)
53+
Lx = (x - 1) // pub_key.n
54+
plain = Lx * pri_key.m % pub_key.n
55+
return plain
56+
57+
def e_add(cipher_1, cipher_2, pub_key: PublicKey):
58+
return cipher_1 * cipher_2 % pub_key.n_sq
59+
60+
def getBitOfPos(num, pos):
61+
return (num >> pos) & 1
62+
63+
64+
65+
# t0 = time.perf_counter()
66+
# pub_key, pri_key = generateNewKeys(512)
67+
# print("Time of key generation: ", time.perf_counter() - t0)
68+
# pub_key.show()
69+
# pri_key.show()
70+
# print("Key was generated!")
71+
# for i in range(10):
72+
# t0 = time.perf_counter()
73+
74+
# m = random.randint(0, pub_key.n)
75+
# c = encrypt(m, pub_key)
76+
# res_m = decrypt(c, pub_key, pri_key)
77+
# if (m != res_m):
78+
# print("error")
79+
# print(c)
80+
81+
# print(time.perf_counter() - t0)

primes.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import random, math
2+
import time
3+
4+
def isPropbablyPrime(n, k=128):
5+
k = int(math.log2(n))
6+
# find s and m such that n - 1 = 2^s * m, m is odd
7+
m = n - 1
8+
s = 0
9+
while not m & 1:
10+
m >>= 1
11+
s += 1
12+
13+
for i in range(k):
14+
a = random.randint(2, n - 1)
15+
b = pow(a, m, n)
16+
if b % n == 1:
17+
break
18+
else:
19+
for j in range(s+1):
20+
if b % n == n - 1:
21+
break
22+
b = pow(b, 2, n)
23+
if j == s:
24+
return False
25+
return True
26+
27+
28+
def generatePrime(size=1024):
29+
while True:
30+
num = random.getrandbits(size)
31+
num |= (1 << size - 1) | 1
32+
if isPropbablyPrime(num):
33+
return num

server.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import db
2+
import fm
3+
from paillier import PublicKey, e_add
4+
import env
5+
6+
def insert(table_name, record):
7+
table_db = db.DB(table_name)
8+
table_db.insert(record)
9+
10+
def select(table_name, record_id):
11+
pub_key = PublicKey(env.n, env.g)
12+
table_db = db.DB(table_name)
13+
record = table_db.select(record_id, '*')
14+
return record
15+
16+
def compute_add(table_name, record_id, col_1, col_2):
17+
pub_key = PublicKey(env.n, env.g)
18+
table_db = db.DB(table_name)
19+
record = table_db.select(record_id, '%s, %s' % (col_1, col_2))
20+
col_1_value = int(record[0])
21+
col_2_value = int(record[1])
22+
return e_add(col_1_value, col_2_value, pub_key)
23+

test.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import random, sys, time
2+
from paillier import PublicKey, PrivateKey, encrypt, decrypt, e_add
3+
import pymysql
4+
import env
5+
import fm
6+
7+
db = pymysql.connect(host="localhost", user="pauker", password="Canh1997", database="vkr")
8+
9+
cursor = db.cursor()
10+
11+
# id = int(input('id: '))
12+
name = fm.convertTextToBigInt(input('Name: '))
13+
salary = int(input('Salary: '))
14+
bonus = int(input('Bonus: '))
15+
16+
# fn = fm.convertTextToBigInt(input('fn: '))
17+
# ln = fm.convertTextToBigInt(input('ln: '))
18+
19+
pub_key = PublicKey(env.p, env.q)
20+
priv_key = PrivateKey(env.p, env.q)
21+
22+
23+
record = {
24+
# 'id': id,
25+
'name': encrypt(name, pub_key),
26+
'salary': encrypt(salary, pub_key),
27+
'bonus': encrypt(bonus, pub_key)
28+
}
29+
30+
31+
32+
cols = ','.join([key for key in record.keys()])
33+
values = ','.join(["'%s'" % record[key] for key in record.keys()])
34+
35+
x = cursor.execute("insert into employees(" + cols + ") values(" + values + ")")
36+
37+
db.commit()
38+
39+
db.close()

test_2.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import random, sys, time
2+
from paillier import PublicKey, PrivateKey, encrypt, decrypt
3+
import pymysql
4+
import env
5+
6+
pub_key = PublicKey()
7+
pub_key.generate(env.p, env.q)
8+
priv_key = PrivateKey(env.p, env.q)
9+
10+
11+
12+
cipher = 4569029418934595882408894686435184711753241898337409515982294979812425608353688992049953178276903628203193780432273590241137400881179420350617677405349824831881020687930893870322505212474099269434149526411761228619963371596258233105869998301743333446220271984432155548958998995398958269492848420521296521295
13+
text = decrypt(cipher, pub_key, priv_key)
14+
print(text)

test_db.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import db
2+
import fm
3+
from paillier import PublicKey, PrivateKey, encrypt, decrypt, e_add
4+
import env
5+
6+
7+
name = fm.convertTextToBigInt(input('Name: '))
8+
salary = int(input('Salary: '))
9+
bonus = int(input('Bonus: '))
10+
11+
# fn = fm.convertTextToBigInt(input('fn: '))
12+
# ln = fm.convertTextToBigInt(input('ln: '))
13+
14+
pub_key = PublicKey(env.n, env.g)
15+
priv_key = PrivateKey(env.p, env.q)
16+
17+
18+
record = {
19+
# 'id': id,
20+
'name': encrypt(name, pub_key),
21+
'salary': encrypt(salary, pub_key),
22+
'bonus': encrypt(bonus, pub_key)
23+
}
24+
25+
employees_db = db.DB('employees')
26+
employees_db.insert(record)

test_en_de.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import random, sys, time
2+
from paillier import PublicKey, PrivateKey, encrypt, decrypt
3+
import pymysql
4+
import env
5+
import fm
6+
7+
8+
pub_key = PublicKey(env.p, env.q)
9+
priv_key = PrivateKey(env.p, env.q)
10+
11+
priv_key.show()
12+
13+
name = fm.convertTextToBigInt(input('Name: '))
14+
print(name)
15+
cp_name = encrypt(name, pub_key)
16+
rv_name = decrypt(cp_name, pub_key, priv_key)
17+
rv_name = fm.convertBigIntToText(rv_name)
18+
print(rv_name)
19+
20+
21+
# salary = int(input('Salary: '))
22+
# cp_salary = encrypt(salary, pub_key)
23+
# rv_salary = decrypt(cp_salary, pub_key, priv_key)
24+
# print(rv_salary)

0 commit comments

Comments
 (0)