Bladeren bron

encryption testing

justtheboss97 4 jaren geleden
bovenliggende
commit
83c23f4b3e
5 gewijzigde bestanden met toevoegingen van 35 en 45 verwijderingen
  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
-#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.primitives.asymmetric import rsa
@@ -55,12 +45,15 @@ def make_keys(contract_name):
 def get_keys(contract_name):
     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:
-        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):
     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="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()
 

+ 21 - 12
gui/data.py

@@ -9,7 +9,8 @@ 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
 
 def new_contract():
     def add():
@@ -120,7 +121,7 @@ def add_term():
         attr = str(change.get())
         value = str(values.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()))
         state = current_contract_state(transactions, iden)
 
@@ -299,17 +300,25 @@ def get_updates(transactions, iden=None, attr=None, accepted=None):
     return updates
 
 def decrypt_transactions(transactions, name):
-    private_key = get_plain_key(name)
+    private_key = get_private_key(name)
     decrypted = []
+
     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

+ 5 - 7
lib/transaction.py

@@ -3,14 +3,14 @@ import json
 import time
 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 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):
@@ -54,6 +54,7 @@ class Transaction:
             'timestamp': self.timestamp,
             'data': self.data,
             'key': self.key,
+            'encrypted key': self.encrypted_key
         }
         self.log(f'Serialized')
         if out_json:
@@ -80,9 +81,9 @@ class Transaction:
 
         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(
         #     block_string,
         #     padding.OAEP(
@@ -91,11 +92,8 @@ class Transaction:
         #         label=None
         #     )
         # )
-
-        self.hash_value = encrypted_data
         self.log(f'Hashed: {self.hash_value}')
 
-        self.data = encrypted_data
 
     def log(self, text):
         print(f'[ TRANS ] {text}')

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