From 4ad80ae5d54180115f994f56e1b455386200664b Mon Sep 17 00:00:00 2001 From: Sean Batzel Date: Wed, 26 Sep 2018 12:57:37 -0400 Subject: [PATCH] All cryptographic functions are now working. Signed-off-by: Sean Batzel --- blockchain_message/src/crypt.py | 11 ++++------- test/test_crypt.py | 1 + 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/blockchain_message/src/crypt.py b/blockchain_message/src/crypt.py index ba7dff5..96ff2ed 100644 --- a/blockchain_message/src/crypt.py +++ b/blockchain_message/src/crypt.py @@ -46,8 +46,7 @@ def encrypt(message: str, recipient: Contact) -> bytes: :param recipient: The individual whose public key to encrypt with. :return: A bytestring representing the encrypted message. """ - return rsa.encrypt(message.encode('latin-1'), - rsa.PublicKey.load_pkcs1(recipient.key)) + return rsa.encrypt(message.encode('utf8'), rsa.PublicKey.load_pkcs1(recipient.key)) def decrypt(self, message: bytes) -> str: """ @@ -55,7 +54,7 @@ def decrypt(self, message: bytes) -> str: :param message: The message to decrypt. :return: A plaintext message decrypted from 'message'. """ - return rsa.decrypt(message, self.key).decode('latin-1') + return rsa.decrypt(message, self.key).decode('utf8') def sign(self, message: str) -> str: """ @@ -63,7 +62,7 @@ def sign(self, message: str) -> str: :param message: The message to sign. :return: The cryptographic signature for the given payload. """ - return rsa.sign(message.encode('latin-1'), self.key, 'SHA-1') + return rsa.sign(message.encode('utf8'), self.key, 'SHA-512').hex() @staticmethod def verify(message: str, signature: str, sender: Contact): @@ -73,9 +72,7 @@ def verify(message: str, signature: str, sender: Contact): :param signature: The signature to verify. :param sender: The contact who the message claims to be sent by. """ - m = bytes(bytes(message, 'latin-1').decode('latin-1'), 'latin-1') - s = bytes(signature, 'latin-1') - rsa.verify(m, s, rsa.PublicKey.load_pkcs1(sender.key)) + rsa.verify(message.encode('utf8'), bytes.fromhex(signature), rsa.PublicKey.load_pkcs1(sender.key)) @staticmethod def generate_key(uname: str): diff --git a/test/test_crypt.py b/test/test_crypt.py index 75bee9e..aa0c037 100644 --- a/test/test_crypt.py +++ b/test/test_crypt.py @@ -7,6 +7,7 @@ class TestCrypt(unittest.TestCase): def test_encrypt(self): c = Crypt("test") con = Contact("00000", "test", "test@test.com") + cipher = c.encrypt("This is a test.", con) def test_decrypt(self): c = Crypt("test")