Created
December 18, 2018 15:39
-
-
Save vp777/fa1210abb60a514404dcfe7b66ea9461 to your computer and use it in GitHub Desktop.
Functions to support base64, xor and splitting/combining of files with progress bar.
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
#example: "file.to.be.(en|de)crypted" | enxor -Secret ΣΕΚΡΕΤ | |
Function enxor { | |
Param ( | |
[Parameter(Mandatory=$True,ValueFromPipeline=$True, ParameterSetName="p1", position=0)] | |
[System.IO.FileSystemInfo]$File, | |
[Parameter(Mandatory=$True,ValueFromPipeline=$True, ParameterSetName="p2", position=0)] | |
[string]$FilePath, | |
[int]$Batch = 102400, | |
[string]$Secret = "suprasucra" | |
) | |
BEGIN { | |
if ($PSVersionTable.PSVersion.Major -gt 2) { | |
if ($Secret -like "*,*"){ | |
$SecretBA = [byte[]]($Secret -split "," | ?{ $_ }) | |
} else { | |
$enc = [system.Text.Encoding]::UTF8 | |
$SecretBA = $enc.GetBytes($Secret) | |
} | |
} | |
} | |
Process { | |
#Powershell v2 doesn't process the begin block as expected | |
if ($PSVersionTable.PSVersion.Major -eq 2) { | |
if ($Secret -like "*,*"){ | |
$SecretBA = [byte[]]($Secret -split "," | ?{ $_ }) | |
} else { | |
$enc = [system.Text.Encoding]::UTF8 | |
$SecretBA = $enc.GetBytes($Secret) | |
} | |
} | |
if ($PsCmdlet.ParameterSetName -eq "p1") { | |
$FilePath = $File.FullName | |
} | |
$infile = (Resolve-Path "$FilePath").Path | |
$outfile = "${infile}.enc" | |
$length = (Get-Item "$infile").length | |
$buf = new-object byte[] $Batch | |
$infs = new-object IO.FileStream($infile, [IO.FileMode]::Open) | |
$reader = new-object IO.BinaryReader($infs) | |
$outfs = new-object IO.FileStream($outfile, [IO.FileMode]::Create, [IO.FileAccess]::Write) | |
$writer = new-object IO.BinaryWriter($outfs) | |
$n=0 | |
while ( $n -lt $length ){ | |
$cread=$reader.Read($buf, 0, $Batch) | |
For ($i=0; $i -lt $cread; $i++) { | |
$buf[$i]=$buf[$i] -bxor $SecretBA[$i%$SecretBA.Count] | |
} | |
$writer.Write($buf, 0, $cread) | |
$n+=$cread | |
Write-Progress -Activity "Processing file ${infile}" -Status "Currently completed ${n}/${length}" -PercentComplete ([int]($n/$length*100)) | |
} | |
Write-Progress -Activity "Processing file ${infile}" -Status "Completed" -Completed | |
$reader.Close() | |
$writer.Close() | |
} | |
} | |
Function b64 { | |
Param ( | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True, ParameterSetName="p1", position=0)] | |
[System.IO.FileSystemInfo]$File, | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True, ParameterSetName="p2", position=0)] | |
[string]$FilePath | |
) | |
Process { | |
if ($PsCmdlet.ParameterSetName -eq "p1") { | |
$FilePath = $File.FullName | |
} | |
$infile = (Resolve-Path "$FilePath").Path | |
$outfile = "${infile}.b64" | |
$bytes = [System.IO.File]::ReadAllBytes($infile) | |
[Convert]::ToBase64String($bytes) | Set-Content $outfile | |
} | |
} | |
Function d64 { | |
Param ( | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True, ParameterSetName="p1", position=0)] | |
[System.IO.FileSystemInfo]$File, | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True, ParameterSetName="p2", position=0)] | |
[string]$FilePath | |
) | |
Process { | |
if ($PsCmdlet.ParameterSetName -eq "p1") { | |
$FilePath = $File.FullName | |
} | |
$infile = (Resolve-Path "$FilePath").Path | |
$outfile = "${infile}.d64" | |
$text = [System.IO.File]::ReadAllText($infile) | |
$bytes = [System.Convert]::FromBase64String($text) | |
[System.IO.File]::WriteAllBytes($outfile, $bytes) | |
} | |
} | |
Function split { | |
Param ( | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True)] | |
[string]$Name, | |
[int]$ChunkSize = 8324000 | |
) | |
Process { | |
$infile = (Resolve-Path "$Name").Path | |
$_outfile = "${infile}.{0}" | |
$length = (Get-Item "$infile").length | |
$buf = new-object byte[] $ChunkSize | |
$infs = new-object IO.FileStream($infile, [IO.FileMode]::Open) | |
$reader = new-object IO.BinaryReader($infs) | |
$n=0 | |
$cnum=1 | |
while ( $n -lt $length ){ | |
$outfile = $_outfile -f $cnum | |
$outfs = new-object IO.FileStream($outfile, [IO.FileMode]::Create, [IO.FileAccess]::Write) | |
$writer = new-object IO.BinaryWriter($outfs) | |
$cread=$reader.Read($buf, 0, $ChunkSize) | |
$writer.Write($buf, 0, $cread) | |
$writer.Close() | |
$n+=$cread | |
$cnum+=1 | |
Write-Progress -Activity "Processing file ${infile}" -Status "Currently completed ${n}/${length}" -PercentComplete ([int]($n/$length*100)) | |
} | |
Write-Progress -Activity "Processing file ${infile}" -Status "Completed" -Completed | |
$reader.Close() | |
} | |
} | |
Function combine { | |
Param ( | |
[Parameter(Mandatory=$True,ValueFromPipelineByPropertyName=$True, ValueFromPipeline=$True)] | |
[string]$Name, | |
[int]$ChunkSize = 8324000 | |
) | |
Process { | |
$infile = (Resolve-Path "$Name").Path | |
$ext = [System.IO.Path]::GetExtension($infile) | |
$filename = $infile.Substring(0, $infile.Length-$ext.Length) | |
$outfile = "${filename}.comb" | |
$_cfile = "${filename}.{0}" | |
$outfs = new-object IO.FileStream($outfile, [IO.FileMode]::Create, [IO.FileAccess]::Write) | |
$writer = new-object IO.BinaryWriter($outfs) | |
$cnum=1 | |
$cfile=$_cfile -f $cnum | |
while ( [System.IO.File]::Exists($cfile) ){ | |
$bytes = [System.IO.File]::ReadAllBytes($cfile) | |
$writer.Write($bytes, 0, $bytes.Length) | |
$cnum+=1 | |
$cfile=$_cfile -f $cnum | |
Write-Progress -Activity "Generating file ${outfile}" -Status "${cfile}" -PercentComplete 1 | |
} | |
Write-Progress -Activity "Generating file ${outfile}" -Status "Completed" -Completed | |
$writer.Close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment