Skip to content

Instantly share code, notes, and snippets.

@Llewellynvdm
Last active November 12, 2019 21:50
Show Gist options
  • Save Llewellynvdm/1a4244d04c3b340f7b0f7426e828be45 to your computer and use it in GitHub Desktop.
Save Llewellynvdm/1a4244d04c3b340f7b0f7426e828be45 to your computer and use it in GitHub Desktop.
Caesar Cipher Hack
#!/usr/bin/python3
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Caesar Cypher Hack
#
# @author Llewellyn van der Merwe <https://www.vdm.io/>
# @copyright Copyright (C) 2019 Vast Development Method. All rights reserved.
# @license GNU/GPL Version 2 or later - http://www.gnu.org/licenses/gpl-2.0.html
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Main Function
def main():
# set the default color
font_color = '1;33;93m'
# welcome message
print('\x1b[{}+======+======+======+======+======+======+======+======+======+======+\x1b[0m'.format(font_color))
print('\x1b[{} Lets play with the Caesar Cypher!\x1b[0m'.format(font_color))
print('\x1b[{}+======+======+======+======+======+======+======+======+======+======+\x1b[0m'.format(font_color))
# okay first ask the question of what is to be done
while True:
print('Do you want to open or lock as string?')
print("\x1b[{}1\x1b[0m) Open".format(font_color))
print("\x1b[{}2\x1b[0m) Lock".format(font_color))
# get the action
try:
action = int(input('Enter a number >> '))
except ValueError:
continue
# only break if we have 1 or 2
if 0 < action < 3:
break
# we do a decrypt
if action == 1:
# Get the message to decrypt from the user
print('Enter the encrypted message:')
encryptedMessage = input()
# hack the message
print("Now we will open this encrypted message with brute force!")
for try_key in range(1, 26, 1):
massage = hack(encryptedMessage, try_key)
# print out the tries
print(massage)
# got it@last
print('\x1b[{}The line you can read is the decrypted message!\x1b[0m'.format(font_color, try_key))
else:
# Get the message to encrypt from the user
print('Enter your message:')
unencryptedMessage = input()
# Get the encryption key from the user. Note the error handling to make sure we have a number from 1-26
key = 0
while (key < 1 or key > 26):
print('Enter the key number (1-26)')
try:
key = int(input('Enter a number >> '))
except ValueError:
continue
print('The key is ' + str(key))
# now we lock the massage
encryptedMessage: str = lock(unencryptedMessage, key)
print("Your encrypted message is:")
print(encryptedMessage)
print('\x1b[{}=====================================================================\x1b[0m'.format(font_color))
# Lets open the message
def hack(encryptedMessage, try_key) -> str:
# We will start with an empty string as our cryptedMessage
unencryptedMessage: str = ''
# For each symbol in the encryptedMessage we will add an encrypted symbol into the encryptedMessage
for symbol in encryptedMessage:
if symbol.isalpha():
num = ord(symbol)
num += try_key
if symbol.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif symbol.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
unencryptedMessage += chr(num)
else:
unencryptedMessage += symbol
return unencryptedMessage
# Caesar Cypher Encryption
def lock(unencryptedMessage, key) -> str:
# We will start with an empty string as our encryptedMessage
encryptedMessage: str = ''
# For each symbol in the unencryptedMessage we will add an encrypted symbol into the encryptedMessage
for symbol in unencryptedMessage:
if symbol.isalpha():
num = ord(symbol)
num += key
if symbol.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif symbol.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
encryptedMessage += chr(num)
else:
encryptedMessage += symbol
return encryptedMessage
if __name__ == "__main__": main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment