Browse Source

encryption testing

justtheboss97 4 years ago
parent
commit
83c23f4b3e
5 changed files with 35 additions and 45 deletions
  1. 9 16
      create_keys.py
  2. 0 1
      data/block.json
  3. 0 9
      gui.py
  4. 21 12
      gui/data.py
  5. 5 7
      lib/transaction.py

+ 9 - 16
create_keys.py

@@ -1,15 +1,5 @@
-import rsa
 import os
 import os
-#from cryptography.fernet import Fernet
-# create the symmetric key only for the JSON file - we are going to only encrypt the keys 
-#key = Fernet.generate_key()
-
-# write the symmetric key to a file
-#k = open('symmetric.key','wb')
-#k.write(key)
-#k.close()
-
-# create the pub & private keys for the parties
+import rsa as rsa2
 
 
 from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.primitives.asymmetric import rsa
 from cryptography.hazmat.primitives.asymmetric import rsa
@@ -55,12 +45,15 @@ def make_keys(contract_name):
 def get_keys(contract_name):
 def get_keys(contract_name):
     path = 'contract_keys\\' + contract_name  + '\\publickey.key'
     path = 'contract_keys\\' + contract_name  + '\\publickey.key'
 
 
+    # with open(path, 'rb') as f:
+    #     public_key = serialization.load_pem_public_key(
+    #         f.read(),
+    #         backend=default_backend()
+    #         )
+    #     return public_key
+
     with open(path, 'rb') as f:
     with open(path, 'rb') as f:
-        public_key = serialization.load_pem_public_key(
-            f.read(),
-            backend=default_backend()
-            )
-        return public_key
+        return rsa2.PublicKey.load_pkcs1(f.read())
 
 
 def get_symmetric_key(contract_name):
 def get_symmetric_key(contract_name):
     path = 'contract_keys\\' + contract_name  + '\\symmetric.key'
     path = 'contract_keys\\' + contract_name  + '\\symmetric.key'

File diff suppressed because it is too large
+ 0 - 1
data/block.json


+ 0 - 9
gui.py

