Skip to content

Commit

Permalink
Create end_to_end_encryption.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Sep 4, 2024
1 parent 0db9e4e commit fa674d2
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions ai_driven_analitycs/end_to_end_encryption.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import hashlib
import os
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import rsa, padding
from cryptography.hazmat.backends import default_backend

class EndToEndEncryption:
def __init__(self, private_key_path, public_key_path):
self.private_key = serialization.load_pem_private_key(
open(private_key_path, 'rb').read(),
password=None,
backend=default_backend()
)
self.public_key = serialization.load_pem_public_key(
open(public_key_path, 'rb').read(),
backend=default_backend()
)

def encrypt(self, data):
encrypted_data = self.public_key.encrypt(
data,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return encrypted_data

def decrypt(self, encrypted_data):
decrypted_data = self.private_key.decrypt(
encrypted_data,
padding.OAEP(
mgf=padding.MGF1(algorithm=hashes.SHA256()),
algorithm=hashes.SHA256(),
label=None
)
)
return decrypted_data

# Example usage:
private_key_path = 'path/to/private_key.pem'
public_key_path = 'path/to/public_key.pem'
e2e_encryption = EndToEndEncryption(private_key_path, public_key_path)

data = b'Hello, Universe!'
encrypted_data = e2e_encryption.encrypt(data)
print(encrypted_data)

decrypted_data = e2e_encryption.decrypt(encrypted_data)
print(decrypted_data)

0 comments on commit fa674d2

Please sign in to comment.