-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecrypt-data.py
More file actions
60 lines (53 loc) · 1.83 KB
/
decrypt-data.py
File metadata and controls
60 lines (53 loc) · 1.83 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
import psycopg2
import subprocess
# Database configuration
DB_HOST = 'localhost'
DB_PORT = '5436'
DB_NAME = 'testdb'
DB_USER = 'postgres'
DB_PASSWORD = 'postgres'
# Function to decrypt data using GPG and private key file
def decrypt_data(data):
process = subprocess.Popen(['gpg', '--decrypt', '--batch', '--quiet', '--pinentry-mode', 'loopback', '--passphrase-file', 'passphrase.txt'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
decrypted_data, _ = process.communicate(input=data)
return decrypted_data
# Define a function to connect to the database and retrieve encrypted data
def get_encrypted_data():
conn = psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
dbname=DB_NAME,
user=DB_USER,
password=DB_PASSWORD
)
cur = conn.cursor()
cur.execute("SELECT credit_card FROM public.cc")
rows = cur.fetchall()
cur.close()
conn.close()
return rows
# Decrypt encrypted data
# Decrypt encrypted data
def decrypt_rows(rows):
decrypted_rows = []
for row in rows:
bytea_data = row[0] # Get bytea data from the row
# Convert bytea data to byte string
encrypted_data = bytes(bytea_data)
# Decrypt the byte string
try:
decrypted_data = decrypt_data(encrypted_data)
decrypted_rows.append(decrypted_data.decode('utf-8'))
except Exception as e:
print(f"Error decrypting data: {e}")
decrypted_rows.append("Decryption Error")
return decrypted_rows
# Open the private key file in binary mode
def main():
encrypted_data_rows = get_encrypted_data()
decrypted_data_rows = decrypt_rows(encrypted_data_rows)
for decrypted_data in decrypted_data_rows:
print("Decrypted Data:", decrypted_data)
if __name__ == "__main__":
main()