您现在的位置是:网站首页> 编程资料编程资料
Powershell实现加密解密文本文件方法实例_PowerShell_
2023-05-26
332人已围观
简介 Powershell实现加密解密文本文件方法实例_PowerShell_
适用于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中使用正则和ValidateSet验证参数合法性_PowerShell_
- PowerShell实现批量重命名文件_PowerShell_
- Powershell中定义常量的方法_PowerShell_
- Powershell实现获取电脑序列号功能脚本分享_PowerShell_
- PowerShell中读取多行文本示例_PowerShell_
- PowerShell复制命令行历史命令方法_PowerShell_
- Powershell脚本中使用条件断点实例_PowerShell_
- PowerShell ISE中代码转换大小写的技巧_PowerShell_
- PowerShell中使用PrintManagement管理打印机示例_PowerShell_
- PowerShell查询和删除打印任务操作代码实例_PowerShell_
