简体   繁体   English

这是AES在powershell中加密较大文件的正确方法吗?

[英]Is this a proper way to AES encrypt larger file in powershell?

I've tried first by loading the file content into some variable with [IO.File]::ReadAllBytes我首先尝试使用[IO.File]::ReadAllBytes将文件内容加载到某个变量中

But that takes a lot of RAM and it's painfully slow.但这需要大量 RAM,而且速度非常慢。

So here's what I've got:所以这就是我所拥有的:

ErrorActionPreference = "Stop"
$AES = [System.Security.Cryptography.AES]::Create()
$AES.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$AES.Mode = [System.Security.Cryptography.CipherMode]::CBC
$AES.BlockSize = 128
$AES.KeySize = 256
$AES.GenerateKey()
$AES.GenerateIV()
$Encryptor = $AES.CreateEncryptor()

$File = Get-Item -Path "C:\Myfile.exe"

$InputStream = New-Object System.IO.FileStream($File, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read)
$OutputStream = New-Object System.IO.FileStream((($File.FullName) + ".AES"), [System.IO.FileMode]::Create, [System.IO.FileAccess]::Write)

$CryptoStream = New-Object System.Security.Cryptography.CryptoStream($OutputStream, $Encryptor, [System.Security.Cryptography.CryptoStreamMode]::Write)

$InputStream.CopyTo($CryptoStream)
$CryptoStream.Dispose()

$AES.Dispose()

It works.有用。 However I was wondering if this is how it's supposed to be done.但是我想知道这是否应该是这样做的。 Do I not need to prepend IV to the beginning of the file, or does it happen automatically with the Encryptor?我不需要在文件开头添加 IV,还是使用加密器自动发生?

Thanks for any responses in advance.感谢您提前回复。

Yes, use streams and CopyTo .是的,使用流和CopyTo Yes, you should probably prefix the IV, no it doesn't do this automatically.是的,您可能应该在 IV 前加上前缀,不,它不会自动执行此操作。

Note that you provide confidentiality, but no authenticity / integrity.请注意,您提供机密性,但没有真实性/完整性。 This could be fine for encrypting files though.不过,这对于加密文件可能很好。

You have used Aes.Create() and indicated the exact mode of operation & padding, which is as it should be.您已经使用Aes.Create()并指出了操作和填充的确切模式,这是应该的。


Note that this is not a security review.请注意,这不是安全审查。 The destruction of the original file or the use case for encrypting an executable is not considered.不考虑原始文件的破坏或加密可执行文件的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM