-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
115 lines (110 loc) · 2.86 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
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
<html>
<head>
<script src="bigint.js"></script>
</head>
<body>
<h1>Rad-X!</h1>
<pre>When I first learned about number bases, I thought it was neat that some numbers
<a href="https://en.wikipedia.org/wiki/Hexspeak">happened to correspond with real words</a>.
Here's a cute little encoder that uses that idea.</pre>
<pre>Take a text input from a-z, and read it as a number.
Figure out the minimum radix (base) needed to properly read it.
Convert it to base 10.
To 'decrypt', provide the original base and the base 10 number.</pre>
<pre>For example, 'radx' is 1073243 in base 34.</pre>
<h2>"Encrypt"</h2>
<label for="plaintextinput">Input</label>
<br>
<input type="text" id="plaintextinput" style="text-transform: uppercase; width: 300px;"></input>
<br>
<button id="encrypt-button" name="encrypt-button">"Encrypt"</button>
<div style="margin-top: 1rem;">
<div>Encrypted Output</div>
<pre id="encryptedoutput"></pre>
</div>
<hr>
<h2>"Decrypt"</h2>
<label for="encryptedinput">Encrypted Input</label>
<br>
<input type="text" id="encryptedinput" style="text-transform: uppercase; width: 300px;"></input>
<br>
<button id="decrypt-button" name="decrypt-button">"Decrypt"</button>
<div style="margin-top: 1rem">
<div>Plaintext Output</div>
<pre id="plaintextoutput"></pre>
</div>
<script>
function encrypt(input){
var map = {
"0": 1,
"1": 2,
"2": 3,
"3": 4,
"4": 5,
"5": 6,
"6": 7,
"7": 8,
"8": 9,
"9": 10,
"A": 11,
"B": 12,
"C": 13,
"D": 14,
"E": 15,
"F": 16,
"G": 17,
"H": 18,
"I": 19,
"J": 20,
"K": 21,
"L": 22,
"M": 23,
"N": 24,
"O": 25,
"P": 26,
"Q": 27,
"R": 28,
"S": 29,
"T": 30,
"U": 31,
"V": 32,
"W": 33,
"X": 34,
"Y": 35,
"Z": 36
}
var base = 1;
for (var i=0; i < input.length; i++) {
var letterbase = map[input.charAt(i).toUpperCase()];
if (base < letterbase){
base = letterbase;
}
}
return bigInt(input, base).toString() + "_" + base.toString();
}
function decrypt(input){
var inputSplit = input.split("_");
return bigInt(inputSplit[0], 10).toString(inputSplit[1]).toUpperCase();
}
function buttonEventListener(input, output, functionToRun){
return function(){
var inputElement = document.getElementById(input);
var outputElement = document.getElementById(output);
outputElement.textContent = "";
var text = inputElement.value;
var inputSplit = text.trim().split(" ");
for (var i=0; i < inputSplit.length; i++) {
if (inputSplit[i] !== ""){
outputElement.textContent += functionToRun(inputSplit[i]);
if (i < inputSplit.length - 1 ) {
outputElement.textContent += " ";
}
}
}
}
}
document.getElementById("encrypt-button").addEventListener("click", buttonEventListener("plaintextinput", "encryptedoutput", encrypt));
document.getElementById("decrypt-button").addEventListener("click", buttonEventListener("encryptedinput", "plaintextoutput", decrypt));
</script>
</body>
</html>