[PowerShell] 自動化更新 IIS SSL 憑證全攻略 (V6 完整版):加密與解密實作
本篇教學整理了 PowerShell V6 版自動化更新 IIS SSL 憑證的完整攻略,重點實作 AES 加密與解密流程。透過將密碼加密儲存,解決腳本明碼風險,並提供詳細的函式參數說明與終端機代碼範例,適合網管人員參考。
📝 寫在前面:
本文整合 PowerShell V6 版精華,示範如何安全加密 IIS SSL 憑證密碼並自動化管理。如果你不想把密碼「明碼」寫在腳本裡,這篇內容可以幫助你快速建立 AES 加密與解密流程。我們也將每個參數與流程詳細記錄,方便未來維護與回顧。
本文整合 PowerShell V6 版精華,示範如何安全加密 IIS SSL 憑證密碼並自動化管理。如果你不想把密碼「明碼」寫在腳本裡,這篇內容可以幫助你快速建立 AES 加密與解密流程。我們也將每個參數與流程詳細記錄,方便未來維護與回顧。
V6 版本核心概念簡單:用 AES 加密保護密碼,需要使用時再解密。即使腳本被外人取得,沒有金鑰 (Key) 也無法還原密碼,安全性大幅提升。
Part 1:加密工具 (Encrypt) - 製作鑰匙
本段程式碼包含兩個函式與一個安全輸入輔助函式,以下詳解每部分功能與運作流程:
1. Secure-Prompt 函式 (安全輸入)
提示用戶輸入敏感資料,如加密金鑰或密碼。
- 功能:接收參數
Prompt,提示用戶輸入文字。 - 機制:使用
Read-Host+-AsSecureString安全接收輸入(螢幕顯示星號)。 - 輸出:將安全字串轉換為明文文字,以便後續加密使用。
2. Encrypt-Data 函式 (加密核心)
加密提供的明文資料並將加密後的資料寫入指定的檔案中。
📦 參數詳細說明:
| 參數名稱 | 用途描述 |
|---|---|
| $PlainText | 輸入的明文資料(欲加密的原始密碼)。 |
| $KeyFilePath | 加密金鑰檔案的路徑(用於存儲生成的 AES Key)。 |
| $IVFilePath | 初始化向量檔案的路徑(增加加密強度的 IV)。 |
| $EncryptedFilePath | 加密後的資料檔案輸出的路徑。 |
⚙️ 執行邏輯與流程:
- 讀取指定路徑的加密金鑰與初始化向量,轉換為位元組陣列。
- 使用
System.Security.Cryptography.Aes類別建立 AES 物件。 - 建立加密器並對明文資料進行加密,將結果寫入
$EncryptedFilePath。 - 使用
Write-Host顯示成功訊息,並在結束時釋放系統資源。
👨💻 [Code] 完整加密工具腳本
# 定義安全提示輸入函式
function Secure-Prompt {
param([string]$Prompt)
$secureString = Read-Host -Prompt $Prompt -AsSecureString
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
try { [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr) } finally { [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ptr) }
}
# 定義執行加密函式
function Encrypt-Data {
param($PlainText, $KeyFilePath, $IVFilePath, $EncryptedFilePath)
$keyBytes = [Convert]::FromBase64String((Get-Content -Path $KeyFilePath -Raw))
$ivBytes = [Convert]::FromBase64String((Get-Content -Path $IVFilePath -Raw))
$aes = [System.Security.Cryptography.Aes]::Create()
$aes.Key = $keyBytes
$aes.IV = $ivBytes
$encryptor = $aes.CreateEncryptor()
$plainBytes = [System.Text.Encoding]::UTF8.GetBytes($PlainText)
$encryptedBytes = $encryptor.TransformFinalBlock($plainBytes, 0, $plainBytes.Length)
[System.IO.File]::WriteAllBytes($EncryptedFilePath, $encryptedBytes)
Write-Host "✅ 加密完成並存儲至: $EncryptedFilePath" -ForegroundColor Green
}
function Secure-Prompt {
param([string]$Prompt)
$secureString = Read-Host -Prompt $Prompt -AsSecureString
$ptr = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
try { [System.Runtime.InteropServices.Marshal]::PtrToStringBSTR($ptr) } finally { [System.Runtime.InteropServices.Marshal]::ZeroFreeBSTR($ptr) }
}
# 定義執行加密函式
function Encrypt-Data {
param($PlainText, $KeyFilePath, $IVFilePath, $EncryptedFilePath)
$keyBytes = [Convert]::FromBase64String((Get-Content -Path $KeyFilePath -Raw))
$ivBytes = [Convert]::FromBase64String((Get-Content -Path $IVFilePath -Raw))
$aes = [System.Security.Cryptography.Aes]::Create()
$aes.Key = $keyBytes
$aes.IV = $ivBytes
$encryptor = $aes.CreateEncryptor()
$plainBytes = [System.Text.Encoding]::UTF8.GetBytes($PlainText)
$encryptedBytes = $encryptor.TransformFinalBlock($plainBytes, 0, $plainBytes.Length)
[System.IO.File]::WriteAllBytes($EncryptedFilePath, $encryptedBytes)
Write-Host "✅ 加密完成並存儲至: $EncryptedFilePath" -ForegroundColor Green
}
Part 2:解密工具 (Decrypt) - 驗證還原
加密後必須驗證產生的金鑰檔案是否能正確還原回原始資料。這段程式碼定義了 Decrypt-DataAndDisplay 函式,用於自動化腳本的「驗證階段」。
程式解密運作邏輯
- 讀取階段:程式從指定路徑讀取加密檔、Key 與 IV。
- 還原金鑰:將 Base64 格式的金鑰與 IV 轉換回原本的 Byte 陣列。
- 建立解密物件:利用
CreateDecryptor建立 AES 解密物件。 - 輸出明文:將還原後的 Byte 陣列轉碼為 UTF-8 字串顯示於控制台。
👨💻 [Code] 解密與驗證測試腳本
function Decrypt-DataAndDisplay {
param($EncryptedFilePath, $KeyFilePath, $IVFilePath)
$keyBytes = [Convert]::FromBase64String((Get-Content -Path $KeyFilePath -Raw))
$ivBytes = [Convert]::FromBase64String((Get-Content -Path $IVFilePath -Raw))
$aes = [System.Security.Cryptography.Aes]::Create()
$aes.Key = $keyBytes
$aes.IV = $ivBytes
$decryptor = $aes.CreateDecryptor()
$encryptedBytes = [System.IO.File]::ReadAllBytes($EncryptedFilePath)
$plainBytes = $decryptor.TransformFinalBlock($encryptedBytes, 0, $encryptedBytes.Length)
$plainText = [System.Text.Encoding]::UTF8.GetString($plainBytes)
Write-Host "🔓 解密成功,明文資料:" -ForegroundColor Green
Write-Host $plainText -ForegroundColor Yellow
}
param($EncryptedFilePath, $KeyFilePath, $IVFilePath)
$keyBytes = [Convert]::FromBase64String((Get-Content -Path $KeyFilePath -Raw))
$ivBytes = [Convert]::FromBase64String((Get-Content -Path $IVFilePath -Raw))
$aes = [System.Security.Cryptography.Aes]::Create()
$aes.Key = $keyBytes
$aes.IV = $ivBytes
$decryptor = $aes.CreateDecryptor()
$encryptedBytes = [System.IO.File]::ReadAllBytes($EncryptedFilePath)
$plainBytes = $decryptor.TransformFinalBlock($encryptedBytes, 0, $encryptedBytes.Length)
$plainText = [System.Text.Encoding]::UTF8.GetString($plainBytes)
Write-Host "🔓 解密成功,明文資料:" -ForegroundColor Green
Write-Host $plainText -ForegroundColor Yellow
}
⚠️ 安全提醒:
生成的
生成的
aes_key.txt 與 aes_iv.txt 必須妥善保管。在自動化流程中,建議將金鑰存儲在具備存取控制的網路空間,而非與加密檔案放在同一路徑下。
💬 網管心情隨筆:
今天嘗試整理這套 PowerShell AES 加密流程,發現以前寫的程式碼太零散了,花了一上午才把它整合成 V6 版。雖然累,但看到完整流程跑通的瞬間,真的很有成就感。這不僅是自動化,更是對資料安全的一種負責。希望這篇文章對自己和讀者都能有幫助。
今天嘗試整理這套 PowerShell AES 加密流程,發現以前寫的程式碼太零散了,花了一上午才把它整合成 V6 版。雖然累,但看到完整流程跑通的瞬間,真的很有成就感。這不僅是自動化,更是對資料安全的一種負責。希望這篇文章對自己和讀者都能有幫助。
參考資料:ChatGPT 技術問答與實作驗證成果
留言
張貼留言