欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Powershell實(shí)現(xiàn)加密解密文本文件方法實(shí)例

 更新時(shí)間:2015年04月11日 09:28:53   投稿:junjie  
這篇文章主要介紹了Powershell實(shí)現(xiàn)加密解密文本文件方法實(shí)例,本文直接給出加密和解密代碼實(shí)例,需要的朋友可以參考下

適用于Powershell3.0及以后版本。
假設(shè)你需要給文件加密,下面教你如何給自己的文件加密:

$Path = "$env:temp\secret.txt"
$Secret = 'Hello World!'
$Passphrase = 'Some secret key'
 
$key = [Byte[]]($Passphrase.PadRight(24).Substring(0,24).ToCharArray())
 
$Secret |
 ConvertTo-SecureString -AsPlainText -Force |
 ConvertFrom-SecureString -Key $key |
 Out-File -FilePath $Path
 
notepad $Path

當(dāng)你需要解密出里面的內(nèi)容,這時(shí)就需要最初的密碼:

$Passphrase = Read-Host 'Enter the secret pass phrase'
 
$Path = "$env:temp\secret.txt"
 
$key = [Byte[]]($Passphrase.PadRight(24).Substring(0,24).ToCharArray())
 
try
{
 $decryptedTextSecureString = Get-Content -Path $Path -Raw |
 ConvertTo-SecureString -Key $key -ErrorAction Stop
 
 $cred = New-Object -TypeName System.Management.Automation.PSCredential('dummy', $decryptedTextSecureString)
 $decryptedText = $cred.GetNetworkCredential().Password
}
catch
{
 $decryptedText = '(wrong key)'
}
"The decrypted secret text: $decryptedText"

相關(guān)文章

最新評(píng)論