Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. ######## Made By Mouad Khiat
  2. ######## This script is the Famous Vigenere cipher its like caesar that ive made in the previous gist
  3. ######## but the difference is that we deal here with letters not numbers
  4. ######## So with this script you can encrypt anything you want with your given key , decrypt it either with a key or a part of it
  5. ######## or by guessing
  6.  
  7. from random import sample
  8. from itertools import product as col
  9.  
  10.  
  11. def generator(key,char,length):
  12. char_len = key.count(char)
  13. key_piece = key[:length - char_len:]
  14. list_keys = [key_piece+"".join(i) for i in list(col([chr(i) for i in range(65, 65+26)], repeat=char_len))]
  15. return list_keys
  16.  
  17. def vigenere(x,key):
  18. lst_final = []
  19. code = list(x)
  20. j = 0
  21.  
  22. for i,char in enumerate(code):
  23. if char.isalpha():
  24. code[i] = key[(i+j)%len(key)]
  25. if encrypt:
  26. lst_final.append((ord(x[i]) + ord(code[i]) - 65 * 2) % 26)
  27. else:
  28. lst_final.append((ord(x[i]) - ord(code[i])) % 26)
  29. else:
  30. lst_final.append(ord(char))
  31. j -=1
  32.  
  33. for i,char in enumerate(code):
  34. if char.isalpha():
  35. lst_final[i] = chr(lst_final[i] + 65)
  36. else:
  37. lst_final[i] = chr(lst_final[i])
  38.  
  39. return ''.join(lst_final)
  40.  
  41. print("Welcome to Vigenere cipher")
  42.  
  43. if input('Encrypt or Decrypt : ').lower() == 'encrypt':
  44. x = input('Enter the text : ').upper()
  45. key = input('Enter the key : ').upper()
  46. encrypt = True
  47. print(vigenere(x,key))
  48. else:
  49. x = input('text : ').upper()
  50. encrypt = False
  51. if input('have you the key (y/n) : ') == "y":
  52. key = input('Enter the key : ').upper()
  53. print(vigenere(x,key))
  54. else:
  55. abc = list("ABCDEFGHIJKHIJKLMNOPQRSTUVWXYZ")
  56. question = input('Enter a part of the key or length (answer by 1 or 2 or nothing): ')
  57. if question == '1':
  58. key = input('*use \'?\' for the missing letter in the key (C?? or CL? refer for ex to CLE): ').upper()
  59. list_of_keys = generator(key,'?',len(key))
  60. for k in list_of_keys:
  61. print(f'for {k} ==> {vigenere(x,k)}')
  62.  
  63. elif question == '2':
  64. length = int(input('Enter the length: '))
  65. while True:
  66. key_gen = ''.join(sample(abc,length))
  67. print(f"for {key_gen} = {vigenere(x,key_gen)}")
  68. if input('continue(y/n) ... : ')== "n":
  69. break
  70. else:
  71. print("S0rry this script cannot find your encrypted text")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement