适用于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"
原文地址:Encrypting Text Information Using Passphrase
本文链接: https://www.pstips.net/encrypting-text-information-using-passphrase.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!
想请教你一个问题:怎么用powershell将连接数据库的密码等相关信息进行加密?
这是我脚本中的一部分代码:
[void][System.Reflection.Assembly]::LoadWithPartialName(“MySql.Data”)
$connectionStr = “Server=10.0.0.1;Uid=user;Pwd=123456;database=my_test;”
$querysql = “SELECT ServerOSSFilePath FROM test WHERE ID = ‘ser’;”
$connection = New-Object MySql.Data.MySqlClient.MySqlConnection
$connection.ConnectionString = $connectionStr
$connection.Open()
怎么将$connectionStr里面的字符串进行加密啊???