forked from cy1c0n9/SecureMultiPartyComputation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrypto.py
More file actions
136 lines (110 loc) · 3.86 KB
/
crypto.py
File metadata and controls
136 lines (110 loc) · 3.86 KB
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
# yao garbled circuit evaluation v1. simple version based on smart
# yicong chen, dept of computing, imperial college, november 2018
import util # xor_bytes, bits
from cryptography.fernet import Fernet
from random import SystemRandom
ENCRYPTED = True
AES_BASED = True
if ENCRYPTED: # ____________________________________________________________
# secure AES based encryption
def encrypt(plain_txt, key0, key1=None):
"""
example0: encrypt(p, k0, k1) returns Ek0(Ek1(p))
example1: encrypt(p, k0) returns Ek0(p)
:param plain_txt: bytes
:param key0: bytes
:param key1: bytes
:return:
"""
if key1 is None:
if AES_BASED:
return aes_encrypt(plain_txt, key0)
else:
return xor_encrypt(plain_txt, key0)
else:
if AES_BASED:
return aes_encrypt(aes_encrypt(plain_txt, key1), key0)
else:
return xor_encrypt(xor_encrypt(plain_txt, key1), key0)
def decrypt(cipher_txt, key0, key1=None):
"""
example0: decrypt(c, k0, k1) c = Ek0(Ek1(p)) return p
example1: decrypt(c, k0) c = Ek0(p) return p
:param cipher_txt: bytes
:param key0: bytes
:param key1: bytes
:return:
"""
if key1 is None:
if AES_BASED:
return aes_decrypt(cipher_txt, key0)
else:
return xor_decrypt(cipher_txt, key0)
else:
if AES_BASED:
return aes_decrypt(aes_decrypt(cipher_txt, key0), key1)
else:
return xor_decrypt(xor_decrypt(cipher_txt, key0), key1)
def aes_encrypt(plain_txt, key):
f = Fernet(key)
token = f.encrypt(plain_txt)
return token
def aes_decrypt(cipher_txt, key):
# print("key in the aes decrypt: " + str(key))
# print("current cipher txt" + str(cipher_txt))
f = Fernet(key)
plain = f.decrypt(cipher_txt)
return plain
def xor_encrypt(plain_txt, key):
"""
:param plain_txt: bytes
:param key: bytes
:return:
"""
return b_xor(plain_txt, key)
def xor_decrypt(cipher_txt, key):
"""
:param cipher_txt: bytes
:param key: bytes
:return:
"""
# return util.xor_bytes(cipher_txt, key)
return b_xor(cipher_txt, key)
def b_xor(b1, b2):
"""
use xor for bytes
:param b1: bytes
:param b2: bytes
:return: bytes b1 ^ b2
"""
result = b""
for b1, b2 in zip(b1, b2):
result += bytes([b1 ^ b2])
return result
# generate key pair
def key_pair():
return {0: Fernet.generate_key(),
1: Fernet.generate_key()}
else: # ____________________________________________________________________
# totally insecure key-less implementation
def encrypt(plain_txt, key=0):
return plain_txt
def decrypt(cipher_txt, key=0):
return cipher_txt
# generate key pair
def key_pair():
return {0: Fernet.generate_key(),
1: Fernet.generate_key()}
# ____________________________________________________________________________
# utilities __________________________________________________________________
# shuffle the list
# example: shuffle(l) the list is shuffled after execution
# def shuffle(l):
# for i in range(len(l)-1, 0, -1):
# j = SystemRandom().randrange(i+1)
# l[i], l[j] = l[j], l[i]
# generate random 1 bit, return a int 0 or int 1
def random_1_bit():
return SystemRandom().randrange(2)
def find_key_pair_index(pair, search_key):
return [idx for idx, key_val in pair.items() if key_val == search_key][0]