將標準加密字串轉換為安全性字串。也可以將純文字轉換成安全性字串。這是搭配 ConvertFrom-SecureStringRead-Host 使用的。

語法

ConvertTo-SecureString [-Key <Byte[]>] [-String] <string> [<CommonParameters>]

ConvertTo-SecureString [-AsPlainText] [-Force] [-String] <string> [<CommonParameters>]

ConvertTo-SecureString [[-SecureKey] <SecureString>] [-String] <string> [<CommonParameters>]

描述

ConvertTo-SecureString Cmdlet 會將標準加密字串轉換成安全字串。也可以將純文字轉換成安全性字串。這是搭配 ConvertFrom-SecureStringRead-Host 使用的。cmdlet 建立的安全性字串可以和需要 SecureString 類型參數的 cmdlet 或函數搭配使用。您可以使用 ConvertFrom-SecureString Cmdlet,將安全字串轉換回加密的標準字串。如此即可將它儲存在檔案中,以供日後使用。

如果即將轉換的標準字串是透過使用某個指定金鑰的 ConvertFrom-SecureString 所加密,則必須提供同一個金鑰做為 ConvertTo-SecureString Cmdlet 的 Key 或 SecureKey 參數值。

參數

-AsPlainText

指定要轉換成安全性字串的純文字字串。安全性字串 cmdlet 可協助保護機密文字。文字會經過加密以保障隱私,且使用後將從電腦記憶體刪除。若使用此參數提供純文字做為輸入,系統無法保護輸入的內容。使用此參數時,必須一併指定 Force 參數。

必要?

false

位置?

2

預設值

接受管線輸入?

false

接受萬用字元?

false

-Force

確認您已了解到使用 AsPlainText 參數的隱憂且仍想使用。

必要?

false

位置?

3

預設值

接受管線輸入?

false

接受萬用字元?

false

-Key <Byte[]>

指定將安全性字串轉換成標準加密字串時所使用的加密金鑰。有效的金鑰長度為 16、24 和 32 位元組。

必要?

false

位置?

named

預設值

接受管線輸入?

false

接受萬用字元?

false

-SecureKey <SecureString>

指定將安全性字串轉換成標準加密字串時所使用的加密金鑰。提供的金鑰必須是安全性字串的格式。安全性字串會先轉換成位元組陣列再當作金鑰使用。有效的金鑰長度為 16、24 和 32 位元組。

必要?

false

位置?

2

預設值

接受管線輸入?

false

接受萬用字元?

false

-String <string>

指定要轉換成安全性字串的字串。

必要?

true

位置?

1

預設值

接受管線輸入?

true (ByValue)

接受萬用字元?

false

<CommonParameters>

這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.

輸入和輸出

輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。

輸入

System.String

您可經由管道將標準加密字串輸出至 ConvertTo-SecureString。

輸出

System.Security.SecureString

ConvertTo-SecureString 會傳回 SecureString 物件。

範例 1

C:\PS>$secure = read-host -assecurestring

C:\PS> $secure
System.Security.SecureString

C:\PS> $encrypted = convertfrom-securestring -securestring $secure
C:\PS> $encrypted
01000000d08c9ddf0115d1118c7a00c04fc297eb010000001a114d45b8dd3f4aa11ad7c0abdae9800000000002000000000003660000a8000000100000005df63cea84bfb7d70bd6842e7
efa79820000000004800000a000000010000000f10cd0f4a99a8d5814d94e0687d7430b100000008bf11f1960158405b2779613e9352c6d14000000e6b7bf46a9d485ff211b9b2a2df3bd
6eb67aae41

C:\PS> $secure2 = convertto-securestring -string $encrypted
C:\PS> $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 變數中的標準加密字串轉換回安全字串。然後,它會將結果儲存在 $secure2 變數中。第六個命令會顯示 $secure2 變數的值。SecureString 型別表示命令執行成功。






範例 2

C:\PS>$secure = read-host -assecurestring

C:\PS> $encrypted = convertfrom-securestring -secureString $secure -key (1..16)

C:\PS> $encrypted | set-content encrypted.txt

C:\PS> $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,以便使用指定的金鑰將它轉換成安全字串。然後,這些結果會儲存在 $secure2 變數中。






範例 3

C:\PS>$secure_string_pwd = convertto-securestring "P@ssW0rD!" -asplaintext -force

描述
-----------
這個命令會將純文字字串 "P@ssW0rD!" 轉換成安全字串,並將結果儲存在 $secure_string_pwd 變數中。若要使用 AsPlainText 參數,此命令也必須包含 Force 參數。






請參閱




目錄