Skip to content

Commit 84bd56f

Browse files
authored
Merge pull request Techiral#51 from slano-ls/main
Added Rebel Cipher Project to Folder R
2 parents f34659f + dba5fb7 commit 84bd56f

File tree

5 files changed

+148
-0
lines changed

5 files changed

+148
-0
lines changed

.DS_Store

8 KB
Binary file not shown.

R/.DS_Store

8 KB
Binary file not shown.

R/rebel-cipher/rebel_cipher.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
```

R/rebel-cipher/rebel_cipher.pdf

157 KB
Binary file not shown.

R/rebel-cipher/rebel_cipher.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
def encrypt(message, key):
3+
encrypted_message = ""
4+
for char in message:
5+
if char.isalpha():
6+
is_uppercase = char.isupper()
7+
char = char.upper()
8+
encrypted_char = chr((ord(char) - 65 + key) % 26 + 65)
9+
if not is_uppercase:
10+
encrypted_char = encrypted_char.lower()
11+
encrypted_message += encrypted_char
12+
else:
13+
encrypted_message += char
14+
return encrypted_message
15+
16+
17+
def decrypt(encrypted_message, key):
18+
return encrypt(encrypted_message, -key)
19+
20+
21+
def save_to_file(data, filename):
22+
with open(filename, "w") as file:
23+
file.write(data)
24+
25+
26+
def read_from_file(filename):
27+
with open(filename, "r") as file:
28+
return file.read()
29+
30+
31+
# Main program
32+
valid_message = False
33+
while not valid_message:
34+
message = input("Enter a message: ")
35+
if any(char.isdigit() for char in message):
36+
print("Invalid message! Please enter a message without numbers.")
37+
else:
38+
valid_message = True
39+
40+
key = int(input("Enter the encryption key: "))
41+
42+
encrypted_message = encrypt(message, key)
43+
decrypted_message = decrypt(encrypted_message, key)
44+
45+
print("Encrypted message:", encrypted_message)
46+
print("Decrypted message:", decrypted_message)
47+
48+
save_to_file(encrypted_message, "encrypted_message.txt")
49+
50+
encrypted_message_from_file = read_from_file("encrypted_message.txt")
51+
print("Read from file:", encrypted_message_from_file)

0 commit comments

Comments
 (0)