Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added 2Step Encryption Module + Updated Readme.md #41

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Projects/Encryption Module(2 Step)/AES Encryption Template/aes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import rsa
import rsa.randnum
import random
from Crypto.Cipher import AES
from secrets import token_bytes

key = rsa.randnum.read_random_bits(128) #Generate a Random Key
pub_key,prvt_key = rsa.newkeys(512) #Generate Public & Private Keys for RSA Encryption
encrypted_aes_key = rsa.encrypt(key, pub_key) #Encrypt the Random Key using RSA

def encrypt(msg): #Sending End
cipher = AES.new(key,AES.MODE_EAX) #Using the Random Key, encrypt the data using AES
nonce = cipher.nonce
ciphertext,tag = cipher.encrypt_and_digest(msg.encode('ascii')) #Encode the Data to ASCII, since AES takes only Bytes
return nonce, ciphertext, tag

#Send the Encrypted Data, along with the Encrypted Random Key (RSA Encrypted)
def decrypt(nonce,ciphertext,tag): #Receiving End
aes_key = rsa.decrypt(encrypted_aes_key,prvt_key) #Decrypt the Key using RSA
cipher = AES.new(aes_key, AES.MODE_EAX, nonce=nonce) #Use the above key to decrypt the Data.
plaintext = cipher.decrypt(ciphertext)
try:
cipher.verify(tag)
return plaintext.decode('ascii') #Decode the text from ASCII to String
except:
return False



nonce,ciphertext,tag = encrypt(input("Enter a message: "))
plaintext = decrypt(nonce,ciphertext,tag)
print(f'Cipher text: {ciphertext}')
if not plaintext:
print('Message is Corrupted')
else:
print(f'Plain Text: {plaintext}')
Binary file not shown.
Binary file not shown.
6 changes: 6 additions & 0 deletions Projects/Encryption Module(2 Step)/decrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import rsa

def decryptmsg(cryptmessage,prvt_key):
decrypted = rsa.decrypt(cryptmessage,prvt_key).decode()
return decrypted

6 changes: 6 additions & 0 deletions Projects/Encryption Module(2 Step)/encrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import rsa

def encryptmsg(message,pub_key):
encrypted = rsa.encrypt(message.encode(),pub_key)
return encrypted

10 changes: 10 additions & 0 deletions Projects/Encryption Module(2 Step)/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import rsa
import encrypt
import decrypt

message = input("Enter a message to be encrypted:")
pub_key,prvt_key = rsa.newkeys(512)
res = encrypt.encryptmsg(message,pub_key)
unres = decrypt.decryptmsg(res,prvt_key)
print("Encrypted Message: ",res)
print("Decrypted Message: ",unres)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ After contributing add your **NAME and USER NAME** here:
5. **Mohit** - [MohitOnHub](https://github.com/MohitOnHub)
6. **Vikalp Shishodia** - [VkRan](https://github.com/VkRan)

7. **Full Name** - [User Name](https://github.com/username)
7. **Swapnanil Ray** - [redhatpanda](https://github.com/redhatpanda)
8. **Full Name** - [User Name](https://github.com/username)


Expand Down