iT邦幫忙

2025 iThome 鐵人賽

DAY 13
0
Security

醫療數據的資安挑戰與創新解決方案系列 第 13

資料庫欄位加密(Field Encryption)

  • 分享至 

  • xImage
  •  

今日學習目標是了解為什麼「只靠資料庫權限」不足以保障醫療資料安全並且學會「欄位加密」。

一、理論重點

  1. 欄位加密用途
  • 即使駭客偷走資料庫檔案(例如 SQLite DB),沒有金鑰也看不到明文資料。
  1. 常見做法
  • 使用 AES 對稱加密儲存敏感欄位(姓名、病歷、診斷)。
  • 金鑰需安全保存(不能直接硬編碼在程式)。

二、案例分享

芬蘭 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 中安全存放敏感欄位。資料在寫入前會先加密,查詢時再由應用程式端解密還原,確保即使資料庫被存取,攻擊者也無法直接讀懂內容,只有掌握金鑰的系統才能還原成真實資訊,達到資料保護與醫療資訊安全的目的。執行結果如下
https://ithelp.ithome.com.tw/upload/images/20250918/20169331dKXIhfRcde.png


上一篇
HTTPS 在醫療情境的真實應用與傳輸驗證
系列文
醫療數據的資安挑戰與創新解決方案13
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言