🚀 新版發布:IIS SSL 自動化腳本 V6 (完整版)
您目前觀看的是舊版本教學。為了提升安全性與自動化穩定度,我已釋出最新的 V6 完整版,整合了 AES 加密與自動解密流程。
👉 點此前往:[PowerShell] IIS SSL 憑證自動更新全攻略 (V6)(包含完整原始碼、加密設定與排程教學)
這段程式碼的主要功能是用來加密一個使用者輸入的憑證私鑰密碼,並將加密後的結果存儲到指定的文件中。以下是每個部分的功能說明:
-
Encrypt-Password 函數:
- 接受三個參數:
$Password(要加密的私鑰密碼)、$KeyFilePath(存儲加密金鑰的文件路徑)、$EncryptedPasswordFilePath(存儲加密後密碼的文件路徑)。 - 從指定的
$KeyFilePath讀取加密金鑰。 - 將
$Password轉換為安全字串 (SecureString)。 - 使用從檔案讀取的加密金鑰來對安全字串進行 AES 加密 (
ConvertFrom-SecureString)。 - 將加密後的結果寫入到指定的
$EncryptedPasswordFilePath文件中。
- 接受三個參數:
-
生成隨機的加密密鑰:
- 使用
Get-Random生成一個隨機的 AES 加密金鑰。 - 將這個隨機生成的金鑰以 Base64 格式保存到
$KeyFilePath中。
- 使用
-
提示用戶輸入憑證私鑰密碼:
- 使用
Read-Host提示用戶輸入憑證私鑰密碼,輸入時會顯示為安全字串。 - 將輸入的安全字串轉換為普通字串,以便後續加密過程使用。
- 使用
-
加密證書私鑰密碼並存儲到文件:
- 呼叫
Encrypt-Password函數,將用戶輸入的私鑰密碼、加密金鑰文件路徑和加密後密碼文件路徑作為參數傳遞進去。 - 函數會將加密後的私鑰密碼存儲到指定的文件中,同時在控制台顯示操作成功的訊息。
- 呼叫
這段程式碼的目的是確保用戶輸入的私鑰密碼在存儲時是安全加密的,以保護敏感資訊不被未授權訪問。
# 定義加密函數
function Encrypt-Password {
param (
[string]$Password,
[string]$KeyFilePath,
[string]$EncryptedPasswordFilePath
)
Write-Host "加密憑證私鑰密碼..."
$Key = [System.Convert]::FromBase64String((Get-Content $KeyFilePath))
$SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force
$EncryptedPassword = $SecurePassword | ConvertFrom-SecureString -Key $Key
$EncryptedPassword | Out-File -FilePath $EncryptedPasswordFilePath -Force
Write-Host "憑證私鑰密碼已成功加密並存儲到 $EncryptedPasswordFilePath。" -ForegroundColor Green
}
# 生成隨機的加密密鑰,並保存到文件
$KeyFilePath = "c:\temp\encryption_key.txt"
$EncryptedPasswordFilePath = "c:\temp\encrypted_password.txt"
$AES_Key = [System.Convert]::ToBase64String((1..32 | ForEach-Object { Get-Random -Minimum 0 -Maximum 256 }))
$AES_Key | Out-File -FilePath $KeyFilePath -Force
Write-Host "生成的加密密鑰已保存到 $KeyFilePath。" -ForegroundColor Green
# 提示用戶輸入證書私鑰密碼進行加密
$Password = Read-Host -Prompt "請輸入憑證私鑰密碼" -AsSecureString
$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))
# 加密證書私鑰密碼並存儲到文件
Encrypt-Password -Password $Password -KeyFilePath $KeyFilePath -EncryptedPasswordFilePath $EncryptedPasswordFilePath
參考資料:
ChatGPT 問答測試結果
文章標籤
全站熱搜

留言功能已依作者設定調整顯示方式