Advertisement
Mr_hEx

Enc and Dec By shift

Dec 7th, 2020 (edited)
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.83 KB | None | 0 0
  1. # Ref: https://www.geeksforgeeks.org/rot13-cipher/
  2.  
  3. dict1 = {'A': 1, 'B': 2, 'C': 3, 'D': 4, 'E': 5,
  4.          'F': 6, 'G': 7, 'H': 8, 'I': 9, 'J': 10,
  5.          'K': 11, 'L': 12, 'M': 13, 'N': 14, 'O': 15,
  6.          'P': 16, 'Q': 17, 'R': 18, 'S': 19, 'T': 20,
  7.          'U': 21, 'V': 22, 'W': 23, 'X': 24, 'Y': 25, 'Z': 26}
  8.  
  9. # Dictionary to lookup alphabets
  10. # corresponding to the index after shift
  11. dict2 = {0: 'Z', 1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E',
  12.          6: 'F', 7: 'G', 8: 'H', 9: 'I', 10: 'J',
  13.          11: 'K', 12: 'L', 13: 'M', 14: 'N', 15: 'O',
  14.          16: 'P', 17: 'Q', 18: 'R', 19: 'S', 20: 'T',
  15.          21: 'U', 22: 'V', 23: 'W', 24: 'X', 25: 'Y'}
  16.  
  17. def encrypt(message, shift):
  18.     cipher = ''
  19.     for letter in message:
  20.         # checking for space
  21.         if (letter != ' '):
  22.             # looks up the dictionary and  
  23.             # adds the shift to the index
  24.             num = (dict1[letter] + shift) % 26
  25.             # looks up the second dictionary for  
  26.             # the shifted alphabets and adds them
  27.             cipher += dict2[num]
  28.         else:
  29.             # adds space
  30.             cipher += ' '
  31.  
  32.     return cipher
  33.  
  34.  
  35. # Function to decrypt the string
  36. # according to the shift provided
  37. def decrypt(message, shift):
  38.     decipher = ''
  39.     for letter in message:
  40.         # checks for space
  41.         if (letter != ' '):
  42.             # looks up the dictionary and  
  43.             # subtracts the shift to the index
  44.             num = (dict1[letter] - shift + 26) % 26
  45.             # looks up the second dictionary for the  
  46.             # shifted alphabets and adds them
  47.             decipher += dict2[num]
  48.         else:
  49.             # adds space
  50.             decipher += ' '
  51.  
  52.     return decipher
  53.  
  54. for i in range(1,27):
  55.     print("[+] shift N : {} MSG : {}".format(i,decrypt("XXXXXXXXXXX",i)))
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement