[PowerShell] IIS SSL 憑證自動更新全攻略 (V6 加密篇):實作 AES 安全加密流程

這篇是 PowerShell 自動化更新 IIS SSL 憑證 V6 版的「加密篇」。為了解決腳本中明文儲存密碼的資安風險,本篇教學將展示如何利用 .NET Framework 的 AES 類別生成金鑰與 IV,並將敏感資料安全加密為二進位檔案。
📝 前言:
💡 系列導讀: 這是 V6 版本的第一部分(加密篇)。
在實作 IIS SSL 憑證自動更新時,如何安全地傳遞密碼是最大難點。本篇介紹加密腳本的運作原理,閱讀完畢後請接續閱讀:解密篇
(或者 前往 Blogger 閱讀完整合併版)

這段程式碼包含兩個核心函式:Secure-Prompt 用於安全輸入,Encrypt-Data 用於執行 AES 加密。以下詳細紀錄功能與運作流程。

1. Secure-Prompt:安全輸入輔助函式

此函式用於提示用戶輸入敏感資料(如加密金鑰或 PFX 密碼),避免密碼以明文顯示在螢幕上。

  • 參數: 接收 Prompt 文字作為輸入提示。
  • 機制: 呼叫 Read-Host -AsSecureString,輸入過程會顯示為 * 星號。
  • 轉化: 將安全字串還原為記憶體中的明文,供加密函式立即使用。

2. Encrypt-Data:AES 加密執行函式

核心加密邏輯,使用 .NET Framework 的 System.Security.Cryptography.Aes 類別進行對稱加密。

函式參數詳細說明

參數名稱 用途描述
$PlainText 要加密的原始資料(例如:憑證私鑰密碼)。
$KeyFilePath 生成的 AES 加密金鑰 存儲路徑。
$IVFilePath 生成的 初始化向量 (IV) 存儲路徑,增加加密強度。
$EncryptedFilePath 加密後的位元組資料輸出的檔案路徑。

3. 主要執行流程 (Workflow)

  1. 初始化路徑: 定義金鑰、IV 與加密結果的存放位置。
  2. 互動輸入: 呼叫 Secure-Prompt 取得欲保護的密碼。
  3. 金鑰生成: 隨機產生 256-bit 金鑰與 IV,並轉為 Base64 字串存檔。
  4. 資料存檔: 執行加密後,將加密後的二進位數據寫入檔案。
🛡️ 技術細節與安全提醒:
  • 使用了 try...catch...finally 結構確保即便發生錯誤也能正確釋放加密物件資源 (Dispose)。
  • AES 為業界標準的對稱加密演算法,安全性極高,請務必妥善保管生成的 aes_key.txt

4. [程式碼] AES 加密工具腳本

# ========================================== # PowerShell AES Encryption Tool (V6 Part 1) # ========================================== function Encrypt-Data { param ( [string]$PlainText, [string]$KeyFilePath, [string]$IVFilePath, [string]$EncryptedFilePath ) Write-Host "正在執行 AES 加密程序..." -ForegroundColor Cyan try { $KeyBase64 = Get-Content -Path $KeyFilePath $IVBase64 = Get-Content -Path $IVFilePath $KeyBytes = [System.Convert]::FromBase64String($KeyBase64) $IVBytes = [System.Convert]::FromBase64String($IVBase64) $AesAlg = [System.Security.Cryptography.Aes]::Create() $AesAlg.Key = $KeyBytes $AesAlg.IV = $IVBytes $encryptor = $AesAlg.CreateEncryptor() $PlainTextBytes = [System.Text.Encoding]::UTF8.GetBytes($PlainText) $EncryptedBytes = $encryptor.TransformFinalBlock($PlainTextBytes, 0, $PlainTextBytes.Length) [System.IO.File]::WriteAllBytes($EncryptedFilePath, $EncryptedBytes) Write-Host "資料已成功加密並存儲至 $EncryptedFilePath" -ForegroundColor Green } catch { Write-Host "加密失敗: $_" -ForegroundColor Red } finally { if ($AesAlg) { $AesAlg.Dispose() } } } function Secure-Prompt { param ([string]$Prompt) $SecureString = Read-Host -Prompt $Prompt -AsSecureString $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString) return [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) } # --- 執行範例 --- $KeyPath = "C:\temp\aes_key.txt" $IVPath = "C:\temp\aes_iv.txt" $EncPath = "C:\temp\encrypted_data.txt" $Data = Secure-Prompt -Prompt "請輸入欲加密的敏感密碼" try { $AES = [System.Security.Cryptography.Aes]::Create() [System.Convert]::ToBase64String($AES.Key) | Out-File -FilePath $KeyPath -Force [System.Convert]::ToBase64String($AES.IV) | Out-File -FilePath $IVPath -Force Encrypt-Data -PlainText $Data -KeyFilePath $KeyPath -IVFilePath $IVPath -EncryptedFilePath $EncPath Write-Host "AES 金鑰與加密作業完成。" -ForegroundColor Yellow } finally { if ($AES) { $AES.Dispose() } }

📚 參考資源

留言

自訂樣式 ~ CSS -- 簡約相簿

© Copyright 2021 Design By 灰鴿 | Modified by Sungshu ( Powered by Blogger )