Advertisement
Guest User

Untitled

a guest
May 25th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. # define and encode test data
  2. $TestString = 'This is a test. A short test for encoding and padding.'
  3. $Encoded = [Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($TestString))
  4.  
  5. # insert random '='
  6. $Length = $Encoded.Length
  7. $RandomChar = 1..($Length - 3) | Get-Random
  8. $Encoded = $Encoded.Insert($RandomChar,'=')
  9.  
  10. # strip out '='
  11. $Stripped = $Encoded.Replace('=','')
  12.  
  13. # append appropriate padding
  14. $ModulusValue = ($Stripped.length % 4)
  15. Switch ($ModulusValue) {
  16. '0' {$Padded = $Stripped}
  17. '1' {$Padded = $Stripped.Substring(0,$Stripped.Length - 1)}
  18. '2' {$Padded = $Stripped + ('=' * (4 - $ModulusValue))}
  19. '3' {$Padded = $Stripped + ('=' * (4 - $ModulusValue))}
  20. }
  21.  
  22. # base64 decode and output
  23. $Decoded = [Text.Encoding]::UTF8.GetString([Convert]::FromBase64String($Padded))
  24. Write-Output $Decoded
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement