Advertisement
Magnie

Small Keys (Implementation of One-Time Pads)

May 26th, 2012
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.50 KB | None | 0 0
  1. #! /usr/bin/env python
  2.  
  3. # One-Time Pad
  4. # By: Magnie (http://scratch.mit.edu/users/Magnie)
  5. # Copyright: GPLv3 (http://www.gnu.org/copyleft/gpl.html)
  6. # I don't know why I bother adding this. I doubt my code will be used
  7. # anyways, since it's probably not worth using. :P
  8.  
  9. import random
  10.  
  11. class OneTime(object):
  12.     """
  13. An attempt to make a one-time pad encryption system work with a key
  14. smaller than the text.
  15.  
  16. It relies on a secure Pseudo-Random Generator (random.Random in this
  17. case), and based on the seed/password/key given, it will generate a
  18. "random" key, allowing the transferring of smaller, simpler (yet more
  19. breakable), keys.
  20.  
  21. This follows the same caveats as the original One-Time Pad
  22. protocol. For example, you can only use the key once (unless you send
  23. a message/file of a different size than any others sent).
  24.    """
  25.    
  26.     def __init__(self):
  27.         """This sets up everything needed"""
  28.        
  29.         # This mostly just saves all the data in the class for later
  30.         # retrieval.
  31.         self.string = ""
  32.         self.key = []
  33.         self.seed = ""
  34.         self.cipher = ""
  35.    
  36.     def generate_key(self):
  37.         """
  38. Based on the seed given, and the length of the file, the
  39. final key will be different.
  40.        """
  41.         key = []
  42.         seed = sum([ord(x) + len(self.string) for x in self.seed])
  43.         generator = random.Random(seed) # Random number generator.
  44.         for i in xrange(0, len(self.string) - 1):
  45.             t1 = generator.random() # Generate random number
  46.             t2 = int(str(t1)[2:5]) # Since it's going to be a decimal
  47.                                    # take the three digits after the
  48.                                    # decimal point.
  49.             key.append(t2)
  50.        
  51.         self.key = [int(x) % 255 for x in key] # Removable.
  52.         return self.key
  53.    
  54.     def encrypt(self):
  55.         """Runs the original One-Time Pad encryption"""
  56.         cipher = ""
  57.         key = self.key
  58.         n = 0
  59.         for i in self.string:
  60.             # Shift the letter and modulo it to keep it in correct
  61.             # range to be put in binary format. Keeping the cipher
  62.             # the same length as the original text.
  63.             t1 = (ord(i) ^ key[n % len(key)]) % 255
  64.             cipher += chr(t1)
  65.             n += 1
  66.        
  67.         self.cipher = cipher
  68.         return cipher
  69.    
  70.     def decrypt(self):
  71.         """Runs the original One-Time Pad decryption"""
  72.         plain = ""
  73.         key = self.key
  74.         n = 0
  75.         for i in self.cipher:
  76.             t1 = (ord(i) ^ key[n % len(key)]) % 255
  77.             plain += chr(t1)
  78.             n += 1
  79.        
  80.         self.plain = plain
  81.         return plain
  82.  
  83. if __name__ == "__main__": # Just a list of tests.
  84.     time = OneTime()
  85.     time.seed = "testing12" # Original test - Set the seed
  86.     time.string = "I am awesome." # Set the string
  87.     print time.generate_key() # Generate key from seed
  88.     print time.encrypt() # Encrypt the data
  89.     print time.decrypt() # Decrypt the data
  90.    
  91.     time.seed = "testing13" # One bit difference in key
  92.     print time.generate_key()
  93.     print time.encrypt()
  94.     print time.decrypt()
  95.    
  96.     time.seed = "testing12" # Different length of string.
  97.     time.string = "I am awesome!!"
  98.     print time.generate_key()
  99.     print time.encrypt()
  100.     print time.decrypt()
  101.    
  102.     time.seed = "testing12" # Same length, different letters.
  103.     time.string = "I am awesome!"
  104.     print time.generate_key()
  105.     print time.encrypt()
  106.     print time.decrypt()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement