我能够AES加密文件,成功地利用这个脚本在这里,使用的是Windows 10,PowerShell的5.1版。
当我尝试在Windows 7,PowerShell v2.0上运行它时,我收到一个错误:
New-CryptographyKey:您无法在空值表达式上调用方法。
在C:\ Users \ IEUser \ Desktop \ enc.ps1:399 char:27
+ $ key = New-CryptographyKey <<<< - AsPlainText
+ CategoryInfo:NotSpecified:(:) [Write-Error],WriteErrorException
+ FullyQualifiedErrorId:Microsoft.PowerShell.Commands.WriteErrorException,New-CryptographyKey
Protect-File:无法将参数绑定到参数'KeyAsPlainText',因为
它是一个空字符串。
在C:\ Users \ IEUser \ Desktop \ enc.ps1:401 char:77
+ Protect-File -FileName“$ env:userprofile / Desktop / secret.txt”-KeyAsPlainText <<<< $ key
+ CategoryInfo:InvalidData :( :) [Protect-File],ParameterBindingValidationException
+ FullyQualifiedErrorId:ParameterArgumentValidationErrorEmptyStringNotAllowed,Protect-File
我如何使其工作?或者是否有另一个使用Powershell进行AES文件加密的交叉兼容解决方案?
编辑:
我试过@Mike Twc的解决方案,我收到了这个错误:
PS C:\Users\IEUser\Desktop> .\bouncy.ps1
Method invocation failed because [Org.BouncyCastle.Crypto.KeyGenerationParamete
rs] doesn't contain a method named 'new'.
At C:\Users\IEUser\Desktop\bouncy.ps1:8 char:65
+ $params = [Org.BouncyCastle.Crypto.KeyGenerationParameters]::new <<<< ($keyBy
tes, 256)
+ CategoryInfo : InvalidOperation: (new:String) [], RuntimeExcept
ion
+ FullyQualifiedErrorId : MethodNotFound
Exception calling "Init" with "1" argument(s): "Value cannot be null.
Parameter name: parameters"
At C:\Users\IEUser\Desktop\bouncy.ps1:9 char:16
+ $generator.Init <<<< ($params)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
urUmoWaCK3A3/28reI8WUV6goc8meBPl
PS C:\Users\IEUser\Desktop>
..
它尽管打印出钥匙。所以我尝试在前面提到的主脚本中使用它。
我将@Mike Twc的解决方案附加到主脚本:( Protect-File
并且Unprotect-File
功能保持不变)
Add-Type -path "$env:userprofile/Desktop/b.dll"
$secRandom = new-object Org.BouncyCastle.Security.SecureRandom
$keyBytes = New-Object byte[] 32
$secRandom.NextBytes($keyBytes)
$generator = [Org.BouncyCastle.Security.GeneratorUtilities]::GetKeyGenerator("AES")
$params = [Org.BouncyCastle.Crypto.KeyGenerationParameters]::new($keyBytes, 256)
$generator.Init($params)
$key = $generator.GenerateKey()
$key=Write-Host ([System.Convert]::ToBase64String($key))
echo $key
Protect-File 'C:/Users/IEUser/Desktop/secret.txt' $key -Algorithm AES
现在我收到此错误:
PS C:\Users\IEUser\Desktop> .\t.ps1
Method invocation failed because [Org.BouncyCastle.Crypto.KeyGenerationParamete
rs] doesn't contain a method named 'new'.
At C:\Users\IEUser\Desktop\t.ps1:341 char:65
+ $params = [Org.BouncyCastle.Crypto.KeyGenerationParameters]::new <<<< ($keyBy
tes, 256)
+ CategoryInfo : InvalidOperation: (new:String) [], RuntimeExcept
ion
+ FullyQualifiedErrorId : MethodNotFound
Exception calling "Init" with "1" argument(s): "Value cannot be null.
Parameter name: parameters"
At C:\Users\IEUser\Desktop\t.ps1:342 char:16
+ $generator.Init <<<< ($params)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
xwlP+m6PGVMC5Qa4z0LjNGLf+bkG2sfY
Protect-File : Exception calling "SecureStringToBSTR" with "1" argument(s): "Va
lue cannot be null.
Parameter name: s"
At C:\Users\IEUser\Desktop\t.ps1:347 char:13
+ Protect-File <<<< 'C:/Users/IEUser/Desktop/secret.txt' $key -Algorithm AES
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep
tion
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio
n,Protect-File
PS C:\Users\IEUser\Desktop>
发布于 2018-10-26 13:47:23
根据您所说的链接的代码的源信息,它不是为PowerShell v2而设计的。因此,应该预料到它失败的事实,因为如果内存服务,则直到PowerShellv3才会引入Export-ModuleMember。
Get-Command -Name Export-moduleMember
CommandType Name Version Source
----------- ---- ------- ------
Cmdlet Export-ModuleMember 3.0.0.0 Microsoft.PowerShell.Core
我在我的环境中的任何地方都没有PowerShellv2可以圣洁地检查。
那么,至于..
我如何使其工作?
..将您的PowerShell版本升级到3 - 5x。
至于......
另一个使用Powershell进行AES文件加密的交叉兼容解决方案?
PowerShell Core(PowerShell v6)是PowerShell唯一的跨平台版本。如果您指的是此模块的跨平台版本,则由作者提供或您编写它。
如果你要求通用的跨平台AES工具(甚至你指向的链接只是Windows - 所以如果你需要跨平台的Win / OSX / Linux那么这无论如何都不会工作),这真的不是PowerShell问题而是软件建议,这些问题有一个单独的委员会。软件建议StackExchange,但在使用PowerShell时可以查看PGP。
如果您所说的AES文件加密版本适用于所有版本的Windows PowerShell,则需要使用.Net命名空间。具体来说,NET的FileStream和CryptoStream类通过此脚本下载使用AES加密整个文件。
PowerShell加密示例 5可用于使用PowerShell安全加密(在大多数情况下,共享)机密数据的不同技术示例。
具体实施例3
或者使用这个模块。
附加的zip文件包含一个模块,可以使用PowerShell轻松加密数据,使其可以被任何计算机上的其他授权用户解密。它通过利用数字证书来实现这一点。
发布于 2018-10-26 14:09:04
您可以尝试使用BouncyCastle库。测试它是否适用于PSv2。要开始只生成一个密钥,看看是否有任何错误。
从这里下载最新的编译程序集(BouncyCastle.Crypto.dll):https://www.bouncycastle.org/csharp/index.html
将该DLL解压缩到任何文件夹(例如C:\ temp),右键单击它,然后选中“Unblock”
运行下面的代码段,看看是否有任何错误:
Add-Type -path "C:\temp\BouncyCastle.Crypto.dll"
$secRandom = new-object Org.BouncyCastle.Security.SecureRandom
$keyBytes = New-Object byte[] 32
$secRandom.NextBytes($keyBytes)
$generator = [Org.BouncyCastle.Security.GeneratorUtilities]::GetKeyGenerator("AES")
$params = [Org.BouncyCastle.Crypto.KeyGenerationParameters]::new($keyBytes, 256)
$generator.Init($params)
$key = $generator.GenerateKey()
Write-Host ([System.Convert]::ToBase64String($key))
https://stackoverflow.com/questions/-100008918
复制相似问题