Advertisement
snozzy

Untitled

Feb 18th, 2020
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. # Oliver Schuller
  2.  
  3.  
  4. def encrypt(msg, n):
  5.     encryptedText = ""
  6.  
  7.     # Make a "for loop" that will convert string to ascii, shifts character n positions and then convert back to
  8.     # normal characters
  9.     for letter in range(len(msg)):
  10.         if ord(msg[letter]) < 97 or ord(msg[letter]) > 122:  # Leaves characters outside the alphabet unchanged
  11.             encryptedText += chr(ord(msg[letter]))
  12.  
  13.         elif ord(msg[letter]) - n < 97:  # If the ascii number is less than 97, add 26 so it stays within the alphabet
  14.             encryptedText += chr(ord(msg[letter]) - n + 26)
  15.  
  16.         else:
  17.             encryptedText += chr(ord(msg[letter]) - n)  # Shifts character n positions
  18.  
  19.     return encryptedText
  20.  
  21.  
  22. def decrypt(msg, n):
  23.     clear_msg = ""
  24.  
  25.     # Make a "for loop" that will convert string to ascii, shifts character n positions and then convert back to
  26.     # normal characters
  27.     for letter in range(len(msg)):
  28.         if ord(msg[letter]) < 97 or ord(msg[letter]) > 122: # Leaves characters outside the alphabet unchanged
  29.             clear_msg += chr(ord(msg[letter]))
  30.  
  31.         elif ord(msg[letter]) + n > 122:
  32.             clear_msg += chr(ord(msg[letter]) + n - 26)  # If the ascii number is greater than 122, subtract 26 so it
  33.             # stays within the alphabet
  34.  
  35.         else:
  36.             clear_msg += chr(ord(msg[letter]) + n)  # Shifts character n positions
  37.  
  38.     return clear_msg
  39.  
  40.  
  41. def lfa(msg):
  42.     count = {}
  43.     for i in msg:
  44.         if ord(i) < 97 or ord(i) > 122:  # Skip characters outside the alphabet
  45.             continue
  46.         elif i in count:  # If the letter already is in count, increment 1
  47.             count[i] += 1
  48.         else:  # Otherwise add the letter as a key and 1 as value
  49.             count[i] = 1
  50.     for letter, occurrence in count.items():  # Print how many times each letter occurs
  51.         print('Character {} occurs {} times'.format(letter, occurrence))
  52.  
  53.  
  54. print("Choose your option: \n 1. Encrypt message \n 2. Decrypt message \n 3. Read and write to file \n "
  55.       "4. Letter frequency analysis \n")
  56. choice = input("Write the number for your option: ")
  57.  
  58. if choice == "1":
  59.     message = input("Enter your text here: ")
  60.     message = message.lower()
  61.     key = int(input("Enter your key here (1-25): "))
  62.     print(encrypt(message, key))
  63.  
  64. elif choice == "2":
  65.     message = input("Enter your text here: ")
  66.     message = message.lower()
  67.     key = int(input("Enter your key here (1-25): "))
  68.     print(decrypt(message, key))
  69.  
  70. elif choice == "3":
  71.     f1 = open("clearText.txt", "r")
  72.     lines = f1.readlines()
  73.     message = ""
  74.     for line in lines:
  75.         message += line
  76.         message = message.replace("\n", " ")
  77.     key = int(input("Enter your key here (1-25): "))
  78.  
  79.     f2 = open("encryptedText.txt", "a")
  80.     lines = f2.write(encrypt(message, key))
  81.  
  82. elif choice == "4":
  83.     text = input("Write your text here: ")
  84.     text = text.lower()
  85.     lfa(text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement