SHARE
TWEET

improved_caesar

snozzy Feb 24th, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3.  
  4. def encrypt_message(letter, key):
  5.     # Om tecknet inte är a-z ska det inte röras
  6.     if ord(letter) < 97 or ord(letter) > 122:
  7.         return chr(ord(letter))
  8.     # Om teckner går utanför z ska det fortsätta vid a
  9.     elif ord(letter) + key > 122:
  10.         return chr(ord(letter) + key - 26)
  11.     else:
  12.         return chr(ord(letter) + key)
  13.  
  14.  
  15. def decrypt_message(letter, key):
  16.     # Om tecknet inte är a-z ska det inte röras
  17.     if ord(letter) < 97 or ord(letter) > 122:
  18.         return chr(ord(letter))
  19.     # Om teckner går utanför a ska det fortsätta vid z
  20.     elif ord(letter) - key < 97:
  21.         return chr(ord(letter) - key + 26)
  22.     else:
  23.         return chr(ord(letter) - key)
  24.  
  25.  
  26. # Skapa listan keyvalue som innehåller 7 slumpade siffror
  27. keyvalue = []
  28. keys = "0123456789"
  29. for i in range(7):
  30.     keyvalue.append(random.choice(keys))
  31. keyvalue = [int(i) for i in keyvalue]
  32.  
  33. # Läser texten ur en fil och sparar den som message
  34. with open("text.txt", "r") as f1:
  35.     encryptMessage = f1.read()
  36.     encryptMessage = encryptMessage.lower()
  37. f1.close()
  38.  
  39. repeatedKey = keyvalue * len(encryptMessage)  # Gör så nycklarna upprepar sig
  40. encrypted_message = ""
  41. currentKey = 0
  42.  
  43. # ---------------------------------------------------------------------
  44.  
  45. # Går genom varje tecken i encryptMessage
  46. for char in encryptMessage:
  47.     if char == " ":  # Nycklen ändras ej om char == " "
  48.         encrypted_message += encrypt_message(char, repeatedKey[currentKey])
  49.     else:
  50.         encrypted_message += encrypt_message(char, repeatedKey[currentKey])
  51.         currentKey += 1
  52.  
  53. # Sparar den krypterade texten i encrypt.txt
  54. with open("encrypt.txt", "w") as f2:
  55.     f2.write(encrypted_message)
  56. f2.close()
  57.  
  58. # ---------------------------------------------------------------
  59.  
  60. with open("encrypt.txt", "r") as f3:
  61.     decryptMessage = f3.read()
  62.     decryptMessage = decryptMessage.lower()
  63. f3.close()
  64.  
  65. currentKey = 0
  66. decrypted_message = ""
  67. for char in decryptMessage:
  68.     if char == " ":
  69.         decrypted_message += decrypt_message(char, repeatedKey[currentKey])
  70.     else:
  71.         decrypted_message += decrypt_message(char, repeatedKey[currentKey])
  72.         currentKey += 1
  73.  
  74. with open("decrypt.txt", "w") as f4:
  75.     f4.write(decrypted_message)
  76. f4.close()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top