⚠️ 網站服務遷移公告 (System Alert)
這段程式碼主要包含兩個主要函式以及一個用於安全輸入的輔助函式。為了日後維護方便,以下詳細紀錄每一部分的功能與運作流程。
1. Secure-Prompt 函式
這個函式用於安全地提示用戶輸入敏感資料,如加密金鑰或密碼。
- 功能:接收一個參數
Prompt,該參數是提示用戶輸入的文字。 - 機制:使用
Read-Host命令提示用戶輸入,並且使用-AsSecureString參數來安全地接收用戶輸入(螢幕顯示為 * 星號)。 - 輸出:將安全字串轉換為明文文字,以便後續處理。
2. Encrypt-Data 函式
這個函式用於加密提供的明文資料並將加密後的資料寫入指定的檔案中。
函式參數說明:
| 參數名稱 | 用途描述 |
|---|---|
| $PlainText | 輸入的明文資料。 |
| $KeyFilePath | 加密金鑰檔案的路徑。 |
| $IVFilePath | 初始化向量檔案的路徑。 |
| $EncryptedFilePath | 加密後的資料檔案的路徑。 |
執行邏輯:
- 讀取指定路徑的加密金鑰和初始化向量,並將其轉換為適當的位元組陣列。
- 使用
System.Security.Cryptography.Aes類別建立 AES 加密演算法物件,並設定金鑰和初始化向量。 - 建立加密器並對輸入的明文資料進行加密,最後將加密後的位元組陣列寫入指定的加密後資料檔案 (
$EncryptedFilePath)。 - 使用
Write-Host顯示加密成功的訊息或是錯誤訊息,並在結束時釋放相關的資源。
主要流程 (Main Workflow)
- 設定檔案路徑:指定加密金鑰、初始化向量和加密後資料的檔案路徑。
- 安全輸入:使用
Secure-Prompt函式來安全地輸入要加密的資料。 - 生成金鑰和初始化向量:使用
System.Security.Cryptography.Aes類別生成隨機的金鑰和初始化向量,將它們轉換為 Base64 格式並存儲到指定的檔案中。 - 加密資料:呼叫
Encrypt-Data函式,將安全輸入的明文資料加密並存儲到指定的加密後資料檔案中。
注意事項與技術細節
- 程式碼中使用了
try...catch...finally結構來確保資源的正確釋放,以及處理可能發生的錯誤。 - 加密和解密的過程使用了 .NET Framework 提供的 AES 加密演算法,這是一種強大且廣泛使用的對稱加密演算法。
這段程式碼整體上提供了一個完整的流程,從安全輸入資料到加密和存儲,並包含了錯誤處理和資源管理。
[程式碼] AES 加密工具腳本
# ==========================================
# PowerShell AES Encryption Tool (V6 Part 1)
# ==========================================
# 定義函數:加密資料
function Encrypt-Data {
param (
[string]$PlainText, # 輸入的明文資料
[string]$KeyFilePath, # 加密金鑰檔案路徑
[string]$IVFilePath, # 初始化向量檔案路徑
[string]$EncryptedFilePath # 加密後的檔案路徑
)
Write-Host "加密資料..."
try {
# 讀取金鑰和初始化向量
$KeyBase64 = Get-Content -Path $KeyFilePath
$IVBase64 = Get-Content -Path $IVFilePath
# 將金鑰和初始化向量轉換為位元組陣列
$KeyBytes = [System.Convert]::FromBase64String($KeyBase64)
$IVBytes = [System.Convert]::FromBase64String($IVBase64)
# 建立 AES 加密演算法物件並設定金鑰與初始化向量
$AesAlg = [System.Security.Cryptography.Aes]::Create()
$AesAlg.Key = $KeyBytes
$AesAlg.IV = $IVBytes
# 建立加密器並進行加密
$encryptor = $AesAlg.CreateEncryptor($AesAlg.Key, $AesAlg.IV)
$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() }
if ($encryptor) { $encryptor.Dispose() }
}
}
# 安全地提示用戶輸入資料
function Secure-Prompt {
param (
[string]$Prompt # 提示用戶的文字
)
$SecureString = Read-Host -Prompt $Prompt -AsSecureString
$PlainText = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecureString))
return $PlainText
}
# --- 設定與執行區 ---
$KeyFilePath = "C:\temp\aes_key.txt" # 加密金鑰檔案路徑
$IVFilePath = "C:\temp\aes_iv.txt" # 初始化向量檔案路徑
$EncryptedFilePath = "C:\temp\encrypted_data.txt" # 加密後的資料檔案路徑
# 提示用戶輸入要加密的資料
$DataToEncrypt = Secure-Prompt -Prompt "請輸入要加密的資料"
try {
# 生成加密所需的金鑰和初始化向量(IV)
$AES = [System.Security.Cryptography.Aes]::Create()
$AES.GenerateKey()
$AES.GenerateIV()
# 將金鑰和IV轉換為Base64格式並存儲到檔案
$KeyBase64 = [System.Convert]::ToBase64String($AES.Key) # 將金鑰轉換為Base64格式
$IVBase64 = [System.Convert]::ToBase64String($AES.IV) # 將初始化向量轉換為Base64格式
$KeyBase64 | Out-File -FilePath $KeyFilePath -Force # 將Base64格式的金鑰寫入到檔案
$IVBase64 | Out-File -FilePath $IVFilePath -Force # 將Base64格式的初始化向量寫入到檔案
Write-Host "生成的加密金鑰已保存到 $KeyFilePath。" -ForegroundColor Green # 提示金鑰已保存
Write-Host "生成的初始化向量(IV)已保存到 $IVFilePath。" -ForegroundColor Green # 提示初始化向量已保存
# 呼叫加密函式
Encrypt-Data -PlainText $DataToEncrypt -KeyFilePath $KeyFilePath -IVFilePath $IVFilePath -EncryptedFilePath $EncryptedFilePath
Write-Host "加密完成。" # 加密完成提示
} catch {
Write-Host "生成金鑰和初始化向量時發生錯誤: $_" -ForegroundColor Red
} finally {
if ($AES) { $AES.Dispose() }
}
參考資料: ChatGPT 問答測試結果
文章標籤
全站熱搜

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