-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdcs_preprocessing.py
67 lines (46 loc) · 1.83 KB
/
dcs_preprocessing.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
import os, sys, math, random
from decimal import *
'''
This isn't really encryption, but rather preprocessing on the plaintext
to prevent against distribution attacks.
'''
class DistributionConfidentialityScheme:
def __init__(self):
# Scaling bit size
self.q = 6;
# Randomization bit size
self.r = 8;
def encrypt(self, plaintext):
if not plaintext:
return None
modified_plaintext = plaintext
modification_bit_count = bin(0)[2:].zfill(self.q)
if '.' in plaintext:
modified_plaintext = plaintext.replace('.', '')
modification_bit_count = bin(len(plaintext.split('.')[1]))[2:].zfill(self.q)
random_bits = str(bin(random.randint(1,math.pow(2,self.r)))[2:].zfill(self.r))
ciphertext = modified_plaintext + modification_bit_count + random_bits
return ciphertext
# These two functions return the minimum possible encodings of plaintexts.
# They are used as upper and lower bounds on a number for executing range queries.
def encrypt_min(self, plaintext):
pass
def encrypt_max(self, plaintext):
pass
def decrypt(self, ciphertext):
if not ciphertext:
return None
non_random_ciphertext = ciphertext[:len(ciphertext) - self.r]
modified_plaintext = non_random_ciphertext[:len(non_random_ciphertext) - self.q]
modification_bit_count = int(non_random_ciphertext[len(non_random_ciphertext) - self.q:],2)
plaintext = modified_plaintext
if not modification_bit_count == 0:
plaintext = modified_plaintext[:-1*modification_bit_count] + '.' + modified_plaintext[-1*modification_bit_count:]
return plaintext
''''
dcs_instance = DistributionConfidentialityScheme()
plaintext = '1110100.0'
ciphertext = dcs_instance.encrypt(plaintext)
decrypted_plaintext = dcs_instance.decrypt(ciphertext)
print "plaintext: ", plaintext, "ciphertext: ", ciphertext, "decrypted_plaintext: ", decrypted_plaintext
'''