def test_creating_and_delete_rsa(self): temp_rsa = create_new_rsa() self.assertEqual(temp_rsa in the_keys(), True, "A problem on the test_creating_rsa") key_delete(temp_rsa) self.assertEqual(temp_rsa not in the_keys(), True, "A problem on the test_creating_rsa")
def send_message(): if request.method == 'POST': user = request.form.get('user') text = request.form.get('text') temp_keys = the_keys()[user] encrypted = encrypt_text(text, user) print("\n**** encrypted: "+ encrypted) pubkey = temp_keys["fromUser"] print("\n**** pubkey: "+ pubkey) send_new_message(encrypted, pubkey) message = {"from":"me","message":text} save_new_message(message,temp_keys["n"],temp_keys["e"]) return redirect(request.referrer)
def get_messages(id): temp_keys = the_keys() if str(id) in temp_keys: messages = the_message(str(id)) return render_template('messages-col.html', user=temp_keys[str(id)], messages=messages)
def create_new_user(name, fromUser, n, e): temp_keys = the_keys() in_list = False for key in temp_keys: if "fromUser" in temp_keys[key]: if temp_keys[key]["fromUser"] == fromUser: in_list = True if temp_keys[key]["n"] == 0 and temp_keys[key]["e"] == 0: temp_keys[key]["n"] = n temp_keys[key]["e"] = e save_keys(temp_keys) return (key) if not in_list: number = str(len(temp_keys) + 1) temp_keys[number] = {} temp_keys[number]["name"] = name temp_keys[number]["fromUser"] = fromUser temp_keys[number]["n"] = n temp_keys[number]["e"] = e save_keys(temp_keys) return (number)
def test_create_and_saving_and_deleting(self): name = "onur" pubkey = "onurspubkey" n = 111 e = 111 temp_user = create_new_user(name,pubkey,n,e) temp_user_object = the_keys()[temp_user] ok = False if temp_user_object["name"] == name and temp_user_object["n"] == n and temp_user_object["e"] == e: ok = True key_delete(temp_user) self.assertEqual(ok,True,"A problem on the test_encrypt_decrypt")
def messages(name): temp_keys = the_keys() for element in temp_keys: if "name" in temp_keys[element]: if temp_keys[element]["name"] == name: return render_template('messages.html', user_id=element)
def send_new_message(message, publickey): key = the_keys()["1"] data = {"app": "messagingapp", "command": "newmessage", "message": message} send(Wallet_Import(0, 0), Wallet_Import(0, 1), publickey, data=data, amount=0)
def user_list(): users = [] temp_keys = the_keys() for element in temp_keys: if "name" in temp_keys[element]: users.append(temp_keys[element]) return render_template('user-list.html', users=users)
def encrypt_text(text_data, key): temp_keys = the_keys()[key] n = temp_keys["n"] #declare n e = temp_keys["e"] #declare e encrypted_mess = "" #create for word in text_data: for char in word: encrypted_mess += str(encryption( ord(char), e, n)) + "\n" #encrypt by char and write to file encrypted_mess += str(n) + "\n" #print n and e encrypted_mess += str(e) return (encrypted_mess)
def add_new_user_request(publickey): key = the_keys()["1"] data = { "app": "messagingapp", "command": "addnewuser", "n": key["n"], "e": key["e"] } send(Wallet_Import(0, 0), Wallet_Import(0, 1), publickey, data=data, amount=0) create_new_user("unknow", publickey, 0, 0)
def save_new_message(message, n, e): temp_keys = the_keys() for temp_element in temp_keys: print("\n\n\n") print(temp_keys[temp_element]["n"] == n and temp_keys[temp_element]["e"] == e) if temp_keys[temp_element]["n"] == n and temp_keys[temp_element][ "e"] == e and "d" not in temp_keys[temp_element]: temp_message = the_message(temp_element) number = str(len(temp_message) + 1) temp_message[number] = message save_message(temp_message, temp_element) return (number)
def create_new_rsa(): p, q, n, e, d = gen_keys(KEY_BIT) from apps.Messaging_App.lib.keys_system import the_keys, save_keys temp_keys = the_keys() number = str(len(temp_keys) + 1) temp_keys[number] = {} temp_keys[number]["p"] = p temp_keys[number]["q"] = q temp_keys[number]["n"] = n temp_keys[number]["e"] = e temp_keys[number]["d"] = d # Public key(n + e) # Private key(n + d) save_keys(temp_keys) return (number)
def decrypt_text(text_data, pubkey): nums = [] for line in text_data.splitlines(): nums.append(int(line)) l = len(nums) n = nums[l - 2] e = nums[l - 1] print("\n\n\n e: " + str(e)) temp_keys = the_keys() print(temp_keys) message_publickey = pubkey.replace("\n", "").replace(" ", "") d = temp_keys["1"]["d"] for keys in temp_keys: if "fromUser" in temp_keys[keys]: key_publickey = temp_keys[keys]["fromUser"].replace("\n", "").replace( " ", "") if message_publickey == key_publickey: i = 0 decrypted_Mess = "" while i < l - 2: x = decrypt(nums[i], int(d), int(n)) y = chr(x) decrypted_Mess += y i += 1 message = {"from": "user", "message": decrypted_Mess} save_new_message(message, temp_keys[keys]["n"], temp_keys[keys]["e"]) return decrypted_Mess