@@ -30,15 +30,6 @@ Button(root, text="New contract", command=new_contract).grid(row=1, column=0, pa
 Button(root, text="Find contract", command=find_transaction).grid(row=1, column=1, padx=10, pady=5)
 Button(root, text="Find contract", command=find_transaction).grid(row=1, column=1, padx=10, pady=5)
 Button(root, text="Add term", command=add_term).grid(row=1, column=2, padx=10, pady=5)
 Button(root, text="Add term", command=add_term).grid(row=1, column=2, padx=10, pady=5)
 
 
-# Button(root, text="View contract", command=myClick).grid(row=0, column=0)
-
-# Button(root, text="Update contract", command=update).grid(row=2, column=0)
-
-# Button(root, text="Accept updates", command=accept_update).grid(row=3, column=0)
-
-# Button(root, text="View pending updates", command=view_updates).grid(row=4, column=0)
-
-
 
 
 root.mainloop()
 root.mainloop()
 
 

+ 21 - 12
gui/data.py

@@ -9,7 +9,8 @@ from create_keys import make_keys, get_private_key, get_plain_key
 import os
 import os
 from cryptography.hazmat.primitives import hashes
 from cryptography.hazmat.primitives import hashes
 from cryptography.hazmat.primitives.asymmetric import padding
 from cryptography.hazmat.primitives.asymmetric import padding
-
+from cryptography.fernet import Fernet
+import rsa
 
 
 def new_contract():
 def new_contract():
     def add():
     def add():
@@ -120,7 +121,7 @@ def add_term():
         attr = str(change.get())
         attr = str(change.get())
         value = str(values.get())
         value = str(values.get())
         comment = str(comments.get())
         comment = str(comments.get())
-        transactions = [x.data for x in conn.root.find_transactions(iden)]
+        transactions = conn.root.find_transactions(iden)
         transactions = decrypt_transactions(transactions, str(contract.get()))
         transactions = decrypt_transactions(transactions, str(contract.get()))
         state = current_contract_state(transactions, iden)
         state = current_contract_state(transactions, iden)
 
 
@@ -299,17 +300,25 @@ def get_updates(transactions, iden=None, attr=None, accepted=None):
     return updates
     return updates
 
 
 def decrypt_transactions(transactions, name):
 def decrypt_transactions(transactions, name):
-    private_key = get_plain_key(name)
+    private_key = get_private_key(name)
     decrypted = []
     decrypted = []
+
     for encrypted in transactions:
     for encrypted in transactions:
-        decrypting_message = private_key.decrypt(
-            encrypted,
-            padding.OAEP(
-                mgf=padding.MGF1(algorithm=hashes.SHA256()),
-                algorithm=hashes.SHA256(),
-                label=None
-            )
-        )
-        decrypted.append(decrypting_message)
+        decrypted_key = rsa.decrypt(encrypted['encrypted key'],private_key)
+        cipher = Fernet(decrypted_key)
+        decrypted.append(cipher.decrypt(encrypted['data']))
+
+
+    # old code
+    # for encrypted in transactions:
+    #     decrypting_message = private_key.decrypt(
+    #         encrypted,
+    #         padding.OAEP(
+    #             mgf=padding.MGF1(algorithm=hashes.SHA256()),
+    #             algorithm=hashes.SHA256(),
+    #             label=None
+    #         )
+    #     )
+    #     decrypted.append(decrypting_message)
 
 
     return decrypted
     return decrypted

+ 5 - 7
lib/transaction.py

@@ -3,14 +3,14 @@ import json
 import time
 import time
 import uuid
 import uuid
 
 
-from create_keys import make_keys, get_keys
+from create_keys import make_keys, get_keys, get_symmetric_key
 from lib.contract import Contract
 from lib.contract import Contract
 from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.backends import default_backend
 from cryptography.hazmat.primitives import serialization
 from cryptography.hazmat.primitives import serialization
 from cryptography.hazmat.primitives import hashes
 from cryptography.hazmat.primitives import hashes
 from cryptography.hazmat.primitives.asymmetric import padding
 from cryptography.hazmat.primitives.asymmetric import padding
 from cryptography.fernet import Fernet
 from cryptography.fernet import Fernet
-
+import rsa
 
 
 class Transaction:
 class Transaction:
     def __init__(self):
     def __init__(self):
@@ -54,6 +54,7 @@ class Transaction:
             'timestamp': self.timestamp,
             'timestamp': self.timestamp,
             'data': self.data,
             'data': self.data,
             'key': self.key,
             'key': self.key,
+            'encrypted key': self.encrypted_key
         }
         }
         self.log(f'Serialized')
         self.log(f'Serialized')
         if out_json:
         if out_json:
@@ -80,9 +81,9 @@ class Transaction:
 
 
         chiper = Fernet(symmetric_key)
         chiper = Fernet(symmetric_key)
 
 
-        encrypted_data = chiper.encrypt(block_string)
+        self.data = chiper.encrypt(block_string)
 
 
-        encrypted_key = rsa.encrypt(symmetric_key,pubkey) # we need an extra field in the transaction
+        self.encrypted_key = rsa.encrypt(symmetric_key,pubkey) # we need an extra field in the transaction
         # encrypted_data = pubkey.encrypt(
         # encrypted_data = pubkey.encrypt(
         #     block_string,
         #     block_string,
         #     padding.OAEP(
         #     padding.OAEP(
@@ -91,11 +92,8 @@ class Transaction:
         #         label=None
         #         label=None
         #     )
         #     )
         # )
         # )
-
-        self.hash_value = encrypted_data
         self.log(f'Hashed: {self.hash_value}')
         self.log(f'Hashed: {self.hash_value}')
 
 
-        self.data = encrypted_data
 
 
     def log(self, text):
     def log(self, text):
         print(f'[ TRANS ] {text}')
         print(f'[ TRANS ] {text}')

Some files were not shown because too many files changed in this diff