Created
July 12, 2021 08:20
-
-
Save FrankSpierings/598f50863103e5ec9b82e6960614399d to your computer and use it in GitHub Desktop.
Read file, encrypt and base64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$filepath = "/etc/passwd" | |
$fs = New-Object IO.FileStream($filepath, [System.IO.FileMode]::Open); | |
$ms = New-Object System.IO.MemoryStream; | |
$aes = [System.Security.Cryptography.Aes]::Create(); | |
$aes.keysize = 128; | |
Write-Host "Key: " (($aes.Key |% ToString X2) -join ''); | |
Write-Host "IV: " (($aes.IV |% ToString X2) -join ''); | |
Write-Host "Mode: " $aes.mode | |
$cs = New-Object System.Security.Cryptography.CryptoStream($ms, $aes.CreateEncryptor(), [System.Security.Cryptography.CryptoStreamMode]::Write); | |
$fs.CopyTo($cs); | |
$cs.Close(); | |
$fs.Close(); | |
$ms.Close(); | |
$enc = $ms.ToArray(); | |
[System.Convert]::ToBase64String($enc); | |
# Decryption | |
$ms = New-Object System.IO.MemoryStream; | |
$cs = New-Object System.Security.Cryptography.CryptoStream($ms, $aes.CreateDecryptor(), [System.Security.Cryptography.CryptoStreamMode]::Write); | |
$cs.Write($enc, 0, $enc.Length); | |
[System.Text.Encoding]::ASCII.GetString($ms.ToArray()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment