适用于Powershell3.0及以后版本。
假设你需要给文件加密,下面教你如何给自己的文件加密:

$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

当你需要解密出里面的内容,这时就需要最初的密码:

$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"

标签:
Powershell,加密,解密文本文件

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
内蒙古资源网 Copyright www.nmgbbs.com