-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathe2ee_messaging_protocol.py
More file actions
62 lines (51 loc) · 2.03 KB
/
e2ee_messaging_protocol.py
File metadata and controls
62 lines (51 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.exceptions import InvalidTag
class E2EEMessagingProtocol:
def __init__(self, key: bytes):
"""
Initialize the protocol with a 256-bit (32-byte) shared key.
Args:
key (bytes): 32-byte shared secret key.
"""
if not isinstance(key, bytes) or len(key) != 32:
raise ValueError("Key must be 32 bytes (256 bits)")
self.key = key
self.aesgcm = AESGCM(self.key)
def encrypt_message(self, plaintext: str) -> bytes:
"""
Encrypt a message using AES-256 GCM.
Args:
plaintext (str): Message to encrypt.
Returns:
bytes: Concatenation of nonce and ciphertext (nonce || ciphertext).
"""
# Encode plaintext to bytes
data = plaintext.encode("utf-8")
# Generate a unique 12-byte nonce
nonce = os.urandom(12)
# Encrypt the data with AES-GCM
ciphertext = self.aesgcm.encrypt(nonce, data, associated_data=None)
# Return nonce + ciphertext
return nonce + ciphertext
# made by modelguyzz
def decrypt_message(self, encrypted_message: bytes) -> str:
"""
Decrypt a message using AES-256 GCM.
Args:
encrypted_message (bytes): The concatenation of nonce and ciphertext.
Returns:
str: Decrypted plaintext.
Raises:
ValueError: If decryption fails or the message is tampered with.
"""
if len(encrypted_message) < 12:
raise ValueError("Encrypted message is too short to contain nonce")
# Extract nonce (first 12 bytes)
nonce = encrypted_message[:12]
ciphertext = encrypted_message[12:]
try:
decrypted_data = self.aesgcm.decrypt(nonce, ciphertext, associated_data=None)
except InvalidTag:
raise ValueError("Invalid authentication tag. Message may be tampered with.")
return decrypted_data.decode("utf-8")