今日學習目標是了解為什麼「只靠資料庫權限」不足以保障醫療資料安全並且學會「欄位加密」。
一、理論重點
二、案例分享
芬蘭 Vastaamo 心理治療中心在 2020 年發生嚴重資安外洩,駭客入侵 MySQL 資料庫後,因缺乏欄位加密與權限控管,導致數萬名病患的心理治療紀錄(以明文儲存)被竊取並用來勒索,最終公司破產。此事件顯示醫療系統必須實作 欄位加密 與 最小化權限管理,才能避免敏感醫療資料一旦資料庫被突破就完全外洩。
三、簡單程式示範
import mysql.connector
from cryptography.fernet import Fernet
# 建立加密器
key = Fernet.generate_key()
cipher = Fernet(key)
# 連線到 MySQL
conn = mysql.connector.connect(
host="localhost",
user="root",
password="your_password",
database="hospital"
)
cur = conn.cursor()
# 建立資料表
cur.execute("DROP TABLE IF EXISTS patients")
cur.execute("""
CREATE TABLE patients (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARBINARY(255),
diagnosis VARBINARY(255)
)
""")
# 插入加密資料
name = "Alice"
diagnosis = "Diabetes Type 2"
enc_name = cipher.encrypt(name.encode())
enc_diag = cipher.encrypt(diagnosis.encode())
cur.execute("INSERT INTO patients (name, diagnosis) VALUES (%s, %s)", (enc_name, enc_diag))
conn.commit()
# 查詢並解密
cur.execute("SELECT id, name, diagnosis FROM patients")
for row in cur.fetchall():
dec_name = cipher.decrypt(row[1]).decode()
dec_diag = cipher.decrypt(row[2]).decode()
print(f"病患 {dec_name} - 診斷: {dec_diag}")
conn.close()
這段程式碼示範了如何在 MySQL 中安全存放敏感欄位。資料在寫入前會先加密,查詢時再由應用程式端解密還原,確保即使資料庫被存取,攻擊者也無法直接讀懂內容,只有掌握金鑰的系統才能還原成真實資訊,達到資料保護與醫療資訊安全的目的。執行結果如下