CAPICOM (Cryptographic API COM) 提供 COM 介面讓 VBScript/JScript 呼叫 Windows 加密服務。本文提供官方範例改良版,支援多種對稱加密演算法、金鑰長度、檔案加密解密完整流程。
cscript.exe 執行 (非 wscript)。regsvr32 capicom.dll。Option Explicit
Const ForReading = 1, ForWriting = 2
Const CMD_UNKNOWN = 0, CMD_ENCRYPT = 1, CMD_DECRYPT = 2
' CAPICOM 演算法常數
Const CAPICOM_ENCRYPTION_ALGORITHM_RC2 = 0
Const CAPICOM_ENCRYPTION_ALGORITHM_RC4 = 1
Const CAPICOM_ENCRYPTION_ALGORITHM_DES = 2
Const CAPICOM_ENCRYPTION_ALGORITHM_3DES = 3
Const CAPICOM_ENCRYPTION_ALGORITHM_AES = 4
Const CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM = 0
Const CAPICOM_ENCRYPTION_KEY_LENGTH_40_BITS = 1
Const CAPICOM_ENCRYPTION_KEY_LENGTH_56_BITS = 2
Const CAPICOM_ENCRYPTION_KEY_LENGTH_128_BITS = 3
Const CAPICOM_ENCRYPTION_KEY_LENGTH_192_BITS = 4
Const CAPICOM_ENCRYPTION_KEY_LENGTH_256_BITS = 5
Dim cmd, password, algorithm, keyLength, verbose, files()
cmd = CMD_UNKNOWN: password = Empty
algorithm = CAPICOM_ENCRYPTION_ALGORITHM_RC2
keyLength = CAPICOM_ENCRYPTION_KEY_LENGTH_MAXIMUM
verbose = False
' 必須用 CScript
If InStr(1, UCase(WScript.FullName), "CSCRIPT.EXE", 1) = 0 Then
WScript.Echo "Must run with CScript.exe"
WScript.Quit(-1)
End If
' 解析命令列參數 (略,完整版見原文)
' ParseCommandLine()
Select Case cmd
Case CMD_ENCRYPT: DoEncryptCommand files, algorithm, keyLength, password
Case CMD_DECRYPT: DoDecryptCommand files, password
End Select
WScript.Quit(0)
Sub DoEncryptCommand(files, alg, keyLen, pwd)
Dim content, encryptedData, msg
Set encryptedData = CreateObject("CAPICOM.EncryptedData")
encryptedData.Algorithm.Name = alg
encryptedData.Algorithm.KeyLength = keyLen
encryptedData.SetSecret pwd
WScript.StdOut.WriteLine "Encrypting: " & files(0)
If verbose Then DisplayDetail encryptedData
LoadFile files(0), content
encryptedData.Content = content
msg = encryptedData.Encrypt
SaveFile files(1), msg
WScript.StdOut.WriteLine "Saved to: " & files(1)
Set encryptedData = Nothing
End Sub
Sub DoDecryptCommand(files, pwd)
Dim encryptedData, msg, content
Set encryptedData = CreateObject("CAPICOM.EncryptedData")
encryptedData.SetSecret pwd
WScript.StdOut.WriteLine "Decrypting: " & files(0)
LoadFile files(0), content
encryptedData.Content = content
msg = encryptedData.Decrypt
SaveFile files(1), msg
WScript.StdOut.WriteLine "Saved to: " & files(1)
Set encryptedData = Nothing
End Sub
Sub LoadFile(path, ByRef content)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(path, ForReading)
content = f.ReadAll
f.Close
End Sub
Sub SaveFile(path, content)
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(path, ForWriting, True)
f.Write content
f.Close
End Sub
Sub DisplayDetail(ed)
WScript.StdOut.WriteLine "Algorithm: " & ed.Algorithm.Name
WScript.StdOut.WriteLine "KeyLength: " & ed.Algorithm.KeyLength
End Sub
REM 加密 (AES 256-bit)
cscript //nologo CEncrypt.vbs /e /a 4 /k 5 /p "MySecretPass" plain.txt encrypted.bin
REM 解密
cscript //nologo CEncrypt.vbs /d /p "MySecretPass" encrypted.bin decrypted.txt
參數說明:
/e 加密、/d 解密/a 演算法 (0=RC2, 1=RC4, 2=DES, 3=3DES, 4=AES)/k 金鑰長度 (0=最大, 3=128-bit, 5=256-bit)/p 密碼/v 詳細輸出| 演算法 | 安全性 | 速度 | 建議用途 |
|---|---|---|---|
| AES-256 | 極高 | 快 (硬體加速) | 現代標準、機密資料 |
| 3DES | 高 | 中 | 相容舊系統 |
| RC4 | 低 (已知弱點) | 極快 | **不建議新專案使用** |
| RC2/DES | 低 | 中/慢 | 僅相容極舊環境 |
System.Security.Cryptography 或 PowerShell Protect-CmsMessage**。此腳本適合舊系統維護、簡單檔案加密自動化、或學習 COM 介面呼叫加密服務的基本模式。