Protect-CmsMessage
使用密碼編譯訊息語法格式來加密內容。
語法
ByContent (預設值)
Protect-CmsMessage
[-To] <CmsMessageRecipient[]>
[-Content] <PSObject>
[[-OutFile] <String>]
[<CommonParameters>]
ByPath
Protect-CmsMessage
[-To] <CmsMessageRecipient[]>
[-Path] <String>
[[-OutFile] <String>]
[<CommonParameters>]
ByLiteralPath
Protect-CmsMessage
[-To] <CmsMessageRecipient[]>
[-LiteralPath] <String>
[[-OutFile] <String>]
[<CommonParameters>]
Description
Protect-CmsMessage Cmdlet 會使用密碼編譯訊息語法 (CMS) 格式來加密內容。
CMS Cmdlet 支援使用 IETF 格式來加密和解密內容,如 RFC5652所記載。
CMS 加密標準會使用公鑰密碼編譯,其中用來加密內容的密鑰(公鑰)和用來解密內容的金鑰(私鑰)是分開的。 您的公鑰可以廣泛共用,而且不是敏感數據。 如果有任何內容使用此公鑰加密,則只有您的私鑰才能解密。 如需詳細資訊,請參閱 公鑰密碼編譯。
您必須先設定加密憑證,才能執行 Protect-CmsMessage Cmdlet。
若要在 PowerShell 中辨識,加密憑證需要唯一的外延密鑰使用方式 (EKU) 識別碼,才能將其識別為數據加密憑證(例如程式代碼簽署和加密郵件的識別符)。 如需適用於檔加密的憑證範例,請參閱本主題中的範例 1。
PowerShell 7.1 已新增 Linux 和 macOS 的支援。
範例
範例 1:建立憑證來加密內容
您必須先建立加密憑證,才能執行 Protect-CmsMessage Cmdlet。 使用下列文字,將 [主旨] 行中的名稱變更為您的名稱、電子郵件或其他標識符,並將憑證儲存在檔案中(例如 DocumentEncryption.inf,如本範例所示)。
# Create .INF file for certreq
{[Version]
Signature = "$Windows NT$"
[Strings]
szOID_ENHANCED_KEY_USAGE = "2.5.29.37"
szOID_DOCUMENT_ENCRYPTION = "1.3.6.1.4.1.311.80.1"
[NewRequest]
Subject = "cn=youralias@emailaddress.com"
MachineKeySet = false
KeyLength = 2048
KeySpec = AT_KEYEXCHANGE
HashAlgorithm = Sha1
Exportable = true
RequestType = Cert
KeyUsage = "CERT_KEY_ENCIPHERMENT_KEY_USAGE | CERT_DATA_ENCIPHERMENT_KEY_USAGE"
ValidityPeriod = "Years"
ValidityPeriodUnits = "1000"
[Extensions]
%szOID_ENHANCED_KEY_USAGE% = "{text}%szOID_DOCUMENT_ENCRYPTION%"
} | Out-File -FilePath DocumentEncryption.inf
# After you have created your certificate file, run the following command to add
# the certificate file to the certificate store. Now you are ready to encrypt and
# decrypt content with the next two examples.
certreq.exe -new DocumentEncryption.inf DocumentEncryption.cer
範例 2:加密透過電子郵件傳送的郵件
$Protected = "Hello World" | Protect-CmsMessage -To "*youralias@emailaddress.com*"
在下列範例中,您會將訊息 「Hello World」 加密至 Protect-CmsMessage Cmdlet,然後將加密的訊息儲存在變數中。
To 參數會使用憑證中「主旨」行的值。
範例 3:檢視檔加密憑證
PS C:\> cd Cert:\CurrentUser\My
PS Cert:\CurrentUser\My> Get-ChildItem -DocumentEncryptionCert
若要在憑證提供者中檢視檔加密憑證,您可以新增 DocumentEncryptionCertGet-ChildItem的動態參數,只有在載入憑證提供者時才可使用。
參數
-Content
指定 PSObject,其中包含您想要加密的內容。 例如,您可以加密事件訊息的內容,然後使用包含訊息的變數($Event在此範例中為 ,在此範例中為 ),做為 Content 參數的值:$event = Get-WinEvent -ProviderName "PowerShell" -MaxEvents 1。 您也可以使用 Get-Content Cmdlet 來取得檔案的內容,例如 Microsoft Word 檔,並將內容儲存在您用來做為 Content 參數值的變數中。
參數屬性
| 類型: | PSObject |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
ByContent
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-LiteralPath
指定您要加密的內容路徑。 不同於 Path,LiteralPath 的值則完全按照輸入時的方式使用。 不會將任何字元解譯為通配符。 如果路徑包含逸出字元,請以單引弧括住它。 單引號會告知PowerShell不要將任何字元解譯為逸出序列。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
ByLiteralPath
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-OutFile
指定您要傳送加密內容之檔案的路徑和檔名。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 2 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-Path
指定您要加密的內容路徑。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
ByPath
| Position: | 1 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-To
指定一個或多個 CMS 郵件收件者,可使用以下任何格式識別:
- 從證書提供者獲取的證書。
- 包含憑證之檔案的路徑。
- 包含憑證之目錄的路徑。
- 憑證的指紋(用來查看憑證庫)。
- 憑證的主題名稱(用於在憑證存儲區中查找)。
參數屬性
| 類型: | |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 Cmdlet 支援一般參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。