AdamHorvath12 пре 4 година
родитељ
комит
1e0107af2a
2 измењених фајлова са 54 додато и 18 уклоњено
  1. 38 9
      create_keys.py
  2. 16 9
      lib/transaction.py

+ 38 - 9
create_keys.py

@@ -9,15 +9,44 @@ import rsa
 #k.close()
 
 # create the pub & private keys for the parties
+
+from cryptography.hazmat.backends import default_backend
+from cryptography.hazmat.primitives.asymmetric import rsa
+from cryptography.hazmat.primitives import serialization
+
 def make_keys(company):
-    (pubkey,privkey)=rsa.newkeys(2048)
+    #(pubkey,privkey)=rsa.newkeys(2048)
+    private_key = rsa.generate_private_key(
+        public_exponent=65537,
+        key_size=2048,
+        backend=default_backend()
+        )
+    public_key = private_key.public_key()
+
+    pem = private_key.private_bytes(
+        encoding=serialization.Encoding.PEM,
+        format=serialization.PrivateFormat.PKCS8,
+        encryption_algorithm=serialization.NoEncryption()
+    )
 
+    with open('companies\\' + company + '_publickey.key','wb') as f:
+        f.write(pem)
+
+    pem = private_key.private_bytes(
+        encoding=serialization.Encoding.PEM,
+        format=serialization.PrivateFormat.PKCS8,
+        encryption_algorithm=serialization.NoEncryption()
+    )
+
+    with open('companies\\' + company + '_privatekey.key','wb') as f:
+        f.write(pem)    
+    
     #write the public key to a file
-    pukey = open('companies\\' + company + '_publickey.key','wb')
-    pukey.write(pubkey.save_pkcs1('PEM'))
-    pukey.close()
-
-    # write the private key to a file
-    prkey = open('companies\\' + company + '_privatekey.key','wb')
-    prkey.write(privkey.save_pkcs1('PEM'))
-    prkey.close()
+    # pukey = open('companies\\' + company + '_publickey.key','wb')
+    # pukey.write(pubkey.save_pkcs1('PEM'))
+    # pukey.close()
+
+    # # write the private key to a file
+    # prkey = open('companies\\' + company + '_privatekey.key','wb')
+    # prkey.write(privkey.save_pkcs1('PEM'))
+    # prkey.close()

+ 16 - 9
lib/transaction.py

@@ -6,8 +6,10 @@ import os
 
 from create_keys import make_keys
 from lib.contract import Contract
-from cryptography.fernet import Fernet
-import rsa
+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
 
 class Transaction:
     def __init__(self):
@@ -71,7 +73,10 @@ class Transaction:
             make_keys(recipient)
 
         with open(path, 'rb') as f:
-            return rsa.PublicKey.load_pkcs1(f.read())
+            public_key = serialization.load_pem_public_key(
+                f.read(),
+                backend=default_backend()
+            return f
     
         # key_file = open(file, 'rb')
         # key_data = key_file.read()
@@ -84,13 +89,15 @@ class Transaction:
 
         # open the public key file
         pubkey = self.get_keys(recipient) #Sender public key - input field neccessary
-        print(pubkey)
-        # create the cipher
-        cipher = Fernet(pubkey)
-
-        # encrypt the data
-        encrypted_data = cipher.encrypt(block_string)
 
+        encrypted_data = pubkey.encrypt(
+            block_string,
+            padding.OAEP(
+                mgf=padding.MGF1(algorithm=hashes.SHA256()),
+                algorithm=hashes.SHA256(),
+                label=None
+            )
+        )
 
         #these lines only neccessary for key_encryption - solve the problem of bytes
         # open the symmetric key file for encryoting the file