ConvertTo-SecureString
將加密標準字串轉換成安全字串。 它也可以將純文字轉換成安全字串。 它可以搭配 ConvertFrom-SecureString 與 Read-Host 使用。
Syntax
ConvertTo-SecureString
[-String] <String>
[[-SecureKey] <SecureString>]
[<CommonParameters>]
ConvertTo-SecureString
[-String] <String>
[-AsPlainText]
[-Force]
[<CommonParameters>]
ConvertTo-SecureString
[-String] <String>
[-Key <Byte[]>]
[<CommonParameters>]
Description
ConvertTo-SecureString Cmdlet 會將加密的標準字串轉換成安全字串。 它也可以將純文字轉換成安全字串。 它可以搭配 ConvertFrom-SecureString 與 Read-Host 使用。 Cmdlet 建立的安全字串可搭配需要 SecureString 類型之參數的 Cmdlet 或函式使用。 使用 ConvertFrom-SecureString Cmdlet 可以將安全字串轉換回加密標準字串。 這可讓它儲存在檔案中以供日後使用。
如果使用指定的密鑰以 ConvertFrom-SecureString 加密轉換的標準字串,則必須提供該相同金鑰做為 ConvertTo-SecureString Cmdlet 之 Key 或 SecureKey 參數的值。
範例
範例 1:將安全字串轉換為加密字串
PS C:\> $Secure = Read-Host -AsSecureString
PS C:\> $Secure
System.Security.SecureString PS C:\> $Encrypted = ConvertFrom-SecureString -SecureString $Secure
PS C:\> $Encrypted
01000000d08c9ddf0115d1118c7a00c04fc297eb010000001a114d45b8dd3f4aa11ad7c0abdae9800000000002000000000003660000a8000000100000005df63cea84bfb7d70bd6842e7
efa79820000000004800000a000000010000000f10cd0f4a99a8d5814d94e0687d7430b100000008bf11f1960158405b2779613e9352c6d14000000e6b7bf46a9d485ff211b9b2a2df3bd
6eb67aae41 PS C:\> $Secure2 = ConvertTo-SecureString -String $Encrypted
PS C:\> $Secure2
System.Security.SecureString
此範例顯示如何從使用者輸入建立安全字串、將安全字串轉換成加密的標準字串,然後再將加密的標準字串轉換回安全字串。
第一個命令會使用 Read-Host Cmdlet 的 AsSecureString 參數來建立安全字串。 輸入命令之後,您輸入的任何字元都會轉換成安全字串,然後儲存在$Secure變數中。
第二個命令會顯示$Secure變數的內容。 因為$Secure變數包含安全字串,所以 Windows PowerShell 只會顯示 System.Security.SecureString 類型。
第三個命令會使用 ConvertFrom-SecureString Cmdlet,將 $Secure 變數中的安全字串轉換成加密的標準字串。 它會將結果儲存在$Encrypted變數中。
第四個命令會在 $Encrypted 變數的值中顯示加密字串。
第五個命令會使用 ConvertTo-SecureString Cmdlet,將$Encrypted變數中的加密標準字串轉換成安全字串。 它會將結果儲存在 $Secure 2 變數中。 第六個命令會顯示 $Secure 2 變數的值。 SecureString 類型表示命令已成功。
範例 2:從檔案中的加密字串 Create 安全字串
PS C:\> $Secure = Read-Host -AsSecureString
PS C:\> $Encrypted = ConvertFrom-SecureString -SecureString $Secure -Key (1..16)
PS C:\> $Encrypted | Set-Content Encrypted.txt
PS C:\> $Secure2 = Get-Content Encrypted.txt | ConvertTo-SecureString -Key (1..16)
此範例顯示如何從儲存在檔案中之加密標準字串建立安全字串。
第一個命令會使用 Read-Host Cmdlet 的 AsSecureString 參數來建立安全字串。 輸入命令之後,您輸入的任何字元都會轉換成安全字串,然後儲存在$Secure變數中。
第二個命令會使用 ConvertFrom-SecureString Cmdlet,使用指定的金鑰,將 $Secure 變數中的安全字串轉換成加密的標準字串。 內容會儲存在$Encrypted變數中。
第三個命令使用管線運算子 (|) 將$Encrypted變數的值傳送至 Set-Content Cmdlet,這會將值儲存在 Encrypted.txt 檔案中。
第四個命令使用 Get-Content Cmdlet 取得 Encrypted.txt 檔案中加密的標準字串。 此命令會使用管線運算符將加密字串傳送至 ConvertTo-SecureString Cmdlet,以使用指定的密鑰將它轉換成安全字串。 結果會儲存在 $Secure 2 變數中。
範例 3:將純文字字串轉換為安全字串串
PS C:\> $Secure_String_Pwd = ConvertTo-SecureString "P@ssW0rD!" -AsPlainText -Force
此命令會轉換純文字字串P@ssW0rD! 放入安全字串,並將結果儲存在 $Secure_String_Pwd 變數中。 若要使用 AsPlainText 參數, Force 參數也必須包含在 命令中。
參數
-AsPlainText
指定要轉換成安全字串的純文字字串。 安全字串 Cmdlet 有助於保護機密文字。 為保有隱私,文字會加密,而且在使用後會從電腦記憶體中刪除。 如果您使用此參數提供純文字做為輸入,系統就無法以此種方式保護保護該輸入。 若要使用此參數,您也必須指定 Force 參數。
Type: | SwitchParameter |
Position: | 1 |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Force
確認您瞭解使用 AsPlainText 參數的影響,但仍想要使用它。
Type: | SwitchParameter |
Position: | 2 |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Key
指定將安全字串轉換成加密標準字串時要使用的加密金鑰。 有效的金鑰長度為 16、24 及 32 個位元組。
Type: | Byte[] |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-SecureKey
指定將安全字串轉換成加密標準字串時要使用的加密金鑰。 金鑰需以安全字串的格式提供。 安全字串在被當作金鑰使用之前,會先轉換成位元組陣列。 有效的金鑰長度為 16、24 及 32 個位元組。
Type: | SecureString |
Position: | 1 |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-String
指定要轉換成安全字串的字串。
Type: | String |
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
輸入
您可以使用管線將標準加密字串傳送至 ConvertTo-SecureString。
輸出
ConvertTo-SecureString 會傳回 SecureString 物件。