-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEncrypt-AES.ps1
More file actions
35 lines (25 loc) · 1.09 KB
/
Encrypt-AES.ps1
File metadata and controls
35 lines (25 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function Encrypt-AES {
param (
[Parameter(Mandatory=$true, Position=0)]
[string]$plainTextFile
)
if (-not (Test-Path $plainTextFile)) {
Write-Error "File not found: $plainTextFile"
return
}
$encryptedFile = "$plainTextFile.aes"
$key = New-Object Byte[] 32
[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($key)
$aes = New-Object Security.Cryptography.AesCryptoServiceProvider
$aes.Key = $key
$plainTextBytes = Get-Content $plainTextFile -Encoding Byte
$encryptedBytes = $aes.CreateEncryptor().TransformFinalBlock($plainTextBytes, 0, $plainTextBytes.Length)
$encryptedBytes | Set-Content $encryptedFile -Encoding Byte
$keyPath = Join-Path (Split-Path $plainTextFile -Parent) "key.txt"
$ivPath = Join-Path (Split-Path $plainTextFile -Parent) "iv.txt"
$key | Set-Content $keyPath -Encoding Byte
$aes.IV | Set-Content $ivPath -Encoding Byte
Remove-Item -Path $plainTextFile -Force
Clear-RecycleBin -Confirm:$false
}
# Encrypt-AES "path/to/the/file"