手机
当前位置:查字典教程网 >脚本专栏 >PowerShell >Powershell实现加密解密文本文件方法实例
Powershell实现加密解密文本文件方法实例
摘要:适用于Powershell3.0及以后版本。假设你需要给文件加密,下面教你如何给自己的文件加密:$Path="$env:tempsecret...

适用于Powershell3.0及以后版本。

假设你需要给文件加密,下面教你如何给自己的文件加密:

$Path = "$env:tempsecret.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:tempsecret.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实现加密解密文本文件方法实例】相关文章:

PowerShell中实现播放WAV音频文件

Powershell中定义常量的方法

Powershell实现导入安装证书功能脚本分享

PowerShell中Get-Date对象的常用属性和方法介绍

Powershell脚本中包含文件资源的例子

Powershell学习笔记--使用正则表达式查找文件

PowerShell批量安装msi后辍软件的方法

Powershell小技巧之记录脚本的操作

PowerShell中计算时间差的方法

Windows Powershell对象转换成文本

精品推荐
分类导航