-
Notifications
You must be signed in to change notification settings - Fork 91
/
Convert-Base64.ps1
235 lines (138 loc) · 6.57 KB
/
Convert-Base64.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
Function Convert-Base64 {
<#
.SYNOPSIS
This cmdlet is used to Encode or Decode Base64 strings.
.DESCRIPTION
Convert a string of text to or from Base64 format. Pipeline input is accepted in string format. Use the switch parameters Encode or Decode to define which action you wish to perform on your string
.PARAMETER Value
Defines the string to be encoded or decoded with base64.
.PARAMETER Encode
This switch parameter is used to tell the cmdlet to encode the base64 string
.PARAMETER Decode
This switch parameter is used to tell the cmdlet to decode the base64 string
.PARAMETER TextEncoding
This parameter is used to define the type of Unicode Character encoding to convert with Base64. This value you can be ASCII, BigEndianUnicode, Default, Unicode, UTF32, UTF7, or UTF8. The default value is UTF8
.EXAMPLE
Convert-Base64 -Value 'Hello World!'' -Encode
# This example encodes "Hello World into Base64 format.
.EXAMPLE
Convert-Base64 -Value 'SGVsbG8gV29ybGQh' -Decode -TextEncoding ASCII
# This example decodes Base64 to a string in ASCII format
.NOTES
Author: Robert H. Osborne
Alias: tobor
Contact: rosborne@osbornepro.com
.LINK
https://powershell.org/2020/11/writing-your-own-powershell-functions-cmdlets/
https://osbornepro.com
https://writeups.osbornepro.com
https://btpssecpack.osbornepro.com
https://github.com/tobor88
https://gitlab.com/tobor88
https://www.powershellgallery.com/profiles/tobor
https://www.linkedin.com/in/roberthosborne/
https://www.credly.com/users/roberthosborne/badges
https://www.hackthebox.eu/profile/52286
.INPUTS
System.String, -Value accepts strings from pipeline.
.OUTPUTS
System.String
#>
[CmdletBinding(DefaultParameterSetName='Encode')]
param(
[Parameter(
Mandatory=$True,
Position=0,
ValueFromPipeline=$True,
HelpMessage="Enter a string you wish to encode or decode using Base64. Example: Hello World!")] # End Parameter
[String]$Value,
[Parameter(
ParameterSetName='Encode',
Mandatory=$True)]
[Switch][Bool]$Encode,
[Parameter(
ParameterSetName='Decode',
Mandatory=$True)]
[Switch][Bool]$Decode,
[Parameter(
Mandatory=$False,
ValueFromPipeline=$False)] # End Parameter
[ValidateSet('ASCII', 'BigEndianUnicode', 'Default', 'Unicode', 'UTF32', 'UTF7', 'UTF8')]
[String]$TextEncoding = 'UTF8'
) # End param
PROCESS {
Switch ($PSCmdlet.ParameterSetName) {
'Encode' {
Switch ($TextEncoding) {
'ASCII' {$StringValue = [System.Text.Encoding]::ASCII.GetBytes("$Value")}
'BigEndianUnicode' {$StringValue = [System.Text.Encoding]::BigEndianUnicode.GetBytes("$Value")}
'Default' {$StringValue = [System.Text.Encoding]::Default.GetBytes("$Value")}
'Unicode' {$StringValue = [System.Text.Encoding]::Unicode.GetBytes("$Value")}
'UTF32' {$StringValue = [System.Text.Encoding]::UTF32.GetBytes("$Value")}
'UTF7' {$StringValue = [System.Text.Encoding]::UTF7.GetBytes("$Value")}
'UTF8' {$StringValue = [System.Text.Encoding]::UTF8.GetBytes("$Value")}
} # End Switch
Try {
[System.Convert]::ToBase64String($StringValue)
} Catch {
Throw "String could not be converted to Base64. The value entered is below. `n$Value"
$Error[0]
} # End Catch
} # End Switch Encode
'Decode' {
$EncodedValue = [System.Convert]::FromBase64String("$Value")
Switch ($TextEncoding) {
'ASCII' {
Try {
[System.Text.Encoding]::ASCII.GetString($EncodedValue)
} Catch {
Throw "[x] Base64 entered was not in a correct format. The value received is below. `n$Value"
} # End Try Catch
} # End Switch ASCII
'BigEndianUnicode' {
Try {
[System.Text.Encoding]::BigEndianUnicode.GetString($EncodedValue)
} Catch {
Throw "[x] Base64 entered was not in a correct format. The value received is below. `n$Value"
} # End Try Catch
} # End Switch BigEndianUnicode
'Default' {
Try {
[System.Text.Encoding]::Default.GetString($EncodedValue)
} Catch {
Throw "[x] Base64 entered was not in a correct format. The value received is below. `n$Value"
} # End Try Catch
} # End Switch Default
'Unicode' {
Try {
[System.Text.Encoding]::Unicode.GetString($EncodedValue)
} Catch {
Throw "[x] Base64 entered was not in a correct format. The value received is below. `n$Value"
} # End Try Catch
} # End Switch Unicode
'UTF32' {
Try {
[System.Text.Encoding]::UTF32.GetString($EncodedValue)
} Catch {
Throw "[x] Base64 entered was not in a correct format. The value received is below. `n$Value"
} # End Try Catch
} # End Switch UTF32
'UTF7' {
Try {
[System.Text.Encoding]::UTF7.GetString($EncodedValue)
} Catch {
Throw "[x] Base64 entered was not in a correct format. The value received is below. `n$Value"
} # End Catch
} # End Swithc UTF7
'UTF8' {
Try {
[System.Text.Encoding]::UTF8.GetString($EncodedValue)
} Catch {
Throw "[x] Base64 entered was not in a correct format. The value received is below. `n$Value"
} # End Catch
} # End Switch UTF8
} # End Switch
} # End Switch Decode
} # End Switch
} # End PROCESS
} # End Function Convert-Base64