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

data anonymization #26

Merged
merged 3 commits into from
Oct 28, 2024
Merged
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
Binary file not shown.
147 changes: 147 additions & 0 deletions project2 data anonymization/encryption.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv\n",
"import os\n",
"import requests\n",
"import pandas as pd\n",
"from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes\n",
"from cryptography.hazmat.backends import default_backend\n",
"import base64\n",
"\n",
"# Load the .env file\n",
"load_dotenv(dotenv_path='api.env')\n",
"\n",
"# Fetch environment variables\n",
"channel1_api_key = os.getenv('CHANNEL1_API_KEY')\n",
"channel2_api_key = os.getenv('CHANNEL2_API_KEY')\n",
"\n",
"# Define API details\n",
"channels = {\n",
" \"Channel1\": {\n",
" \"url\": f\"https://api.thingspeak.com/channels/2561136/feeds.json?api_key={channel1_api_key}\",\n",
" \"fields\": [\"created_at\", \"field2\"],\n",
" \"rename\": {\n",
" \"created_at\": \"timestamp\",\n",
" \"field2\": \"Body_temp\"\n",
" }\n",
" },\n",
" \"Channel2\": {\n",
" \"url\": f\"https://api.thingspeak.com/channels/2561145/feeds.json?api_key={channel2_api_key}\",\n",
" \"fields\": [\"created_at\", \"field2\", \"field3\"],\n",
" \"rename\": {\n",
" \"created_at\": \"timestamp\",\n",
" \"field2\": \"heartRate\",\n",
" \"field3\": \"SP02\"\n",
" }\n",
" }\n",
"}\n",
"\n",
"# Function to fetch and transform data\n",
"def fetch_and_transform_data(channel_url, fields, rename_columns):\n",
" try:\n",
" response = requests.get(channel_url)\n",
" response.raise_for_status()\n",
" data = response.json()\n",
"\n",
" if 'feeds' not in data:\n",
" print(\"Error: 'feeds' key not found in the API response.\")\n",
" return pd.DataFrame()\n",
"\n",
" df = pd.DataFrame(data['feeds'])\n",
" df = df[fields]\n",
" df.rename(columns=rename_columns, inplace=True)\n",
" df['timestamp'] = pd.to_datetime(df['timestamp'])\n",
" for column in df.columns:\n",
" if column != 'timestamp':\n",
" df[column] = pd.to_numeric(df[column], errors='coerce').round(2)\n",
"\n",
" return df\n",
"\n",
" except requests.exceptions.RequestException as e:\n",
" print(f\"Error fetching data from {channel_url}: {e}\")\n",
" return pd.DataFrame()\n",
" except Exception as e:\n",
" print(f\"An error occurred: {e}\")\n",
" return pd.DataFrame()\n",
"\n",
"# Function to generate a new AES key\n",
"def generate_aes_key():\n",
" return os.urandom(32) \n",
"\n",
"# Function to encrypt a message using AES\n",
"def encrypt_message(key, plaintext):\n",
" iv = os.urandom(16) # 128-bit IV\n",
" cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())\n",
" encryptor = cipher.encryptor()\n",
" ciphertext = encryptor.update(plaintext.encode()) + encryptor.finalize()\n",
" return base64.b64encode(iv + ciphertext).decode()\n",
"\n",
"# Fetch and transform data for both channels\n",
"df_channel_1 = fetch_and_transform_data(channels['Channel1']['url'], channels['Channel1']['fields'], channels['Channel1']['rename'])\n",
"df_channel_2 = fetch_and_transform_data(channels['Channel2']['url'], channels['Channel2']['fields'], channels['Channel2']['rename'])\n",
"\n",
"# Display data before encryption\n",
"print(\"Channel 1 Data Before Encryption:\")\n",
"print(df_channel_1.head())\n",
"\n",
"print(\"\\nChannel 2 Data Before Encryption:\")\n",
"print(df_channel_2.head())\n",
"\n",
"# Encrypt the data\n",
"key = generate_aes_key()\n",
"df_channel_1['Body_temp'] = df_channel_1['Body_temp'].apply(lambda x: encrypt_message(key, str(x)))\n",
"df_channel_2['heartRate'] = df_channel_2['heartRate'].apply(lambda x: encrypt_message(key, str(x)))\n",
"df_channel_2['SP02'] = df_channel_2['SP02'].apply(lambda x: encrypt_message(key, str(x)))\n",
"\n",
"# Display data after encryption \n",
"print(\"\\nChannel 1 Data After Encryption:\")\n",
"print(df_channel_1[['timestamp', 'Body_temp']].head())\n",
"\n",
"print(\"\\nChannel 2 Data After Encryption:\")\n",
"print(df_channel_2[['timestamp', 'heartRate', 'SP02']].head())\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Function to decrypt an encrypted message using AES\n",
"def decrypt_message(key, ciphertext):\n",
" ciphertext = base64.b64decode(ciphertext)\n",
" iv = ciphertext[:16] # Extract the IV from the first 16 bytes\n",
" actual_ciphertext = ciphertext[16:] # The rest is the encrypted message\n",
" cipher = Cipher(algorithms.AES(key), modes.CFB(iv), backend=default_backend())\n",
" decryptor = cipher.decryptor()\n",
" plaintext = decryptor.update(actual_ciphertext) + decryptor.finalize()\n",
" return plaintext.decode()\n",
"\n",
"# Decrypt the data\n",
"df_channel_1['Decrypted_Body_temp'] = df_channel_1['Body_temp'].apply(lambda x: decrypt_message(key, x))\n",
"df_channel_2['Decrypted_heartRate'] = df_channel_2['heartRate'].apply(lambda x: decrypt_message(key, x))\n",
"df_channel_2['Decrypted_SP02'] = df_channel_2['SP02'].apply(lambda x: decrypt_message(key, x))\n",
"\n",
"# Display the decrypted data\n",
"print(\"\\nChannel 1 Data After Decryption:\")\n",
"print(df_channel_1[['timestamp', 'Decrypted_Body_temp']].head())\n",
"\n",
"print(\"\\nChannel 2 Data After Decryption:\")\n",
"print(df_channel_2[['timestamp', 'Decrypted_heartRate', 'Decrypted_SP02']].head())\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Binary file added project2 data anonymization/snowflake.pdf
Binary file not shown.
Loading