Skip to content

Commit 34ff805

Browse files
authored
Add Base64 Encode Decode in PowerShell (#4713)
1 parent b8a259b commit 34ff805

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
function Show-Usage() {
2+
Write-Host "Usage: please provide a mode and a string to encode/decode"
3+
Exit 1
4+
}
5+
6+
function Get-Base64Encode([string]$Str) {
7+
$Bytes = [Text.Encoding]::Ascii.GetBytes($Str)
8+
[Convert]::ToBase64String($Bytes)
9+
}
10+
11+
function Get-Base64Decode([string]$Str) {
12+
$Bytes = [Convert]::FromBase64String($Str)
13+
[Text.Encoding]::Ascii.GetString($Bytes)
14+
}
15+
16+
if ($args.Length -lt 2 -or -not $args[1]) {
17+
Show-Usage
18+
}
19+
20+
$Mode = $args[0]
21+
$Str = $args[1]
22+
switch ($Mode) {
23+
"encode" {
24+
$Result = Get-Base64Encode($Str)
25+
}
26+
"decode" {
27+
try {
28+
$Result = Get-Base64Decode($Str)
29+
} catch [Management.Automation.MethodInvocationException] {
30+
Show-Usage
31+
}
32+
}
33+
default {
34+
Show-Usage
35+
}
36+
}
37+
38+
Write-Host $Result

0 commit comments

Comments
 (0)