Ahmad Hammoudeh commited on
Commit
d374750
·
1 Parent(s): 9b7b20b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -118
app.py CHANGED
@@ -16,124 +16,7 @@ import rsa
16
  import string
17
 
18
 
19
-
20
- def seedx(path , User_passcode=0, N_days=1, encryption_difficulty = 2**10):
21
- try:
22
- with open(path +'indx.txt') as f:
23
- old_day_index = int(f.read())
24
- except:
25
- old_day_index = 0
26
- print('no old index was found')
27
- timestamp = datetime.timestamp(datetime.now())
28
- #total number of days since unix time
29
- days_span = timestamp/math.factorial(6)/120 + 0.1#*random.random() #first time calculate the days since unix, thae latter randomize the time zone by up to ~7 hours
30
- day_index = math.floor(days_span/N_days)
31
- seed = int(day_index**2 + User_passcode)
32
- if day_index == old_day_index:
33
- with open('prvt.pem', 'rb') as file:
34
- key_data = file.read()
35
- privateKey = rsa.PrivateKey.load_pkcs1(key_data)
36
- with open('pblc.pem', 'rb') as file:
37
- key_data = file.read()
38
- publicKey = rsa.PublicKey.load_pkcs1(key_data)
39
- else:
40
- publicKey, privateKey = rsa.newkeys(encryption_difficulty)
41
- old_day_index = day_index
42
- #save in a folder
43
- with open(path +'indx.txt', 'w') as f:
44
- f.write(str(old_day_index))
45
- with open(path +'prvt.pem', 'w') as f:
46
- f.write(privateKey.save_pkcs1().decode('utf-8'))
47
- with open(path +'pblc.pem', 'w') as f:
48
- f.write(publicKey.save_pkcs1().decode('utf-8'))
49
-
50
- return {'seed':seed,'publicKey': publicKey, 'privateKey': privateKey}
51
-
52
-
53
- def AES_encrypt(User_message,seed, publicKey,encoding = 1, padding = 30):
54
- #number conversion to encrypt non-english content
55
- User_message_numbered = (int.from_bytes(bytes(User_message, 'utf-16'), "big"))
56
- #padding
57
- front_padding = ''.join(random.choices(string.digits, k=padding)) #string.ascii_lowercase +
58
- back_padding = ''.join(random.choices(string.digits, k=padding)) #string.ascii_lowercase +
59
- msg = front_padding + str(User_message_numbered) + back_padding
60
- msg_len =len(msg) #total number of charachters in the msg
61
- permutation_list = list(range(0,msg_len))
62
- random.seed(seed)
63
- random.shuffle(permutation_list)
64
- shffled_msg_list = [msg[i] for i in permutation_list]
65
- shffled_msg = ''.join(shffled_msg_list)
66
- #print(shffled_msg)
67
- encMessage = AESCipher(str(publicKey.n)).encrypt(shffled_msg)
68
- if encoding == 0:
69
- encMessage_int = encMessage
70
- elif encoding == 1:
71
- encMessage_int = encMessage.decode('utf-16')
72
- else:
73
- encMessage_int = int.from_bytes(encMessage, "big")
74
- return encMessage_int
75
-
76
-
77
- class AESCipher(object):
78
- def __init__(self, key):
79
- self.bs = AES.block_size
80
- self.key = hashlib.sha256(key.encode()).digest()
81
- def encrypt(self, raw):
82
- raw = self._pad(raw)
83
- iv = Random.new().read(AES.block_size)
84
- cipher = AES.new(self.key, AES.MODE_CBC, iv)
85
- return base64.b64encode(iv + cipher.encrypt(raw.encode()))
86
- def decrypt(self, enc):
87
- enc = base64.b64decode(enc)
88
- iv = enc[:AES.block_size]
89
- cipher = AES.new(self.key, AES.MODE_CBC, iv)
90
- return AESCipher._unpad(cipher.decrypt(enc[AES.block_size:])).decode('utf-8')
91
- def _pad(self, s):
92
- return s + (self.bs - len(s) % self.bs) * chr(self.bs - len(s) % self.bs)
93
- @staticmethod
94
- def _unpad(s):
95
- return s[:-ord(s[len(s)-1:])]
96
-
97
- def invert_perm_list(p):
98
- return [p.index(l) for l in range(len(p))]
99
-
100
- def AES_decrypt(encMessage,seed,publicKey, encoding = 1, encryption_difficulty = 2**10, padding = 30):
101
- if encoding == 0:
102
- encMessage_byte = encMessage
103
- elif encoding == 1:
104
- encMessage_byte = encMessage.encode('utf-16')
105
- else:
106
- encMessage_byte = encMessage.to_bytes((encMessage.bit_length() + 7) // 8, "big")
107
- decMessage = AESCipher(str(publicKey.n)).decrypt(encMessage_byte)
108
- decMessage_len =len(decMessage) #total number of charachters in the msg
109
- permutation_list = list(range(0,decMessage_len))
110
- random.seed(seed)
111
- random.shuffle(permutation_list)
112
- inverted_permutation_list = invert_perm_list(permutation_list)
113
- recovered_list = [decMessage[i] for i in inverted_permutation_list]
114
- recovered_msg_i_pad = ''.join(recovered_list)
115
- recovered_msg_i = int(recovered_msg_i_pad[padding:-padding])
116
- recovered_msg = recovered_msg_i.to_bytes((recovered_msg_i.bit_length() + 7) // 8, "big").decode("utf-16")
117
- return recovered_msg
118
-
119
-
120
-
121
- def Encrypt_msg(User_message,user_password,path):
122
- User_passcode = (int.from_bytes(bytes(str(user_password), 'utf-8'), "little"))
123
- Num_dict_enc = seedx(path, User_passcode)
124
- encMessage_i = AES_encrypt(User_message,Num_dict_enc['seed'], Num_dict_enc['publicKey'],encoding = 1, padding = 30)
125
- #print(encMessage_i)
126
- return encMessage_i
127
-
128
-
129
- def Decrypt_msg(encMessage,user_password,path):
130
- User_passcode = (int.from_bytes(bytes(str(user_password), 'utf-8'), "little"))
131
- Num_dict_enc = seedx(path, User_passcode)
132
- recovered_msg = AES_decrypt(encMessage,Num_dict_enc['seed'], Num_dict_enc['publicKey'], encoding = 1, encryption_difficulty = 2**10, padding = 30)
133
- #print(recovered_msg)
134
- return recovered_msg
135
-
136
-
137
 
138
 
139
  def Encrypt_users_txt(Txt):
 
16
  import string
17
 
18
 
19
+ from msg_encX import *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
 
22
  def Encrypt_users_txt(Txt):