Advertisement
RootOfTheNull

Vignere Cipher Encode/Decode

Aug 14th, 2018
1,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.36 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import itertools
  4. import string
  5. import collections
  6.  
  7. lowercase = collections.deque( string.ascii_lowercase + string.digits )
  8.  
  9. # Getting the message...
  10. h = open('flag.enc')
  11. message = h.read()
  12. h.close()
  13.  
  14. key = 'blaise'
  15.  
  16. def encrypt( message, key, multipler = -1 ):
  17.  
  18.     compressed_message = message.lower()
  19.  
  20.     for punctuation in str(string.punctuation + ' '):
  21.         compressed_message = compressed_message.replace(punctuation, '')
  22.     cycler = itertools.cycle(key.lower())
  23.  
  24.     long_key = ''.join( [ cycler.next() for _ in range(len(compressed_message))] )
  25.  
  26.     coded = []
  27.     for number in range(len(long_key)):
  28.        
  29.         cipher_letter = compressed_message[number]
  30.         key_letter = long_key[number]
  31.         key_index = string.ascii_lowercase.index(key_letter)
  32.         cipher_index = string.ascii_lowercase.index(cipher_letter)
  33.  
  34.         lowercase = collections.deque( string.ascii_lowercase )
  35.         lowercase.rotate( multipler * key_index )
  36.         new_alphabet = ''.join(list(lowercase))
  37.         new_character = new_alphabet[cipher_index]
  38.         coded.append( new_character )
  39.  
  40.     return ''.join(coded)
  41.  
  42. def decrypt( message, key, multipler = -1 ):
  43.  
  44.     return encrypt( message, key, 1 )
  45.  
  46. print message
  47. print decrypt(message, key)
  48.  
  49. # cryptaclysmevenindecipherablecipherscanbebroken
  50. # czqtulctqwn{Pvmf_Mooekatipritpf_Nixzisd_Cif_Ff_Mrwcio}
  51. # cryptaclysm{Even_Indecipherable_Ciphers_Can_Be_Broken}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement