-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoder64.html
280 lines (261 loc) · 8.59 KB
/
Encoder64.html
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<!DOCTYPE html>
<html>
<head>
<title>PS Encoder 64</title>
</head>
<body>
<h2 class="title">BASE 64 ENCODER FOR POWERSHELL</h2>
<h5 class="subtitle">✓ Full Unicode support, serverless, UTF-16 PowerShell compatible.</h5>
<div class="app-menu">
<a href="https://github.com/LazaroOnline/Encoder64">
<svg height="32" width="32" viewBox="0 0 16 16" style="fill:chartreuse">
<path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path>
</svg>
</a>
</div>
<table style="width:100%">
<th width="160px"></th>
<tr>
<td>Plain text:</td>
<td>
<textarea id="txtPlainText"></textarea>
<button type="button" id="btnEncode" class="center-horizontal">Encode</button>
</td>
</tr>
<tr>
<td>Encoded Text Utf-16:</td>
<td><input type="text" id="txtEncodedTextUtf16" readonly/></td>
</tr>
<tr>
<td>Encoded Text Utf-8:</td>
<td><input type="text" id="txtEncodedTextUtf8" readonly/></td>
</tr>
<tr class="txt-console">
<td>PS Encode:</td>
<td><input type="text" id="txtPSCommandEncode" readonly/></td>
</tr>
<tr class="txt-console">
<td>PS Decode:</td>
<td><input type="text" id="txtPSCommandDecode" readonly/></td>
<tr class="txt-console">
<td>CMD Run:</td>
<td><input type="text" id="txtPSCommandRun" readonly/></td>
</tr>
<tr height="30"></tr>
<tr>
<td>Encoded text:</td>
<td>
<input type="text" id="txtEncodedText" />
<button type="button" id="btnDecode" class="center-horizontal">Decode</button>
</td>
</tr>
<tr>
<td>Decoded Text as Utf-16:</td>
<td><textarea type="text" id="txtDecodedTextUtf16" readonly></textarea></td>
</tr>
<tr>
<td>Decoded Text as Utf-8:</td>
<td><textarea type="text" id="txtDecodedTextUtf8" readonly></textarea></td>
</tr>
</table>
</body>
</html>
<style>
/* Dark theme */
body,
input,
button,
textarea {
font-family: monospace;
background-color: #111;
color: chartreuse;
}
button,
input,
textarea {
background-color: #222;
}
input,
textarea {
border: 0;
}
/* Layout */
.title, .subtitle {
margin: 0;
margin-right: 30px;
}
.app-menu {
position: absolute;
right: 5px;
top: 5px;
}
#txtPlainText,
#txtEncodedText,
#txtEncodedTextUtf16,
#txtEncodedTextUtf8,
#txtDecodedTextUtf16,
#txtDecodedTextUtf8,
#txtPSCommandEncode,
#txtPSCommandDecode,
#txtPSCommandRun
{
width: 100%;
}
#btnEncode,
#btnDecode {
width: 300px;
}
.txt-console textarea,
.txt-console input {
font-family: monospace !important;
color: gray !important;
max-width: 100%;
}
.txt-error {
background-color: rgba(255, 0, 0, 0.15);
}
.center-horizontal {
display: block;
margin: 0 auto;
}
</style>
<script>
// ------------------------------------------------------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------------------------------------------------------
"use strict";
/*\
|*| Base64 / binary data / UTF-8 strings utilities (#1)
|*| https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#Solution_1_%E2%80%93_JavaScript's_UTF-16_%3E_base64
|*| Author: madmurphy
\*/
/* Array of bytes to base64 string decoding */
function b64ToUint6 (nChr) {
return nChr > 64 && nChr < 91 ?
nChr - 65
: nChr > 96 && nChr < 123 ?
nChr - 71
: nChr > 47 && nChr < 58 ?
nChr + 4
: nChr === 43 ?
62
: nChr === 47 ?
63
:
0;
}
function base64DecToArr (sBase64, nBlockSize) {
var sB64Enc = sBase64.replace(/[^A-Za-z0-9\+\/]/g, ""), nInLen = sB64Enc.length
var nOutLen = nBlockSize ? Math.ceil((nInLen * 3 + 1 >>> 2) / nBlockSize) * nBlockSize : nInLen * 3 + 1 >>> 2, aBytes = new Uint8Array(nOutLen);
for (var nMod3, nMod4, nUint24 = 0, nOutIdx = 0, nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 18 - 6 * nMod4;
if (nMod4 === 3 || nInLen - nInIdx === 1) {
for (nMod3 = 0; nMod3 < 3 && nOutIdx < nOutLen; nMod3++, nOutIdx++) {
aBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
}
nUint24 = 0;
}
}
return aBytes;
}
/* Base64 string to array encoding */
function uint6ToB64 (nUint6) {
return nUint6 < 26 ?
nUint6 + 65
: nUint6 < 52 ?
nUint6 + 71
: nUint6 < 62 ?
nUint6 - 4
: nUint6 === 62 ?
43
: nUint6 === 63 ?
47
:
65;
}
function base64EncArr (aBytes) {
var eqLen = (3 - (aBytes.length % 3)) % 3, sB64Enc = "";
for (var nMod3, nLen = aBytes.length, nUint24 = 0, nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
/* Uncomment the following line in order to split the output in lines 76-character long: */
/*
if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) { sB64Enc += "\r\n"; }
*/
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCharCode(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
nUint24 = 0;
}
}
return eqLen === 0 ?
sB64Enc :
sB64Enc.substring(0, sB64Enc.length - eqLen) + (eqLen === 1 ? "=" : "==");
}
</script>
<script>
function encodeBase64Utf16 (plainText) {
if (!plainText)
return ''
var aUTF16CodeUnits = new Uint16Array(plainText.length)
Array.prototype.forEach.call(aUTF16CodeUnits, function (el, idx, arr) { arr[idx] = plainText.charCodeAt(idx); })
var sUTF16Base64 = base64EncArr(new Uint8Array(aUTF16CodeUnits.buffer))
return sUTF16Base64
}
function decodeBase64Utf16 (encodedText) {
if (!encodedText)
return ''
//return atob(encodedText) // This would display strange characters betwen normal characters.
var sDecodedString = String.fromCharCode.apply(null, new Uint16Array(base64DecToArr(encodedText, 2).buffer))
return sDecodedString
}
function encodeBase64Utf8(plainText) {
if (!plainText)
return ''
return window.btoa(unescape(encodeURIComponent(plainText)))
}
/** Some special characters won't be rendered in IE11 */
function decodeBase64Utf8(encodedText) {
if (!encodedText)
return ''
return decodeURIComponent(escape(window.atob(encodedText)))
}
</script>
<script>
// VAINILLA JS:
function updatePowerShellExamples(plainText, encodedText) {
document.querySelector('#txtPSCommandEncode').value = '[Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes("' + plainText + '"))'
document.querySelector('#txtPSCommandDecode').value = '[System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String("' + encodedText + '"))'
document.querySelector('#txtPSCommandRun').value = 'powershell -EncodedCommand "' + encodedText + '" -noExit'
}
document.querySelector('#btnEncode').onclick = function() {
var plainText = document.querySelector('#txtPlainText').value
var encodedTextUtf16 = encodeBase64Utf16(plainText)
var encodedTextUtf8 = encodeBase64Utf8(plainText)
document.querySelector('#txtEncodedTextUtf16').value = encodedTextUtf16
document.querySelector('#txtEncodedTextUtf8').value = encodedTextUtf8
updatePowerShellExamples(plainText, encodedTextUtf16)
}
document.querySelector('#btnDecode').onclick = function() {
var txtDecoded16 = document.querySelector('#txtDecodedTextUtf16')
var txtDecoded8 = document.querySelector('#txtDecodedTextUtf8')
txtDecoded16.classList.remove('txt-error')
txtDecoded8.classList.remove('txt-error')
var encodedText = document.querySelector('#txtEncodedText').value
var plainText16 = ''
var plainText8 = ''
try {
plainText16 = decodeBase64Utf16(encodedText)
} catch(ex) {
document.querySelector('#txtDecodedTextUtf16').classList.add('txt-error')
// alert('Can not convert to base64 as Utf-16:\r\n' + encodedText)
}
try {
plainText8 = decodeBase64Utf8(encodedText)
} catch(ex) {
txtDecoded8.classList.add('txt-error')
// alert('Can not convert to base64 as Utf-8:\r\n' + encodedText)
}
txtDecoded16.value = plainText16
txtDecoded8.value = plainText8
}
</script>