Skip to content

Commit 892fdad

Browse files
authored
PR #75: Encryption Project
Encryption Project Merge pull request #75 from moonabys/encryption_project
2 parents 195b740 + cbf6192 commit 892fdad

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

Encryption_Project/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Encryption Project
2+
3+
For now this project can only encrypt messages using the following methods:
4+
5+
* SHA-256
6+
* AES-128
7+
* Base64
8+
9+
*If you want to understand how this works, this project comments all around it to make sure you understand what's happening*
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Let's start with AES
2+
# AES is a type of encryption used mainly for things that require certain level of safety and later will be used
3+
# There is a specific thing you shouldn't encrypt with AES that's Passkeys
4+
# For passkeys you will use a method showed later that are the Hash Methods
5+
6+
# In this example we using pycryptodome
7+
8+
from Crypto.Cipher import AES
9+
10+
def encrypt_aes(message: str, key: str):
11+
encrypt_cipher = AES.new(key, AES.MODE_EAX) # This is the "configuration" for the encryption
12+
13+
nonce = encrypt_cipher.nonce # Nonce aka Number Once is basically what makes the key random
14+
15+
encrypted_message = encrypt_cipher.encrypt_and_digest(message) # Here we are truly encrypting the message
16+
17+
return encrypted_message, nonce
18+
19+
def decrypt_aes(message: bytes, key: bytes, nonce: bytes):
20+
decrypt_cipher = AES.new(key, AES.MODE_EAX, nonce=nonce) # Here we are Saying what's the key, type of encryption and nonce
21+
decrypted_message = decrypt_cipher.decrypt(message)
22+
return decrypted_message
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Let's start
2+
# Base64 is NOT a safe way to encrypt important things, it's used to encrypt binary data to text and doesn't have a key such as AES
3+
# If you pretend encrypting something that you need to know later you should use AES
4+
# Else you just want to encrypt something and doesn't care about the privacy Base64 might be a good choice
5+
6+
# In this example we are using a pre-installed module from Python called "base64"
7+
8+
import base64
9+
10+
def encrypt_base64(message: str):
11+
encrypt_message = base64.b64encode(message.encode()).decode()
12+
return encrypt_message
13+
14+
def decrypt_base64(message: str):
15+
decrypt_message = base64.b64decode(message).decode('utf-8')
16+
return decrypt_message
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Let's start with Hashes! (more specifically SHA256)
2+
# Hashes are a way to encrypt thing in a irreversible way
3+
# This means if you encrypt something using Hash Methods there is no way to know the content
4+
5+
# In this example we using pre-installed module called "hashlib"
6+
7+
import hashlib
8+
9+
def encryption_sha(message: str):
10+
return hashlib.sha256().digest() # This makes the encryption in SHA-256. This is the usually how your passkeys are encrypted

Encryption_Project/main.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from encryption import sha, aes, base64
2+
import os
3+
4+
def main():
5+
while True:
6+
print("""Welcome to Encryption Project\n
7+
1 - SHA256\n
8+
2 - AES\n
9+
3 - Base64\n
10+
4 - Quit\n
11+
More soon!\n""")
12+
13+
encrypt_choice = int(input())
14+
15+
match encrypt_choice:
16+
case 1:
17+
message_to_encrypt = input("What's the message you want to encrypt?: ")
18+
19+
encrypted_message = sha.encryption_sha(message_to_encrypt)
20+
21+
print(f"The encrypted message is: {encrypted_message}")
22+
23+
case 2:
24+
aes_choice = int(input("1 - Encrypt\n2 - Decrypt\n"))
25+
26+
if aes_choice == 1:
27+
message_to_encrypt = input("What's the message you want to encrypt? ").encode('utf-8')
28+
key = os.urandom(16) # if you are confused, this just guarantee the key will have 16 bytes
29+
30+
encrypted_message, nonce = aes.encrypt_aes(message_to_encrypt, key)
31+
32+
print(f"Encrypted message: {encrypted_message}\nnonce: {nonce}\nkey: {key}\n *Save those!*")
33+
elif aes_choice == 2:
34+
message_to_decrypt = eval(input("What's the message to decrypt? "))
35+
key = eval(input("What's the key? "))
36+
nonce = eval(input("What's the nonce? "))
37+
38+
decrypted_message = aes.decrypt_aes(message_to_decrypt, key, nonce)
39+
40+
print(f"Message: {decrypted_message}")
41+
42+
else:
43+
print("Option does not exist")
44+
45+
case 3:
46+
base_choice = int(input("1 - Encrypt\n2 - Decrypt\n"))
47+
48+
if base_choice == 1:
49+
message_to_encrypt = input("What's the message you want to encrypt? ")
50+
51+
encrypted_message = base64.encrypt_base64(message_to_encrypt)
52+
53+
print(f"Message: {encrypted_message}")
54+
55+
elif base_choice == 2:
56+
message_to_decrypt = input("What's the message to decrypt? ")
57+
58+
decrypted_message = base64.decrypt_base64(message_to_decrypt)
59+
60+
print(f"Message: {decrypted_message}")
61+
62+
else:
63+
print("Option does not exist")
64+
65+
case 4:
66+
print("Bye!")
67+
break
68+
69+
case _:
70+
print("This option is not available")
71+
72+
if __name__ == "__main__":
73+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pycryptodome

0 commit comments

Comments
 (0)