|
| 1 | +# The Rebel Cipher Algorithm |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +Welcome to my Rebel Cipher Algorithm which takes in an input of a string, encrypts it based on **your** given encryption key, and writes it to a file known as *encrypted_message.txt* |
| 6 | + |
| 7 | +## The Encryption Function |
| 8 | + |
| 9 | +Here we convert the message into barebones raw value which we leave consistent throughout our code. This includes checking if the message is **Uppercase**, if it is **Lowercase** we use the `.upper()` method in order to convert the values into a UPPERCASE Value. |
| 10 | + |
| 11 | +We the convert the encrypted characters (*the values*) into their respective **ASCII** values, by using the `chr((ord(char) - 65 + key)%26 + 65)` function in order to convert it into the numerical ASCII value which we will convert later. |
| 12 | + |
| 13 | +Then the function returns the value of the encrypted message |
| 14 | + |
| 15 | +```python |
| 16 | +def encrypt(message, key): |
| 17 | + encrypted_message = "" |
| 18 | + for char in message: |
| 19 | + if char.isalpha(): |
| 20 | + is_upper = char.isupper() |
| 21 | + char = char.upper() |
| 22 | + encrypted_char = chr((ord(char) - 65 + key) % 26 + 65) |
| 23 | + if not is_upper: |
| 24 | + encrypted_char = encrypted_char.lower() |
| 25 | + encrypted_message+=encrypted_char |
| 26 | + else: |
| 27 | + encrypted_message+=char |
| 28 | + return encrypted_message |
| 29 | +``` |
| 30 | + |
| 31 | +## The Decryption Function |
| 32 | + |
| 33 | +The Decryption Function is fairly similar, it returns the encrypted value; except we have a **-key** value which basically flips the value of the key so that it is decrypted from the original key (if it added 5, the decryption subtracts 5) |
| 34 | + |
| 35 | +```python |
| 36 | +def decrypt(encrypted_message, key): |
| 37 | + return encrypt(encrypted_message, -key) |
| 38 | +``` |
| 39 | + |
| 40 | +## The Saving Function |
| 41 | + |
| 42 | +That was a funny name for a function... |
| 43 | +In this we have the function which saves the data to a file which we haven't specified. An important note: we are using the *"w"* function so that we are able to write to the given file. |
| 44 | + |
| 45 | +```python |
| 46 | +def save_to_file(data, filename): |
| 47 | + with open(filename, "w") as file: |
| 48 | + file.write(data) |
| 49 | + |
| 50 | +``` |
| 51 | + |
| 52 | +## The Reading Function |
| 53 | + |
| 54 | +We do a similar thing as above, except now we are only *"r"* or reading from the file. Since this is a simple code example, it is not really necessary for us to do this; but as a good programming practice, it is important to only use the file in the way you want it to be specified. |
| 55 | + |
| 56 | +```python |
| 57 | +def read_from_file(filename): |
| 58 | + with open(filename, "r") as file: |
| 59 | + return file.read() |
| 60 | + |
| 61 | +``` |
| 62 | + |
| 63 | +## Main Loop |
| 64 | + |
| 65 | +In this, we take the user input and ensure that it is not a number through the following |
| 66 | + |
| 67 | +```python |
| 68 | +while not valid_message: |
| 69 | + message = input("Enter the Message> ") |
| 70 | + if any(char.isdigit() for char in message): |
| 71 | + print("This was an invalid input, please try entering a LETTER VALUE") |
| 72 | + else: |
| 73 | + valid_message = True |
| 74 | +``` |
| 75 | + |
| 76 | +In here all we are doing is ensuring that we have a value that **ISN'T** a number through the *.isdigit()* method. If it is a number, we ask them to give another input; if it isn't a number, then we allow the programming to continue running by breaking out of the while loop by setting the Boolean Expression of the while loop to **TRUE** |
| 77 | + |
| 78 | +Next, we are gathering all our various inputs |
| 79 | + |
| 80 | +```python |
| 81 | +key = int(input("Enter the Encryption Key> ")) |
| 82 | + |
| 83 | +encrypted_message = encrypt(message, key) |
| 84 | +decrypted_message = decrypt(encrypted_message, key) |
| 85 | +``` |
| 86 | + |
| 87 | +And finally, we are printing out all our values; just converting them to Uppercase by using the *.upper()* function once again: |
| 88 | + |
| 89 | +```python |
| 90 | +print(f"Your encrypted message is: {encrypted_message.upper()}") |
| 91 | +print(f"Your decrypted message is: {decrypted_message.upper()}") |
| 92 | + |
| 93 | +save_to_file(encrypted_message, "encrypted_message.txt") |
| 94 | + |
| 95 | +encrypted_message_read_from_file = read_from_file("encrypted_message.txt") |
| 96 | +print(f"The message read from the file is {encrypted_message_read_from_file.upper()}") |
| 97 | +``` |
0 commit comments