Created
July 31, 2014 13:50
-
-
Save avlidienbrunn/652e63e4331b8251c21c to your computer and use it in GitHub Desktop.
base64fiddle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<script> | |
function text2bin(text){ | |
var pad = "00000000"; | |
var result = ""; | |
for(index in text){ | |
result += (pad + text.charCodeAt(index).toString(2)).slice(-8); //Pad with zeroes so that each char always becomes 8 binary chars | |
} | |
return result; | |
} | |
function bin2int(text){ | |
return parseInt(text, 2); | |
} | |
function int2bin(text){ | |
var pad = "00000000"; | |
var result = ""; | |
for(index in text){ | |
result += (pad + text.toString(2)).slice(-8); //Pad with zeroes so that each char always becomes 8 binary chars | |
} | |
return result; | |
} | |
function parseString(text){ | |
//WARNING: Ugly hack ahead! | |
text = text.replace(/"/g, "\\x22"); | |
return eval('"' + text + '"') | |
} | |
function int2base64(char){ | |
var base64table = ["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Za","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/"]; | |
return base64table[char]; | |
} | |
function validate(startswith, desired){ | |
bitcount = Math.pow(2, 6 - startswith.length); //Calculate how many bits to iterate | |
startswithint = bin2int((startswith + "000000").slice(6)); | |
valid = []; | |
for(x=0;x<bitcount;x++){ | |
currentchar = startswithint + x; | |
if(desired.indexOf(int2base64(currentchar)) != -1){ | |
console.log(desired); | |
//The next charcter can start with the binary representation of x! | |
valid.push(x); | |
} | |
} | |
return valid; | |
} | |
</script> | |
<script> | |
var startingchars = null; | |
function encodeArea(){ | |
var item1 = document.getElementById('textbox'); | |
var item2 = document.getElementById('textboxbase64'); | |
var parsed = parseString(item1.value); | |
calulatePossible(); | |
item2.value = btoa(parsed); | |
item1.vaue = atob(item2.value); | |
} | |
function calculateStartingChars(){ | |
var desired = document.getElementById('desired').value + "="; | |
var possible = document.getElementById('possible'); | |
startingchars = []; | |
var found = false; | |
for(char1=0;char1<255;char1++){ | |
for(char2=0;char2<255;char2++){ | |
b64 = btoa(String.fromCharCode(char1) + String.fromCharCode(char2)); | |
if(desired.indexOf(b64[0]) != -1 && desired.indexOf(b64[1]) != -1){ | |
startingchars.push((String.fromCharCode(char1))); | |
break; | |
} | |
} | |
found = false; | |
} | |
possible.value = startingchars.join(',').replace(/(\s)/g, escape); | |
} | |
function calulatePossible(){ | |
var desired = document.getElementById('desired').value + "="; | |
var possible = document.getElementById('possible'); | |
var parsed = parseString(document.getElementById('textbox').value); | |
var possiblechars = []; | |
var currentIndex = (parsed.length - 1) % 3; //Base64 works in triplets | |
if(startingchars == null || parsed == ""){ | |
calculateStartingChars(); | |
possible.value = startingchars.join(',').replace(/(\s)/g, escape); | |
return; | |
} | |
parsed = parsed.replace(/(?:.{3})*(.{1,3})/g, "$1"); //Remove every triplet before the last triplet (aaabbbcc will become cc) | |
if(currentIndex == 0){ | |
for(char1=0;char1<256;char1++){ | |
b64 = btoa(parsed + String.fromCharCode(char1)); | |
if(desired.indexOf(b64[0]) != -1 && desired.indexOf(b64[1]) != -1){ | |
//Found one character that will generate DD?? (D = desired) | |
for(char2=0;char2<256;char2++){ | |
b64inner = btoa(parsed + String.fromCharCode(char1) + String.fromCharCode(char2)); | |
if(desired.indexOf(b64[2]) != -1 && desired.indexOf(b64[3]) != -1){ | |
//Found one character that will generate DDDD (D = desired) | |
possiblechars.push((String.fromCharCode(char1))); | |
break; | |
} | |
} | |
} | |
} | |
} | |
if(currentIndex == 1){ | |
for(char1=0;char1<256;char1++){ | |
b64 = btoa(parsed + String.fromCharCode(char1)); | |
if(desired.indexOf(b64[2]) != -1 && desired.indexOf(b64[3]) != -1){ | |
//Found one character that will generate DDDD (D = desired) | |
possiblechars.push((String.fromCharCode(char1))); | |
} | |
} | |
} | |
if(currentIndex == 2){ | |
possiblechars = startingchars; | |
} | |
possible.value = possiblechars.join(',').replace(/(\s)/g, escape); | |
} | |
</script> | |
<style> | |
/* CSS. Not even once. */ | |
textarea {width: 49%; height: 200;} | |
input {width: 300px;} | |
</style> | |
</head> | |
<body> | |
Desired charset: <input type="text" id="desired" value="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+/"/><button onclick="calculateStartingChars()">Set charset</button><br /> | |
<textarea onkeyup="encodeArea()" id="textbox"></textarea> | |
<textarea onkeyup="encodeArea()" id="textboxbase64"></textarea><br /> | |
Possible next characters: <textarea id="possible"></textarea> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment