Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # define and encode test data
- $TestString = 'This is a test. A short test for encoding and padding.'
- $Encoded = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($TestString))
- # insert random '='
- $Length = $Encoded.Length
- $RandomChar = 1..($Length - 3) | Get-Random
- $Encoded = $Encoded.Insert($RandomChar,'=')
- # strip out '='
- $Stripped = $Encoded.Replace('=','')
- # append appropriate padding
- $ModulusValue = ($Stripped.length % 4)
- Switch ($ModulusValue) {
- '0' {$Padded = $Stripped}
- '1' {$Padded = $Stripped.Substring(0,$Stripped.Length - 1)}
- '2' {$Padded = $Stripped + ('=' * (4 - $ModulusValue))}
- '3' {$Padded = $Stripped + ('=' * (4 - $ModulusValue))}
- }
- # base64 decode and output
- $Decoded = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($Padded))
- Write-Output $Decoded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement