cryptic-cypher-machine


SUBMITTED BY: okpalan86

DATE: March 1, 2024, 3:01 p.m.

UPDATED: March 1, 2024, 3:12 p.m.

FORMAT: Text only

SIZE: 1.6 kB

HITS: 474

  1. class CrypticStateMachine:
  2. def __init__(self):
  3. pass
  4. def encrypt(self, message, shift):
  5. """
  6. Encrypts the message using Caesar cipher with the given shift.
  7. """
  8. encrypted_message = ""
  9. for char in message:
  10. if char.isalpha():
  11. ascii_offset = ord('a') if char.islower() else ord('A')
  12. encrypted_message += chr((ord(char) - ascii_offset + shift) % 26 + ascii_offset)
  13. else:
  14. encrypted_message += char
  15. return encrypted_message
  16. def decrypt(self, encrypted_message, shift):
  17. """
  18. Decrypts the encrypted message using Caesar cipher with the given shift.
  19. """
  20. decrypted_message = ""
  21. for char in encrypted_message:
  22. if char.isalpha():
  23. ascii_offset = ord('a') if char.islower() else ord('A')
  24. decrypted_message += chr((ord(char) - ascii_offset - shift) % 26 + ascii_offset)
  25. else:
  26. decrypted_message += char
  27. return decrypted_message
  28. def all_encryptions(self, message):
  29. """
  30. Generates all possible encrypted messages for the given input message.
  31. """
  32. for shift in range(26):
  33. yield self.encrypt(message, shift)
  34. # Usage
  35. cryptic_state_machine = CrypticStateMachine()
  36. message = "I never said she stole my money"
  37. all_encrypted_messages = list(cryptic_state_machine.all_encryptions(message))
  38. for i, encrypted_message in enumerate(all_encrypted_messages):
  39. print(f"Shift {i}: {encrypted_message}")

comments powered by Disqus