]> git.datanom.net - securemail.git/commitdiff
Make backwards compatible with nacl 1.0.x
authorMichael Rasmussen <mir@datanom.net>
Sat, 11 Aug 2018 23:40:24 +0000 (01:40 +0200)
committerMichael Rasmussen <mir@datanom.net>
Sat, 11 Aug 2018 23:40:24 +0000 (01:40 +0200)
cryptonize.py
user.py

index 9c1b02fdcfd17b8f179dc4228b98b0a421befe87..7a598d32cfd77cb672415d30d908d76b31b84814 100644 (file)
@@ -50,6 +50,8 @@ class Cryptonize:
         return plain
         
     def asymmetric_encrypt(self, privkey, pubkey, plain):
+        if not isinstance(plain, bytes):
+            plain = plain.encode('utf-8')
         box = Box(privkey, pubkey)
         if NACL_VERSION < "1.1.0":
             nonce = random(Box.NONCE_SIZE)
@@ -61,6 +63,8 @@ class Cryptonize:
         return cipher
         
     def asymmetric_decrypt(self, privkey, pubkey, cipher):
+        if not isinstance(cipher, bytes):
+            cipher = cipher.encode('utf-8')
         box = Box(privkey, pubkey)
         plain = box.decrypt(cipher)
         box = None
diff --git a/user.py b/user.py
index 64673c7c7ce58f5484d271cb7f85c9f5623221c3..7c1632e71129e5db64aaacca827b9312a16a3130 100644 (file)
--- a/user.py
+++ b/user.py
@@ -120,7 +120,7 @@ if __name__ == '__main__':
             for attr, value in u.__dict__.items():
                 print ('{0}: {1}'.format(attr, value))
             print ('{0} - {1} - {2}'.format(u.name, u.email, u.pubkeys))
-        from nacl.public import Box
+#        from nacl.public import Box
         c = Cryptonize()
         keypair1 = c.get_key_pair()
         keypair2 = c.get_key_pair()
@@ -128,11 +128,13 @@ if __name__ == '__main__':
             u.add_pubkey('test', keypair2[1])
         except KeyError:
             u.update_pubkey('test', keypair2[1])
-        bob_box = Box(keypair1[0], u.get_pubkey('test'))
+#        bob_box = Box(keypair1[0], u.get_pubkey('test'))
         message = "Kill all humans æøåÅØÆ"
-        encrypted = bob_box.encrypt(message.encode())
-        alice_box = Box(keypair2[0], keypair1[1])
-        plaintext = alice_box.decrypt(encrypted)
+#        encrypted = bob_box.encrypt(message.encode())
+        encrypted = c.asymmetric_encrypt(keypair1[0], u.get_pubkey('test'), message)
+#        alice_box = Box(keypair2[0], keypair1[1])
+#        plaintext = alice_box.decrypt(encrypted)
+        plaintext = c.asymmetric_decrypt(keypair2[0], keypair1[1], encrypted)
         print (plaintext.decode())
 #        c = Cryptonize()
 #        key = 'æselØre' #c.get_random_key()
This page took 0.035226 seconds and 5 git commands to generate.