Kaynağa Gözat

more encryption fixes

justtheboss97 4 yıl önce
ebeveyn
işleme
ea0790061d
6 değiştirilmiş dosya ile 50 ekleme ve 37 silme
  1. 2 3
      create_keys.py
  2. 0 0
      data/block.json
  3. 8 20
      gui/data.py
  4. 31 0
      gui/encyption.py
  5. 8 13
      lib/transaction.py
  6. 1 1
      main.py

+ 2 - 3
create_keys.py

@@ -25,7 +25,7 @@ def make_keys(contract_name):
     os.mkdir(path)
     
     with open(path + 'publickey.key','wb') as f:
-        f.write(public_key)
+        f.write(public_key.save_pkcs1('PEM'))
 
     # pem = private_key.private_bytes(
     #     encoding=serialization.Encoding.PEM,
@@ -34,8 +34,7 @@ def make_keys(contract_name):
     # )
 
     with open(path + 'privatekey.key','wb') as f:
-        f.write(private_key)
-    
+        f.write(private_key.save_pkcs1('PEM'))    
     
     symmetric_key = Fernet.generate_key()
 

Dosya farkı çok büyük olduğundan ihmal edildi
+ 0 - 0
data/block.json


+ 8 - 20
gui/data.py

@@ -7,14 +7,13 @@ import time
 import json
 from create_keys import make_keys, get_private_key, get_plain_key
 import os
-from cryptography.hazmat.primitives import hashes
-from cryptography.hazmat.primitives.asymmetric import padding
-from cryptography.fernet import Fernet
-import rsa
+from gui.encyption import decrypt_data
 
+# from gui.encyption import decrypt
 def new_contract():
     def add():
         name = str(title.get())
+
         if not os.path.isdir('contract_keys\\' + name):
             make_keys(name)
             pubkey = get_plain_key(name)
@@ -35,11 +34,10 @@ def new_contract():
                                     }
                         }
 
-            conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
-
             t = Transaction()
             t.set_contract(contract)
             t.hash(contract=name)
+            conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
             conn.root.push_transaction(t.serialize(out_json=True))
 
             conn.close()
@@ -48,10 +46,6 @@ def new_contract():
             print('Contract name already exists, choose another')
             newWindow.destroy()
             new_contract()
-            
-        
-
-
         
 
     newWindow = Tk()
@@ -95,7 +89,7 @@ def find_transaction():
     def find():
         conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
         iden = get_plain_key(str(key.get()))
-        transactions = [x.data for x in conn.root.find_transactions(iden)]
+        transactions = conn.root.find_transactions(iden)
         transactions = decrypt_transactions(transactions, str(key.get()))
         print(current_contract_state(transactions, iden=iden))
 
@@ -108,7 +102,7 @@ def find_transaction():
     Label(newWindow, text='Contract key: ').grid(row=0, column=0)
     key = Entry(newWindow, width=35, borderwidth=5)
     key.grid(row=0, column=1, columnspan=3, padx=10, pady=10)
-    key.insert(0, '1')
+    key.insert(0, 'test')
 
     Button(newWindow, text='find contracts', command=find).grid(row=1, column=1)
 
@@ -300,13 +294,7 @@ def get_updates(transactions, iden=None, attr=None, accepted=None):
     return updates
 
 def decrypt_transactions(transactions, name):
-    private_key = get_private_key(name)
-    decrypted = []
-
-    for encrypted in transactions:
-        decrypted_key = rsa.decrypt(encrypted['encrypted key'],private_key)
-        cipher = Fernet(decrypted_key)
-        decrypted.append(cipher.decrypt(encrypted['data']))
+    decrypted_data = [decrypt_data(name, x) for x in transactions]
 
 
     # old code
@@ -321,4 +309,4 @@ def decrypt_transactions(transactions, name):
     #     )
     #     decrypted.append(decrypting_message)
 
-    return decrypted
+    return decrypted_data

+ 31 - 0
gui/encyption.py

@@ -0,0 +1,31 @@
+from cryptography.hazmat.backends import default_backend
+from cryptography.hazmat.primitives import serialization
+from cryptography.hazmat.primitives import hashes
+from cryptography.hazmat.primitives.asymmetric import padding
+from cryptography.fernet import Fernet
+import rsa
+
+from create_keys import get_keys, get_symmetric_key, get_private_key
+
+
+def encrypt_data(contract, block_string): 
+    symmetric_key = get_symmetric_key(contract)
+    pubkey = get_keys(contract)
+
+    chiper = Fernet(symmetric_key)
+
+    data = str(chiper.encrypt(block_string))
+
+    encrypted_key = str(rsa.encrypt(symmetric_key,pubkey))
+    return data, encrypted_key
+
+
+def decrypt_data(name, t):
+    print('DATA OF TRANSACTION: ', type(t.data), t.data)
+    private_key = get_private_key(name)
+    en_key = t.encrypted_key
+    print('ENCRYPTED KEY: ', type(en_key), en_key)
+    key = rsa.decrypt(en_key, private_key)
+    cipher = Fernet(key)
+    decrypted_data = cipher.decrypt(t.data)
+    print(decrypted_data.decode(), t.data)

+ 8 - 13
lib/transaction.py

@@ -3,14 +3,9 @@ import json
 import time
 import uuid
 
+from gui.encyption import encrypt_data
 from create_keys import make_keys, get_keys, get_symmetric_key
 from lib.contract import Contract
-from cryptography.hazmat.backends import default_backend
-from cryptography.hazmat.primitives import serialization
-from cryptography.hazmat.primitives import hashes
-from cryptography.hazmat.primitives.asymmetric import padding
-from cryptography.fernet import Fernet
-import rsa
 
 class Transaction:
     def __init__(self):
@@ -20,13 +15,17 @@ class Transaction:
         self.locked = False
         self.timestamp = 0
         self.key = None
+        self.encrypted_key = None
 
     def from_serialized(self, jsonobj: str):
+        print('FROM SERIALIZED: \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n')
         obj = json.loads(jsonobj)
         self.id = obj['id']
         self.data = obj['data']
         self.timestamp = obj['timestamp']
         self.key = obj['key']
+        self.encrypted_key = obj['encrypted key']
+        
 
     def set_serialized_contract(self, serialized):
         if not self.locked:
@@ -75,15 +74,11 @@ class Transaction:
         string_object = json.dumps(self.data, sort_keys=True)
         block_string = string_object.encode()
 
-        # open the public key file
-        symmetric_key = get_symmetric_key(contract)
-        pubkey = get_keys(contract)
-
-        chiper = Fernet(symmetric_key)
+        self.data, self.encrypted_key = encrypt_data(contract, block_string)
 
-        self.data = chiper.encrypt(block_string)
 
-        self.encrypted_key = rsa.encrypt(symmetric_key,pubkey) # we need an extra field in the transaction
+        # open the public key file
+         # we need an extra field in the transaction
         # encrypted_data = pubkey.encrypt(
         #     block_string,
         #     padding.OAEP(

+ 1 - 1
main.py

@@ -113,7 +113,7 @@ if __name__ == '__main__':
                 found = chain.find_transactions(address)
 
                 return found
-                return json.dumps(found, sort_keys=True)
+                # return json.dumps(found, sort_keys=True)
 
             def exposed_push_contract(self, contract):
                 global CURRENT

Bu fark içinde çok fazla dosya değişikliği olduğu için bazı dosyalar gösterilmiyor