close
# 定義加密函數 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 問答測試結果
文章標籤
全站熱搜