AdamHorvath12 4 лет назад
Родитель
Сommit
b10d7fe853
2 измененных файлов с 53 добавлено и 5 удалено
  1. 25 0
      create_keys.py
  2. 28 5
      lib/transaction.py

+ 25 - 0
create_keys.py

@@ -0,0 +1,25 @@
+import rsa
+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
+(pubkey_forParty1,privkey_forParty1)=rsa.newkeys(2048)
+(pubkey_forParty2,privkey_forParty2)=rsa.newkeys(2048)
+
+# write the public key to a file
+pukey = open('publickeys.key','wb')
+pukey.write(pubkey_forParty1.save_pkcs1('PEM'))
+pukey.write(pubkey_forParty2.save_pkcs1('PEM'))
+pukey.close()
+
+# write the private key to a file
+prkey = open('privkeys.key','wb')
+prkey.write(privkey_forParty1.save_pkcs1('PEM'))
+prkey.write(privkey_forParty2.save_pkcs1('PEM'))
+prkey.close()

+ 28 - 5
lib/transaction.py

@@ -4,7 +4,8 @@ import time
 import uuid
 
 from lib.contract import Contract
-
+from cryptography.fernet import Fernet
+import rsa
 
 class Transaction:
     def __init__(self):
@@ -62,17 +63,39 @@ class Transaction:
             return True
         return False
 
+    def file_open(file):
+        key_file = open(file, 'rb')
+        key_data = key_file.read()
+        key_file.close()
+        return key_data
+
     def hash(self):
         string_object = json.dumps(self.data, sort_keys=True)
         block_string = string_object.encode()
 
-        raw_hash = hashlib.sha256(block_string)
-        hex_hash = raw_hash.hexdigest()
+        # open the public key file
+        pubkey = rsa.PublicKey.load_pkcs1(file_open('publickey.key')) #Sender public key - input field neccessary
+
+        # create the cipher
+        cipher = Fernet(pubkey)
+
+        # encrypt the data
+        encrypted_data = cipher.encrypt(block_string)
+
+
+        #these lines only neccessary for key_encryption - solve the problem of bytes
+        # open the symmetric key file for encryoting the file
+        #skey = open('symmetric.key','rb')
+        #key = skey.read()
+
+
+        # encrypt the symmetric key file with the public key
+        #encrypted_key = rsa.encrypt(key,pubkey)
 
-        self.hash_value = hex_hash
+        self.hash_value = encrypted_data
         self.log(f'Hashed: {self.hash_value}')
 
-        return hex_hash
+        return encrypted_data
 
     def log(self, text):
         print(f'[ TRANS ] {text}')