Advertisement
Guest User

Untitled

a guest
Mar 6th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.80 KB | None | 0 0
  1. # Brute Force Tutorial
  2.  
  3. # We need regex for checking purposes
  4. import re
  5. from itertools import combinations_with_replacement
  6.  
  7. # Step 1
  8. ## Develop a list, called letters, which contains all letters from A to Z in capitalized format
  9. letters = "abcdefghijklmnopqrstuvwxyz"
  10. letters = map(lambda x: x.upper(),letters)
  11.  
  12. # Step 2
  13. ## Develop a way to find the letters in a key, and reorganize them in a way where the key starts first, and the letters later
  14. alpha_key = "thanks"
  15.  
  16. # Transform the key into upper case
  17. alpha_key = map(lambda x: x.upper(),alpha_key)
  18.  
  19. # Letter List will hold the letters for our set that we will manipulate later
  20. letter_list = list(letters)
  21.  
  22. # Will help the alphabets map for vigenere
  23. alpha_map = []
  24.  
  25. # Loop through the key letters
  26. for character in alpha_key:
  27.  
  28. # Make sure it's a valid character
  29. if character in letters:
  30.  
  31. # Pluck the letter from the letter_list
  32. if character in letter_list:
  33.  
  34. # Locate the location of the letter
  35. i = letter_list.index(character)
  36.  
  37. # Pop it from the letter list and add it into the alpha map
  38. alpha_map.append(letter_list.pop(i))
  39.  
  40. # Combine what is remaining from the letter list to the alpha map
  41. alpha_map += letter_list
  42.  
  43. # Test the result
  44. # print alpha_map
  45.  
  46. # Retrieve Media/Prose
  47. passphrase = "WE COME FROM THE ERIDANUS SUPERVOID"
  48.  
  49. # Create a regex that only accepts non-alphabets and spaces
  50. # How it's build is by building the condition for alphabets and spaces
  51. # And then using the NOT element '^' which means all characters BUT NOT
  52. # a-z and A-Z and space.
  53. regex = re.compile('[^ a-zA-Z]')
  54.  
  55. # Strip out all non-alphabet characters using regex substitution
  56. # In essence, substitute characters that match our regex above with
  57. # '' (or blank)
  58. passphrase = regex.sub('', passphrase)
  59.  
  60. # Ensure each word is listed individually and uppercased
  61. passphrase = map(lambda x: x.upper(), passphrase.split(" "))
  62.  
  63. ### TUTORIAL
  64. # VIGENERE CIPHER METHOD
  65. # ENCODING:
  66. # Given key 'THANKS' and passphrase 'HELLO' and message 'ZOOLOGY'
  67. # The output would be 'TZKABIC'
  68. #
  69. # How?
  70. # Facts:
  71.  
  72. # The alphamap is our new letter mapping, which is listed as:
  73. # ['T', 'H', 'A', 'N', 'K', 'S', 'B', 'C', 'D', 'E', 'F', 'G', 'I', 'J', 'L', 'M', 'O', 'P', 'Q', 'R', 'U', 'V', 'W', 'X', 'Y', 'Z']
  74. # H is the 2nd (1st in list) letter of the alphabet in alphamap
  75. # Z is the 26th (25th in the list) letter of the alphabet in alphamap
  76. #
  77. # H + Z = ?
  78. # print alpha_map.index("H")
  79. # print alpha_map.index("Z")
  80. # 1 + 25 = 26
  81. # Since there are only 26 letters, we find the modulo:
  82. # 26 % 26 = 0 (position of letter T in alphamap)
  83. # But the first character is supposed to be T, which is 7 steps ahead.
  84.  
  85. class Vigenere:
  86.  
  87. def __init__(self):
  88. self.LETTERS = "abcdefghijklmnopqrstuvwxyz"
  89. self.LETTERS = map(lambda x: x.upper(),self.LETTERS)
  90.  
  91. # Develop Alpha Map
  92. def generateAlphaMap(self,key):
  93.  
  94. # Transform the key into upper case
  95. alpha_key = map(lambda x: x.upper(),key)
  96.  
  97. # Letter List will hold the letters for our set that we will manipulate later
  98. letter_list = list(self.LETTERS)
  99.  
  100. # Will help the alphabets map for vigenere
  101. alpha_map = []
  102.  
  103. # Loop through the key letters
  104. for character in alpha_key:
  105.  
  106. # Make sure it's a valid character
  107. if character in self.LETTERS:
  108.  
  109. # Pluck the letter from the letter_list
  110. if character in letter_list:
  111.  
  112. # Locate the location of the letter
  113. i = letter_list.index(character)
  114.  
  115. # Pop it from the letter list and add it into the alpha map
  116. alpha_map.append(letter_list.pop(i))
  117.  
  118. # Combine what is remaining from the letter list to the alpha map
  119. alpha_map += letter_list
  120.  
  121. # Return the result
  122. return alpha_map
  123.  
  124. # A module that cleans up non-alphabet characters
  125. def cleanUp(self,element):
  126. # Create a regex that only accepts non-alphabets and spaces
  127. # How it's build is by building the condition for alphabets and spaces
  128. # And then using the NOT element '^' which means all characters BUT NOT
  129. # a-z and A-Z and space.
  130. regex = re.compile('[^ a-zA-Z]')
  131.  
  132. # Strip out all non-alphabet characters using regex substitution
  133. # In essence, substitute characters that match our regex above with
  134. # '' (or blank)
  135. element = regex.sub('', element)
  136.  
  137. return element
  138.  
  139. # Develop Encryption Module
  140. def encrypt(self, message, key, passphrase, method = "Normal"):
  141.  
  142. # Clean up passphrase
  143. passphrase = self.cleanUp(passphrase)
  144.  
  145. # Record message into a new var
  146. message = message.upper()
  147.  
  148. # Without spaces
  149. message_stripped = message.replace(" ","")
  150.  
  151. # Get the length of the message
  152. length_of_message = len(message_stripped)
  153.  
  154. # For keyed and normal vigenere, do special stuff
  155. if method == "Keyed" or method == "Autokey":
  156.  
  157. # Generate an alphamap
  158. alphamap = self.generateAlphaMap(key)
  159.  
  160. # With some math, repeat the passphrase over the course of the letter
  161. if method == "Keyed" or method == "Normal":
  162. if length_of_message > len(passphrase):
  163. size = length_of_message/len(passphrase)
  164. passphrase = size * passphrase + passphrase[:(length_of_message%len(passphrase))]
  165.  
  166. # Setup for encryption
  167. encrypted_message = []
  168.  
  169. # For looping purposes
  170. i = 0
  171.  
  172. # Go through the index of each letter in the message
  173. for l in message:
  174.  
  175. # If it's part of the alphabets, let's work on it
  176. if l in self.LETTERS:
  177.  
  178. # Collect the information
  179. msg_letter = message_stripped[i].upper()
  180. pass_letter = passphrase[i].upper()
  181.  
  182. # Do vig calculations
  183. if method == "Normal":
  184.  
  185. # Normal vigenere
  186. vigIndex = (self.LETTERS.index(msg_letter) + self.LETTERS.index(pass_letter)) % 26
  187.  
  188. # Find the letter based on vigIndex result
  189. msg_letter = self.LETTERS[vigIndex]
  190.  
  191. elif method == "Keyed":
  192. # Keyed vigenere
  193. vigIndex = (alphamap.index(msg_letter) + alphamap.index(pass_letter)) % 26
  194.  
  195. # Find the letter based on vigIndex result
  196. msg_letter = alphamap[vigIndex]
  197.  
  198. elif method == "Autokey":
  199. # Autokey vigenere
  200. vigIndex = (alphamap.index(msg_letter) + alphamap.index(pass_letter)) % 26
  201.  
  202. # Append message to the passphrase
  203. passphrase += msg_letter
  204.  
  205. # Find the letter based on vigIndex result
  206. msg_letter = alphamap[vigIndex]
  207.  
  208. # Push it as encrypted message
  209. encrypted_message.append(msg_letter)
  210.  
  211. # Increment letter shifts
  212. i += 1
  213.  
  214. else:
  215. # If it's non-alphabetical character, simply append
  216. encrypted_message.append(l)
  217.  
  218. return "".join(encrypted_message)
  219.  
  220. # Develop Decryption Module
  221. def decrypt(self, message, key, passphrase, method = "Normal"):
  222.  
  223. # Clean up passphrase
  224. passphrase = self.cleanUp(passphrase)
  225.  
  226. # Record message into a new var
  227. message = message.upper()
  228.  
  229. # Without spaces
  230. message_stripped = message.replace(" ","")
  231.  
  232. # Get the length of the message
  233. length_of_message = len(message_stripped)
  234.  
  235. # For keyed and normal vigenere, do special stuff
  236. if method == "Keyed" or method == "Autokey":
  237.  
  238. # Generate an alphamap
  239. alphamap = self.generateAlphaMap(key)
  240.  
  241. # With some math, repeat the passphrase over the course of the letter
  242. if method == "Keyed" or method == "Normal":
  243. if length_of_message > len(passphrase):
  244. size = length_of_message/len(passphrase)
  245. passphrase = size * passphrase + passphrase[:(length_of_message%len(passphrase))]
  246.  
  247. # Setup for encryption
  248. decrypted_message = []
  249.  
  250. # For looping purposes
  251. i = 0
  252.  
  253. # Go through the index of each letter in the message
  254. for l in message:
  255.  
  256. # If it's part of the alphabets, let's work on it
  257. if l in self.LETTERS:
  258.  
  259. # Collect the information
  260. msg_letter = message_stripped[i].upper()
  261. pass_letter = passphrase[i].upper()
  262.  
  263. # Do vig calculations
  264. if method == "Normal":
  265. # Normal vigenere
  266. vigIndex = (self.LETTERS.index(msg_letter) - self.LETTERS.index(pass_letter)) % 26
  267.  
  268. # Find the letter based on vigIndex result
  269. msg_letter = self.LETTERS[vigIndex]
  270.  
  271. elif method == "Keyed":
  272. # Keyed vigenere
  273. vigIndex = (alphamap.index(msg_letter) - alphamap.index(pass_letter)) % 26
  274.  
  275. # Find the letter based on vigIndex result
  276. msg_letter = alphamap[vigIndex]
  277.  
  278. elif method == "Autokey":
  279. # Autokey vigenere
  280. vigIndex = (alphamap.index(msg_letter) - alphamap.index(pass_letter)) % 26
  281.  
  282. # Find the letter based on vigIndex result
  283. msg_letter = alphamap[vigIndex]
  284.  
  285. # Append the new letter to the passphrase
  286. passphrase += msg_letter
  287.  
  288. # Push it as encrypted message
  289. decrypted_message.append(msg_letter)
  290.  
  291. # Increment letter shifts
  292. i += 1
  293.  
  294. else:
  295. # If it's non-alphabetical character, simply append
  296. decrypted_message.append(l)
  297.  
  298. return "".join(decrypted_message)
  299.  
  300. # Brute Force Decrypt
  301. def bruteForce(self, message, potential_passphrase, potential_answer, method = "Normal"):
  302.  
  303. # Split into an array
  304. potential_passphrase = potential_passphrase.split()
  305.  
  306. # Clean out any non-alphabetical characters
  307. potential_passphrase = map(self.cleanUp,potential_passphrase)
  308.  
  309. # Strip out repeated words
  310. # "This sorts the set of all the (unique) words in your string by the word's index in the original list of words."
  311. # Source: https://stackoverflow.com/questions/7794208/how-can-i-remove-duplicate-words-in-a-string-with-python
  312. potential_passphrase = sorted(set(potential_passphrase), key = potential_passphrase.index)
  313.  
  314. # Develop combination
  315. potential_combinations = combinations_with_replacement(potential_passphrase,5)
  316.  
  317. # Potential Answer fix and upper
  318. potential_answer = self.cleanUp(potential_answer).upper()
  319.  
  320. # Clean up encoded message
  321. message = self.cleanUp(message).upper()
  322.  
  323. # # Brute force
  324. # # Develop combination of words
  325. for combination in potential_combinations:
  326.  
  327. # At which step?
  328. step = 0
  329.  
  330. # For each new combo, reset the message
  331. encoded_message = message
  332.  
  333. # Go through each word in the combination
  334. for word in combination:
  335.  
  336. # Pull the decoded message
  337. decoded = vig.decrypt(encoded_message,"t",word, method = method)
  338.  
  339. if potential_answer == decoded:
  340. return combination, step
  341. break
  342. else:
  343. encoded_message = decoded
  344.  
  345. step += 1
  346.  
  347. # # Check Code
  348. # vig = Vigenere()
  349. # new_msg = vig.encrypt("ZOOLOGY IS AWESOME","thanks","hello", method = "Autokey")
  350. # print new_msg
  351. # org_msg = vig.decrypt(new_msg,"thanks","hello", method = "Autokey")
  352. # print org_msg
  353.  
  354. #### BRUTE FORCE MECHANISM ####
  355.  
  356. # Capture potential passphrase
  357. vig = Vigenere()
  358. potential_passphrase = "WE COME FROM THE ERIDANUS SUPERVOID"
  359. potential_answer = "trialists"
  360. message = "ydhjerbbp"
  361. method = "Autokey"
  362.  
  363. result = vig.bruteForce(message,potential_passphrase,potential_answer, method = method)
  364.  
  365. print "The combination that works is {}, it terminated at work {}".format(result[0], result[0][result[1]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement