Advertisement
Guest User

Untitled

a guest
Apr 10th, 2010
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.70 KB | None | 0 0
  1. ######################################################
  2. #                                                    #
  3. #  rollingcypher.py - a rolling substitution cypher  #
  4. #                                                    #
  5. #  A more complex version of the ceaser cypher.      #
  6. #                                                    #
  7. #  Every string is divided up into arbitrary length  #
  8. #  blocks, with four padded characters on the end.   #
  9. #  This padding is (a) the shift to use, and         #
  10. #  (b) the length of the next segment. This extra    #
  11. #  info is shuffled with the text before it, and     #
  12. #  provides the key to the next segment.             #
  13. #                                                    #
  14. #  The 4-character key (unencrypted) is required to  #
  15. #  decrypt the first segment, and thus the rest.     #
  16. #                                                    #
  17. ######################################################
  18.  
  19. from random import *
  20.  
  21. #Original: [" ", "A", "a", "B", "b", "C", "c", "D", "d", "E", "e", "F", "f", "G", "g", "H", "h", "I", "i", "J", "j", "K", "k", "L", "l", "M", "m", "N", "n", "O", "o", "P", "p", "Q", "q", "R", "r", "S", "s", "T", "t", "U", "u", "V", "v", "W", "w", "X", "x", "Y", "y", "Z", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "=", "+", "[", "]", "{", "}", "\\", "|", ";", ":", "'", "\"", ",", "<", ".", ">", "/", "?"]
  22. characterlist = ['j', '+', '?', '$', 'a', 'c', 'g', 'T', '\\', 's', '"', ' ', '@', '0', 't', 'p', 'L', ',', 'u', '/', 'Q', '`', 'm', 'n', 'F', '}', 'U', '#', 'l', ':', '[', '>', '6', 'w', 'o', '.', '(', '8', '9', '*', 'C', '=', 'K', '%', 'r', '!', 'v', '7', 'k', '2', '3', 'H', 'P', 'x', 'z', 'M', 'Y', 'O', 'Z', "'", 'B', '&', '~', 'V', 'h', '<', 'W', 'S', '^', 'b', 'X', '_', 'R', ')', 'i', 'D', 'E', 'y', ']', '4', 'G', '|', 'A', ';', 'N', 'e', 'I', '1', 'J', '5', 'q', 'f', '-', '{', 'd', '0']
  23.  
  24. def encrypt(cleartext, key = "0000"):
  25.     """
  26.     Encrypts the string. Key defaults to 0000, and if it is 0000, will generate a random key.
  27.    
  28.     Cleartext can be any string. Key must be 4 characters, padded with 0s if nessecary, shift then segment length
  29.     The function returns (initial key, encrypted text)
  30.     """
  31.    
  32.     ciphertext = ""
  33.     shift  = 0
  34.     seglen = 0
  35.     if key == "0000":
  36.         shift = randint(1,len(characterlist)-1)
  37.         seglen = randint(1,30)
  38.        
  39.         if shift < 10:  shiftstr = "0"+str(shift)
  40.         else:           shiftstr = str(shift)
  41.         if seglen < 10: seglenstr = "0"+str(seglen)
  42.         else:           seglenstr = str(seglen)
  43.         key = shiftstr + seglenstr
  44.     else:
  45.         shift = int(key[0:2])
  46.         seglen = int(key[2:4])
  47.    
  48.     while len(cleartext) > 0:
  49.         #loop through each segment, split it off the cleartext, encrypt, then attach it to the ciphertext with key info
  50.         segment = cleartext[:seglen]
  51.         cleartext = cleartext[seglen:]
  52.        
  53.         #Calculate the next shift so it can be attached onto the end of the cleartext strip
  54.         newshift  = randint(1,len(characterlist)-1)
  55.         newseglen = randint(1,30)
  56.         if newshift < 10: shiftstr = "0"+str(newshift)
  57.         else:             shiftstr = str(newshift)
  58.         if newseglen < 10: seglenstr = "0"+str(newseglen)
  59.         else:              seglenstr = str(newseglen)
  60.        
  61.         segment = segment + shiftstr + seglenstr
  62.        
  63.         #replace each character by shifting x items through the character list
  64.         for character in segment:
  65.             pos = 0
  66.             for position, item in enumerate(characterlist):
  67.                 if item == character:
  68.                     pos = position
  69.                     break
  70.                    
  71.             newpos = pos + shift
  72.             if newpos >= len(characterlist):
  73.                 newpos -= len(characterlist)
  74.            
  75.             ciphertext += characterlist[newpos]
  76.        
  77.         shift = newshift
  78.         seglen = newseglen
  79.    
  80.     return (ciphertext, key)
  81. #End encrypt
  82.  
  83. def decrypt(ciphertext, key):
  84.     """
  85.     Decrypts by reversing the above method. An initial key is nessecary
  86.    
  87.     Ciphertext is any string encrypted with the above function. Key must be the four character key used to encrypt
  88.     The function returns the cleartext version
  89.     """
  90.    
  91.     cleartext = ""
  92.     shift  = int(key[0:2])
  93.     seglen = int(key[2:4])
  94.    
  95.     while len(ciphertext) > 0:
  96.         #loop through each segment, split off the ciphertext, decrypt, split the key info, then attach to cleartext
  97.         segment    = ciphertext[:seglen+4]
  98.         ciphertext = ciphertext[seglen+4:]
  99.        
  100.         for character in segment:
  101.             pos = 0
  102.             for position, item in enumerate(characterlist):
  103.                 if item == character:
  104.                     pos = position
  105.                     break
  106.            
  107.             newpos = pos - shift
  108.             if newpos < 0:
  109.                 newpos += len(characterlist)
  110.            
  111.             cleartext += characterlist[newpos]
  112.        
  113.         #split off key info for the next iteration
  114.         shift  = int(cleartext[-4:-2])
  115.         seglen = int(cleartext[-2:])
  116.         cleartext = cleartext[:-4]
  117.    
  118.     return cleartext
  119. #End decrypt
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement