-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
48 lines (43 loc) · 1.47 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Base 64 Encode / Decode</title>
</head>
<body>
<h1>Base 64 Encode / Decode</h1>
<!-- <input type="text" > -->
<textarea id="inputText" rows="4" cols="50"></textarea>
<br>
<button type="button" onclick="doaTob();">Encode</button>
<br>
<textarea id="outputText" rows="4" cols="50"></textarea>
<br>
<button type="button" onclick="dobToa();">Decode</button>
<br>
<textarea id="decodeText" rows="4" cols="50"></textarea>
<script>
function doaTob() {
document.getElementById("outputText").value = b64EncodeUnicode(document.getElementById("inputText").value);
}
function dobToa() {
document.getElementById("decodeText").value = b64DecodeUnicode(document.getElementById("outputText").value);
}
function b64EncodeUnicode(str) {
// first we use encodeURIComponent to get percent-encoded UTF-8,
// then we convert the percent encodings into raw bytes which
// can be fed into btoa.
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function toSolidBytes(match, p1) {
return String.fromCharCode('0x' + p1);
}));
}
function b64DecodeUnicode(str) {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
</script>
</body>
</html>