Guest User

Encryption functions

a guest
Mar 6th, 2013
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -*- coding: UTF-8 -*-
  2. #Encrypter by BrolofTheViking
  3. #4/23/12
  4. #Cipher
  5.  
  6. from random import randint
  7. #This function takes an input string and integer and outputs an encrypted string.
  8. #Each character in the input string will be turned into multiple characters. The number of characters it turns into is equal to the input integer.
  9. #Each character has a numerical value associated with it
  10. #The value of each character in the group of characters in the encrypted string that corresponds to each character in the input string adds up to the value of the character in the input string.
  11. #For example, the character Z has a value of 13
  12. #So if the input string is just "Z" and the input integer is 3, then "Z" might be represented as "gkX" because the value of g is 2, the value of k is 10, and the value of X is 1
  13. #The encryption also puts random numbers into the output, all of which are treated as having a value of 0
  14. #So "Z" can also be represented as "8Z2"
  15. #Or as "X2*" because * has a value of 12, and X has a value of 1
  16. #The encrypter adds the
  17. global default
  18. default = 2
  19. def encrypt(string,encryptvalue = default):
  20.     if type(string) != str:
  21.         string = str(string)
  22.     if encryptvalue < 2:
  23.         encryptvalue = 2
  24.     alphabet = ['Fill','X','g','h','b','#','v','d',']','`','k','q','*','Z','D','j','p','J','W','i','T','c','%','s','!','[','Q','_','R','A','C',"'",'$','P','.',')','l','Y','f','y','n','S',' ','O','}','&','^','K','B','e','|','?','@','a','(','>',':','z','<','u','t','=','N','"','F','H',';',',','U','m','I','r','-','G','{','M','E','+','o','~','V','w','/','x','L','1','2','3','4','5','6','7','8','9','0']
  25.     encryptedstring = ""
  26.     if encryptvalue != default:
  27.         encryptedstring += str(encryptvalue)
  28.     encryptedstring += alphabet[randint(1,82)]
  29.     n = 0
  30.     while n < len(string):
  31.         charactervalue = 0
  32.         addvalue = 0
  33.         characteradded = 0
  34.         while string[n] != alphabet[charactervalue]:
  35.             charactervalue += 1
  36.         if charactervalue >= 85:
  37.             firstcharacter = True
  38.             while characteradded < encryptvalue - 1:
  39.                 if firstcharacter == True:
  40.                     random = randint(10,84)
  41.                     firstcharacter = False
  42.                 else:
  43.                     random = randint(0,charactervalue-addvalue)
  44.                 if random == 0:
  45.                     encryptedstring += str(randint(0,9))
  46.                 else:
  47.                     encryptedstring += alphabet[random]
  48.                 addvalue += random
  49.                 characteradded += 1
  50.         else:
  51.             while characteradded < encryptvalue - 1:
  52.                 random = randint(0,charactervalue-addvalue)
  53.                 if random == 0:
  54.                     encryptedstring += str(randint(0,9))
  55.                 else:
  56.                     encryptedstring += alphabet[random]
  57.                 addvalue += random
  58.                 characteradded += 1
  59.         random = charactervalue-addvalue
  60.         if random == 0:
  61.             encryptedstring += str(randint(0,9))
  62.         else:
  63.             encryptedstring += alphabet[random]
  64.         addvalue += random
  65.         n += 1
  66.     return encryptedstring
  67.  
  68. def decrypt(string):
  69.     alphabet = ['Fill','X','g','h','b','#','v','d',']','`','k','q','*','Z','D','j','p','J','W','i','T','c','%','s','!','[','Q','_','R','A','C',"'",'$','P','.',')','l','Y','f','y','n','S',' ','O','}','&','^','K','B','e','|','?','@','a','(','>',':','z','<','u','t','=','N','"','F','H',';',',','U','m','I','r','-','G','{','M','E','+','o','~','V','w','/','x','L','1','2','3','4','5','6','7','8','9','0']
  70.     set_size = 0
  71.     start = 0
  72.     while string[start] == "0" or string[start] == "1" or string[start] == "2" or string[start] == "3" or string[start] == "4" or string[start] == "5" or string[start] == "6" or string[start] == "7" or string[start] == "8" or string[start] == "9":
  73.         start += 1
  74.     if start == 0:
  75.         set_size = default
  76.     else:
  77.         set_size = int(string[:start])
  78.     start += 1
  79.     end = start + set_size
  80.     output = ""
  81.     while start < len(string):
  82.         character = 0
  83.         while start < end:
  84.             charactervalue = 0
  85.             if string[start] == "0" or string[start] == "1" or string[start] == "2" or string[start] == "3" or string[start] == "4" or string[start] == "5" or string[start] == "6" or string[start] == "7" or string[start] == "8" or string[start] == "9":
  86.                 charactervalue = 0
  87.             else:
  88.                 while string[start] != alphabet[charactervalue]:
  89.                     charactervalue += 1
  90.             character += charactervalue
  91.             start += 1
  92.         output += alphabet[character]
  93.         end += set_size
  94.     return output
Advertisement
Add Comment
Please, Sign In to add comment