Ver código fonte

finally fixed the encryption

justtheboss97 4 anos atrás
pai
commit
d271f407bc
5 arquivos alterados com 36 adições e 29 exclusões
  1. 0 0
      data/block.json
  2. 17 13
      gui/data.py
  3. 13 13
      gui/encyption.py
  4. 1 0
      lib/block.py
  5. 5 3
      lib/transaction.py

Diferenças do arquivo suprimidas por serem muito extensas
+ 0 - 0
data/block.json


+ 17 - 13
gui/data.py

@@ -1,14 +1,17 @@
-from tkinter import *
-from lib.chain import Chain, Block
-from lib.transaction import Transaction
-from lib.block import CURRENT
-import rpyc
-import time
 import json
-from create_keys import make_keys, get_private_key, get_plain_key
 import os
+import time
+from tkinter import *
+
+import rpyc
+from create_keys import get_plain_key, get_private_key, make_keys
+from lib.block import CURRENT
+from lib.chain import Block, Chain
+from lib.transaction import Transaction
+
 from gui.encyption import decrypt_data
 
+
 # from gui.encyption import decrypt
 def new_contract():
     def add():
@@ -37,8 +40,9 @@ def new_contract():
             t = Transaction()
             t.set_contract(contract)
             t.hash(contract=name)
+            temp = t.serialize(out_json=True)
             conn = rpyc.connect(host='localhost', port=42069, keepalive=True)
-            conn.root.push_transaction(t.serialize(out_json=True))
+            conn.root.push_transaction(temp)
 
             conn.close()
             newWindow.destroy()
@@ -138,7 +142,7 @@ def add_term():
 
         t = Transaction()
         t.set_contract(update)
-        t.hash(contract=iden)
+        t.hash(contract=str(contract.get()))
         conn.root.push_transaction(t.serialize(out_json=True))
 
         #### TEST CODE PLEASE IGONRE ####
@@ -154,7 +158,7 @@ def add_term():
 
         t = Transaction()
         t.set_contract(update)
-        t.hash(contract=iden)
+        t.hash(contract=str(contract.get()))
         conn.root.push_transaction(t.serialize(out_json=True))
 
         #### END TEST CODE PLEASE DONT IGONRE ####
@@ -294,8 +298,8 @@ def get_updates(transactions, iden=None, attr=None, accepted=None):
     return updates
 
 def decrypt_transactions(transactions, name):
-    decrypted_data = [decrypt_data(name, x) for x in transactions]
-
+    decrypted_data = [json.loads(decrypt_data(name, x)) for x in transactions]
+    print(type(decrypted_data[0]))
 
     # old code
     # for encrypted in transactions:
@@ -309,4 +313,4 @@ def decrypt_transactions(transactions, name):
     #     )
     #     decrypted.append(decrypting_message)
 
-    return decrypted_data
+    return decrypted_data

+ 13 - 13
gui/encyption.py

@@ -1,11 +1,10 @@
+from codecs import encode
+import rsa
+from create_keys import get_keys, get_private_key, get_symmetric_key
+from cryptography.fernet import Fernet
 from cryptography.hazmat.backends import default_backend
-from cryptography.hazmat.primitives import serialization
-from cryptography.hazmat.primitives import hashes
+from cryptography.hazmat.primitives import hashes, serialization
 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): 
@@ -14,18 +13,19 @@ def encrypt_data(contract, block_string):
 
     chiper = Fernet(symmetric_key)
 
-    data = str(chiper.encrypt(block_string))
+    data = str(chiper.encrypt(block_string))[2:-1]
 
-    encrypted_key = str(rsa.encrypt(symmetric_key,pubkey))
+    encrypted_key = str(rsa.encrypt(symmetric_key,pubkey))[2:-1]
+    # encrypted_key = rsa.encrypt(symmetric_key,pubkey)
+    print('ENCRYPTED KEY: ', type(encrypted_key), encrypted_key)
     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)
+    en_key = encode(t.encrypted_key.encode().decode('unicode_escape'),"raw_unicode_escape")
     key = rsa.decrypt(en_key, private_key)
     cipher = Fernet(key)
-    decrypted_data = cipher.decrypt(t.data)
-    print(decrypted_data.decode(), t.data)
+    decrypted_data = cipher.decrypt(bytes(t.data, 'utf-8'))
+    print(decrypted_data)
+    return decrypted_data.decode('utf8').replace("'", '"')

+ 1 - 0
lib/block.py

@@ -29,6 +29,7 @@ class Block:
         self.nonce = self.height
 
     def add_hashed_transaction(self, hashed: str):
+        print(hashed)
         trans = Transaction()
         trans.from_serialized(hashed)
         self.transactions.append(trans)

+ 5 - 3
lib/transaction.py

@@ -3,10 +3,12 @@ import json
 import time
 import uuid
 
+from create_keys import get_keys, get_symmetric_key, make_keys
 from gui.encyption import encrypt_data
-from create_keys import make_keys, get_keys, get_symmetric_key
+
 from lib.contract import Contract
 
+
 class Transaction:
     def __init__(self):
         self.id = None
@@ -24,7 +26,7 @@ class Transaction:
         self.data = obj['data']
         self.timestamp = obj['timestamp']
         self.key = obj['key']
-        self.encrypted_key = obj['encrypted key']
+        self.encrypted_key = obj['encrypted_key']
         
 
     def set_serialized_contract(self, serialized):
@@ -53,7 +55,7 @@ class Transaction:
             'timestamp': self.timestamp,
             'data': self.data,
             'key': self.key,
-            'encrypted key': self.encrypted_key
+            'encrypted_key': self.encrypted_key
         }
         self.log(f'Serialized')
         if out_json:

Alguns arquivos não foram mostrados porque muitos arquivos mudaram nesse diff