Skip to content

Instantly share code, notes, and snippets.

@FrankSpierings
Created July 12, 2021 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FrankSpierings/598f50863103e5ec9b82e6960614399d to your computer and use it in GitHub Desktop.
Save FrankSpierings/598f50863103e5ec9b82e6960614399d to your computer and use it in GitHub Desktop.
Read file, encrypt and base64
$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