![](/img/trans.png)
[英]What is the proper way to encrypt an XmlTextWriter and serialize it to a file?
[英]Is this a proper way to AES encrypt larger file in powershell?
我首先尝试使用[IO.File]::ReadAllBytes
将文件内容加载到某个变量中
但这需要大量 RAM,而且速度非常慢。
所以这就是我所拥有的:
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()
有用。 但是我想知道这是否应该是这样做的。 我不需要在文件开头添加 IV,还是使用加密器自动发生?
感谢您提前回复。
是的,使用流和CopyTo
。 是的,您可能应该在 IV 前加上前缀,不,它不会自动执行此操作。
请注意,您提供机密性,但没有真实性/完整性。 不过,这对于加密文件可能很好。
您已经使用Aes.Create()
并指出了操作和填充的确切模式,这是应该的。
请注意,这不是安全审查。 不考虑原始文件的破坏或加密可执行文件的用例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.