5.
Data Anonymization (Masking and k-Anonymity)
import pandas as pd
data = {'Name': ['John', 'Alice', 'Bob', 'Carol', 'David'],
'Age': [25, 30, 25, 40, 30],
'City': ['NY', 'LA', 'NY', 'LA', 'NY']}
df = pd.DataFrame(data)
df['Name'] = df['Name'].apply(lambda x: x[0] + '*')
df['Age'] = df['Age'].apply(lambda x: '20-30' if 20 <= x <= 30 else '31-40')
print(df)
output
Name Age City
0 J* 20-30 NY
1 A* 20-30 LA
2 B* 20-30 NY
3 C* 31-40 LA
4 D* 20-30 NY
---
6. Image Encryption (Simple XOR Encryption)
from PIL import Image
import numpy as np
def encrypt_image(image_path, key=123):
img = Image.open(image_path)
img_array = np.array(img)
encrypted = img_array ^ key
encrypted_img = Image.fromarray(encrypted)
encrypted_img.save('encrypted_image.png')
encrypt_image('sample.png')
---
7. Image Obfuscation (Blurring)
import cv2
def blur_image(image_path):
img = cv2.imread(image_path)
blurred = cv2.GaussianBlur(img, (21, 21), 0)
cv2.imwrite('blurred_image.png', blurred)
blur_image('C:/Users/Admin/Downloads/butterfly.png')
---
8. Role-Based Access Control (RBAC)
roles = {
'admin': ['view', 'edit', 'delete'],
'user': ['view'],
'guest': []
}
def check_access(role, action):
if action in roles.get(role, []):
print(f"Access granted to {role} for {action}")
else:
print(f"Access denied to {role} for {action}")
check_access('admin', 'edit')
check_access('guest', 'view')
output
Access granted to admin for edit
Access denied to guest for view
---
9. Attribute-Based Access Control (ABAC)
def abac_access(user):
if user['department'] == 'HR' and user['clearance'] >= 2:
print("Access granted")
else:
print("Access denied")
user1 = {'department': 'HR', 'clearance': 3}
user2 = {'department': 'IT', 'clearance': 1}
abac_access(user1)
abac_access(user2)
output
Access granted
Access denied
---
10. Log Monitoring System with Incident Management (Simple Cloud Logs)
logs = [
"INFO: Server started",
"ERROR: Database connection failed",
"INFO: User login",
"CRITICAL: Security breach detected"
]
def monitor_logs(logs):
for log in logs:
if "ERROR" in log or "CRITICAL" in log:
print(f"Incident: {log}")
monitor_logs(logs)
output
Incident: ERROR: Database connection failed
Incident: CRITICAL: Security breach detected
---