-
Notifications
You must be signed in to change notification settings - Fork 1
/
aes.ps1
47 lines (41 loc) · 1.63 KB
/
aes.ps1
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
36
37
38
39
40
41
42
43
44
45
46
47
Param
(
[Parameter(Mandatory = $true)]
[ValidateSet('encrypt', 'decrypt')]
[String] $method,
[Parameter(Mandatory = $true)]
[String] $key,
[Parameter(Mandatory = $true)]
[String] $infile, #Infile file
[Parameter(Mandatory=$true)]
[String] $outfile #Output File
)
$shaManaged = New-Object System.Security.Cryptography.SHA256Managed
$aesManaged = New-Object System.Security.Cryptography.AesManaged
$aesManaged.Mode = [System.Security.Cryptography.CipherMode]::CBC
$aesManaged.Padding = [System.Security.Cryptography.PaddingMode]::PKCS7
$aesManaged.BlockSize = 128
$aesManaged.KeySize = 256
$aesManaged.Key = $shaManaged.ComputeHash([System.Text.Encoding]::UTF8.GetBytes($key))
switch ($method) {
'encrypt' {
$plainBytes = Get-Content $infile -Encoding Byte -ReadCount 0
$encryptor = $aesManaged.CreateEncryptor()
$encryptedBytes = $encryptor.TransformFinalBlock($plainBytes, 0, $plainBytes.Length)
$encryptedBytes = $aesManaged.IV + $encryptedBytes
$aesManaged.Dispose()
Set-Content -NoNewline -Path $outfile -Value $encryptedBytes -Encoding Byte
(Get-Item $outfile).LastWriteTime = (Get-Item $infile).LastWriteTime
#return "File encrypted to $outfile"
}
'decrypt' {
$cipherBytes = Get-Content $infile -Encoding Byte -ReadCount 0
$aesManaged.IV = $cipherBytes[0..15]
$decryptor = $aesManaged.CreateDecryptor()
$decryptedBytes = $decryptor.TransformFinalBlock($cipherBytes, 16, $cipherBytes.Length - 16)
$aesManaged.Dispose()
Set-Content -NoNewline -Path $outfile -Value $decryptedBytes -Encoding Byte
(Get-Item $outfile).LastWriteTime = (Get-Item $infile).LastWriteTime
#return "File decrypted to $outfile"
}
}