Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- goodCharacters = [ chr(z) for (f,t) in [('a', 'z'), ('0', '9'), (' ', ' ')] for z in range(ord(f),ord(t)+1) ]
- #goodCharacters = [ chr(z) for (f,t) in [('a', 'z')] for z in range(ord(f),ord(t)+1) ]
- def encrypt(plaintext, key):
- ciphertext = ""
- last = 0
- for j in range(len(plaintext)):
- c = plaintext[j]
- i = goodCharacters.index(c)
- i = (i + key[j%len(key)] + j + last) % len(goodCharacters)
- #i = (i + key[j%len(key)]) % len(goodCharacters)
- cp = goodCharacters[i]
- ciphertext = ciphertext + cp
- last = i
- return ciphertext
- def to_original(encrypted):
- ciphertext = ""
- last = 0
- aux_last = 0
- for j in range(len(encrypted)):
- c = encrypted[j]
- i = goodCharacters.index(c)
- aux_last = i
- i = (i - j - last) % len(goodCharacters)
- last = aux_last
- cp = goodCharacters[i]
- ciphertext = ciphertext + cp
- return ciphertext
- def decrypt(plaintext, key):
- ciphertext = ""
- last = 0
- for j in range(len(plaintext)):
- c = plaintext[j]
- i = goodCharacters.index(c)
- i = (i - key[j%len(key)]) % len(goodCharacters)
- cp = goodCharacters[i]
- ciphertext = ciphertext + cp
- last = i
- return ciphertext
- def get_max(dict):
- maximum = 0;
- key = 0;
- for k,v in dict.items():
- #print (k)
- if maximum < v:
- maximum = v
- key = k
- print(key)
- def print_distances(encrypted):
- print("%20s%20s%20s%20s" % ("Repeated Bigram","Location","Distance","Factors") )
- values_list = dict()
- for i in range(1, len(plaintext)):
- pair = encrypted[i-1] + encrypted[i]
- for j in range(i, len(plaintext)-1):
- nextpair = encrypted[j] + encrypted[j+1]
- if pair == nextpair:
- dist = j-i+1
- #print("%20s%20d%20d" % (pair, j, j-i+1) )
- if dist in values_list:
- values_list[dist] = values_list[dist] + 1
- if dist == 3:
- print ("dist: " + str(dist) + " value: " + str(values_list[dist]))
- else:
- values_list[dist] = 1;
- break
- get_max(values_list)
- def bruteforce(original):
- key = ""
- the_occurrences = 0
- count = 0
- for i in range(len(goodCharacters)):
- for j in range(len(goodCharacters)):
- for k in range(len(goodCharacters)):
- count = count + 1
- if count % 100 == 0:
- print(str(count) + " out of " + str(len(goodCharacters)**3))
- key = [i, j, k]
- plaintext = decrypt(original, key)
- the_occurrences = plaintext.count(" the ")
- if the_occurrences > 10:
- print ("FOR KEY: " + str(key))
- print (plaintext)
- plaintext = ""
- f = open("cipher.txt", "r")
- for line in f:
- line = line.lower()
- line = line.strip()
- line = line + " "
- plaintext = plaintext + "".join([c for c in line if c in goodCharacters])
- f.close()
- # you need to figure out the real key
- #print encrypt(plaintext, [1,2,3,4,5,6,7,8,9,10])
- #key = [17,4,11,0,19,8,14,13,18] #RELATIONS
- #encrypted = encrypt(plaintext, key)
- # print (encrypted)
- original = to_original(plaintext)
- # print(original)
- bruteforce(original)
- #print_distances(original)
- # decrypted = decrypt(original, key)
- # print (decrypted)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement