Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/core/main/client/encode/base64.js
1154 views
1
//
2
// Copyright (c) 2006-2025 Wade Alcorn - [email protected]
3
// Browser Exploitation Framework (BeEF) - https://beefproject.com
4
// See the file 'doc/COPYING' for copying permission
5
//
6
7
// Base64 code from http://stackoverflow.com/questions/3774622/how-to-base64-encode-inside-of-javascript/3774662#3774662
8
9
beef.encode = {};
10
11
/**
12
* Base64 code from http://stackoverflow.com/questions/3774622/how-to-base64-encode-inside-of-javascript/3774662#3774662
13
* @namespace beef.encode.base64
14
*/
15
beef.encode.base64 = {
16
17
keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
18
/**
19
* @memberof beef.encode.base64
20
* @param {string} input
21
* @return {string}
22
*/
23
encode : function (input) {
24
if (window.btoa) {
25
return btoa(unescape(encodeURIComponent(input)));
26
}
27
28
var output = "";
29
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
30
var i = 0;
31
32
input = beef.encode.base64.utf8_encode(input);
33
34
while (i < input.length) {
35
36
chr1 = input.charCodeAt(i++);
37
chr2 = input.charCodeAt(i++);
38
chr3 = input.charCodeAt(i++);
39
40
enc1 = chr1 >> 2;
41
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
42
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
43
enc4 = chr3 & 63;
44
45
if (isNaN(chr2)) {
46
enc3 = enc4 = 64;
47
} else if (isNaN(chr3)) {
48
enc4 = 64;
49
}
50
51
output = output +
52
this.keyStr.charAt(enc1) + this.keyStr.charAt(enc2) +
53
this.keyStr.charAt(enc3) + this.keyStr.charAt(enc4);
54
55
}
56
57
return output;
58
},
59
60
/**
61
* @memberof beef.encode.base64
62
* @param {string} input
63
* @return {string}
64
*/
65
decode : function (input) {
66
if (window.atob) {
67
return escape(atob(input));
68
}
69
70
var output = "";
71
var chr1, chr2, chr3;
72
var enc1, enc2, enc3, enc4;
73
var i = 0;
74
75
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
76
77
while (i < input.length) {
78
79
enc1 = this.keyStr.indexOf(input.charAt(i++));
80
enc2 = this.keyStr.indexOf(input.charAt(i++));
81
enc3 = this.keyStr.indexOf(input.charAt(i++));
82
enc4 = this.keyStr.indexOf(input.charAt(i++));
83
84
chr1 = (enc1 << 2) | (enc2 >> 4);
85
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
86
chr3 = ((enc3 & 3) << 6) | enc4;
87
88
output = output + String.fromCharCode(chr1);
89
90
if (enc3 != 64) {
91
output = output + String.fromCharCode(chr2);
92
}
93
if (enc4 != 64) {
94
output = output + String.fromCharCode(chr3);
95
}
96
97
}
98
99
output = beef.encode.base64.utf8_decode(output);
100
101
return output;
102
103
},
104
105
/**
106
* @memberof beef.encode.base64
107
* @param {string} string
108
* @return {string}
109
*/
110
utf8_encode : function (string) {
111
string = string.replace(/\r\n/g,"\n");
112
var utftext = "";
113
114
for (var n = 0; n < string.length; n++) {
115
116
var c = string.charCodeAt(n);
117
118
if (c < 128) {
119
utftext += String.fromCharCode(c);
120
}
121
else if((c > 127) && (c < 2048)) {
122
utftext += String.fromCharCode((c >> 6) | 192);
123
utftext += String.fromCharCode((c & 63) | 128);
124
}
125
else {
126
utftext += String.fromCharCode((c >> 12) | 224);
127
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
128
utftext += String.fromCharCode((c & 63) | 128);
129
}
130
131
}
132
133
return utftext;
134
},
135
/**
136
* @memberof beef.encode.base64
137
* @param {string} utftext
138
* @return {string}
139
*/
140
utf8_decode : function (utftext) {
141
var string = "";
142
var i = 0;
143
var c = c1 = c2 = 0;
144
145
while ( i < utftext.length ) {
146
147
c = utftext.charCodeAt(i);
148
149
if (c < 128) {
150
string += String.fromCharCode(c);
151
i++;
152
}
153
else if((c > 191) && (c < 224)) {
154
c2 = utftext.charCodeAt(i+1);
155
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
156
i += 2;
157
}
158
else {
159
c2 = utftext.charCodeAt(i+1);
160
c3 = utftext.charCodeAt(i+2);
161
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
162
i += 3;
163
}
164
165
}
166
167
return string;
168
}
169
170
};
171
172
beef.regCmp('beef.encode.base64');
173
174