summaryrefslogblamecommitdiff
path: root/blog/sbs_1/decryptor.js
blob: dcd64e3d7850d011c58ae5b4633da00e6c668f90 (plain) (tree)








































































                                                                                                                                 
//License: Expat(MIT)
//Chrisoft Xiong 2017
// http://www.onicos.com/staff/iz/amuse/javascript/expert/utf.txt
function Utf8ArrayToStr(array){
    var out,i,len,c;
    var char2,char3;
    out="";
    len=array.length;
    i=0;
    while(i<len){
		c=array[i++];
		switch(c>>4)
		{
		  case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:
			// 0xxxxxxx
			out+=String.fromCharCode(c);
			break;
		  case 12:case 13:
			// 110x xxxx   10xx xxxx
			char2=array[i++];
			out+=String.fromCharCode(((c&0x1F)<<6)|(char2&0x3F));
			break;
		  case 14:
			// 1110 xxxx  10xx xxxx  10xx xxxx
			char2=array[i++];
			char3=array[i++];
			out+=String.fromCharCode(((c&0x0F)<<12)|
						   ((char2&0x3F)<<6)|
						   ((char3&0x3F)<<0));
			break;
		}
    }
    return out;
}
var decid;
function decryptui(id)
{
	document.getElementById("decryptui").style.display="block";
	document.getElementById("keyinp").focus();
	setTimeout(function(){document.getElementById("decryptui").style.opacity="1";},20);
	decid=id;
	document.getElementById("keyhint").innerHTML="Hint: "+document.getElementById("encrypted"+id).getAttribute("hint");
	document.getElementById("keyinp").onkeypress=function(e){if(e.keyCode==13)document.getElementById('btndecrypt').click();}
}
function hidedecryptui()
{
	document.getElementById("decryptui").style.opacity="0";
	setTimeout(function(){
		document.getElementById("decryptui").style.display="none";
		document.getElementById("keyinp").value="";
	},500);
}
function decryptor(id,key)
{
	var e=document.getElementById("encrypted"+id);
	var cont=e.getAttribute("encont");
	var bc=atob(cont);
	var b=new Array(bc.length);
	for(var i=0;i<bc.length;++i)b[i]=bc.charCodeAt(i);
	var lkey=key.length;
	var u8arr=new Uint8Array(b);
	var hash=5381;
	for(var i=0;i<u8arr.length;++i){u8arr[i]^=key.charCodeAt(i%lkey);hash=(hash*33)+u8arr[i];hash%=4294967296;}
	if(hash!=parseInt(e.getAttribute("hash")))
	{
		alert("The decryption key you have entered could be wrong, please try again.");
		return;
	}
	//e.innerHTML=new TextDecoder("utf-8").decode(u8arr);
	e.innerHTML=Utf8ArrayToStr(u8arr);
	footnoter();
	hidedecryptui();
}