![](/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.