gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r25644 - gnunet-update/src/gnunet_update


From: gnunet
Subject: [GNUnet-SVN] r25644 - gnunet-update/src/gnunet_update
Date: Mon, 24 Dec 2012 19:29:36 +0100

Author: harsha
Date: 2012-12-24 19:29:36 +0100 (Mon, 24 Dec 2012)
New Revision: 25644

Modified:
   gnunet-update/src/gnunet_update/util.py
Log:
Symmetric Encryption

Modified: gnunet-update/src/gnunet_update/util.py
===================================================================
--- gnunet-update/src/gnunet_update/util.py     2012-12-24 12:08:19 UTC (rev 
25643)
+++ gnunet-update/src/gnunet_update/util.py     2012-12-24 18:29:36 UTC (rev 
25644)
@@ -29,7 +29,10 @@
 import shutil
 import platform
 import subprocess
-
+import sys    
+import termios
+import fcntl
+from Crypto.Cipher import AES
 from metadata import Metadata
 from file import DependencyFileObject
 
@@ -357,10 +360,11 @@
     """Returns an ASCII encoding of the given data block like
     GNUNET_STRINGS_data_to_string() function.
     
-    data: The block of data which has to be encoded
+    data: A bytearray representing the block of data which has to be encoded
     """
-    echart = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
+    echart = "0123456789ABCDEFGHIJKLMNOPQRSTUV"    
     assert (None != data)
+    assert (bytearray == type(data))
     size = len(data)
     assert (0 != size)
     vbit = 0
@@ -370,7 +374,7 @@
     out = ""
     while (rpos < size) or (vbit > 0):
         if (rpos < size) and (vbit < 5):
-            bits = (bits << 8) | ord (data[rpos]) # eat 8 more bits
+            bits = (bits << 8) | data[rpos] # eat 8 more bits
             rpos += 1
             vbit += 8
         if (vbit < 5):
@@ -384,9 +388,6 @@
     return out;
              
 # From 
http://love-python.blogspot.de/2010/03/getch-in-python-get-single-character.html
-import sys    
-import termios
-import fcntl
 
 def getch(rfile, fd=None):
     """Returns a character read from file like UNIX-style fgetch() function
@@ -417,3 +418,45 @@
     if input_ok:
         return c
     return None
+
+class AESKey:
+    """Class for AES Keys. Contains the main key and the initialization
+    vector. """
+
+    key = None                  # The actual AES key
+    iv = None                   # The initialization vector
+    cipher = None               # The cipher object
+    KEY_SIZE = 32               # AES 256-bit key = 32 bytes
+    IV_SIZE = AES.block_size    # Initialization vector size (= AES block size)
+    
+    def __init__ (self, passphrase):
+        """Creates a new AES key. 
+        
+        passphrase: string containing the passphrase to get the AES key and
+                      initialization vector
+        """
+        passphrase = bytearray (passphrase, 'utf-8');
+        self.key = bytearray (self.KEY_SIZE)
+        self.iv = bytearray (self.IV_SIZE)
+        if (len (passphrase) > self.KEY_SIZE):
+            self.key = passphrase[:self.KEY_SIZE]
+            passphrase = passphrase[:self.KEY_SIZE]
+            if (len (passphrase) > self.IV_SIZE):
+                self.iv = passphrase[:self.IV_SIZE]
+            else:
+                self.iv[0:len (passphrase)] = passphrase
+        else:
+            self.key[0:len (passphrase)] = passphrase
+        self.key = str (self.key)
+        self.iv = str (self.iv)
+        assert (len(self.key) == self.KEY_SIZE)
+        assert (len(self.iv) == self.IV_SIZE)
+
+def aes_encrypt (aes_key, data):
+    cipher = AES.new (aes_key.key, AES.MODE_CFB, aes_key.iv)
+    return cipher.encrypt (data)
+
+def aes_decrypt (aes_key, data):
+    cipher = AES.new (aes_key.key, AES.MODE_CFB, aes_key.iv)
+    ptext = cipher.decrypt (aes_key.iv + data)
+    return ptext[aes_key.IV_SIZE:]




reply via email to

[Prev in Thread] Current Thread [Next in Thread]