-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcription.min.js
1 lines (1 loc) · 6.5 KB
/
cription.min.js
1
var Cription=function(){var t={appData:{key:null,secret:null,error:""},aes:{AES_Sbox:new Array(99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,147,38,54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179,41,227,47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64,143,146,157,56,245,188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115,96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,78,169,108,86,244,234,101,122,174,8,186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158,225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22),AES_ShiftRowTab:new Array(0,5,10,15,4,9,14,3,8,13,2,7,12,1,6,11),bytesToString:function(e){var t="";for(var n=0;n<e.length;n++){t+=String.fromCharCode(e[n])}return t},stringToBytes:function(e){var t,n,r=[];for(var i=0;i<e.length;i++){t=e.charCodeAt(i);n=[];do{n.push(t&255);t=t>>8}while(t);r=r.concat(n.reverse())}return r},randomStr:function(e){var e=e||16;s="",r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(var t=0;t<e;t++){s+=r.charAt(Math.floor(Math.random()*r.length))}return s},AES_Init:function(){t.aes.AES_Sbox_Inv=new Array(256);for(var e=0;e<256;e++){t.aes.AES_Sbox_Inv[t.aes.AES_Sbox[e]]=e}t.aes.AES_ShiftRowTab_Inv=new Array(16);for(var e=0;e<16;e++){t.aes.AES_ShiftRowTab_Inv[t.aes.AES_ShiftRowTab[e]]=e}t.aes.AES_xtime=new Array(256);for(var e=0;e<128;e++){t.aes.AES_xtime[e]=e<<1;t.aes.AES_xtime[128+e]=e<<1^27}},AES_Done:function(){delete t.aes.AES_Sbox_Inv;delete t.aes.AES_ShiftRowTab_Inv;delete t.aes.AES_xtime},AES_ExpandKey:function(e){var n=e.length,r,i=1;switch(n){case 16:r=16*(10+1);break;case 24:r=16*(12+1);break;case 32:r=16*(14+1);break;default:console.log("AES_ExpandKey: Only key lengths of 16, 24 or 32 bytes allowed!")}for(var s=n;s<r;s+=4){var o=e.slice(s-4,s);if(s%n==0){o=new Array(t.aes.AES_Sbox[o[1]]^i,t.aes.AES_Sbox[o[2]],t.aes.AES_Sbox[o[3]],t.aes.AES_Sbox[o[0]]);if((i<<=1)>=256){i^=283}}else if(n>24&&s%n==16){o=new Array(t.aes.AES_Sbox[o[0]],t.aes.AES_Sbox[o[1]],t.aes.AES_Sbox[o[2]],t.aes.AES_Sbox[o[3]])}for(var u=0;u<4;u++){e[s+u]=e[s+u-n]^o[u]}}},AES_Encrypt:function(e,n){var r=n.length;t.aes.AES_AddRoundKey(e,n.slice(0,16));for(var i=16;i<r-16;i+=16){t.aes.AES_SubBytes(e,t.aes.AES_Sbox);t.aes.AES_ShiftRows(e,t.aes.AES_ShiftRowTab);t.aes.AES_MixColumns(e);t.aes.AES_AddRoundKey(e,n.slice(i,i+16))}t.aes.AES_SubBytes(e,t.aes.AES_Sbox);t.aes.AES_ShiftRows(e,t.aes.AES_ShiftRowTab);t.aes.AES_AddRoundKey(e,n.slice(i,r))},AES_Decrypt:function(e,n){var r=n.length;t.aes.AES_AddRoundKey(e,n.slice(r-16,r));t.aes.AES_ShiftRows(e,t.aes.AES_ShiftRowTab_Inv);t.aes.AES_SubBytes(e,t.aes.AES_Sbox_Inv);for(var i=r-32;i>=16;i-=16){t.aes.AES_AddRoundKey(e,n.slice(i,i+16));t.aes.AES_MixColumns_Inv(e);t.aes.AES_ShiftRows(e,t.aes.AES_ShiftRowTab_Inv);t.aes.AES_SubBytes(e,t.aes.AES_Sbox_Inv)}t.aes.AES_AddRoundKey(e,n.slice(0,16))},AES_SubBytes:function(e,t){for(var n=0;n<16;n++){e[n]=t[e[n]]}},AES_AddRoundKey:function(e,t){for(var n=0;n<16;n++){e[n]^=t[n]}},AES_ShiftRows:function(e,t){var n=(new Array).concat(e);for(var r=0;r<16;r++){e[r]=n[t[r]]}},AES_MixColumns:function(e){for(var n=0;n<16;n+=4){var r=e[n+0],i=e[n+1];var s=e[n+2],o=e[n+3];var u=r^i^s^o;e[n+0]^=u^t.aes.AES_xtime[r^i];e[n+1]^=u^t.aes.AES_xtime[i^s];e[n+2]^=u^t.aes.AES_xtime[s^o];e[n+3]^=u^t.aes.AES_xtime[o^r]}},AES_MixColumns_Inv:function(e){for(var n=0;n<16;n+=4){var r=e[n+0],i=e[n+1];var s=e[n+2],o=e[n+3];var u=r^i^s^o;var a=t.aes.AES_xtime[u];var f=t.aes.AES_xtime[t.aes.AES_xtime[a^r^s]]^u;var l=t.aes.AES_xtime[t.aes.AES_xtime[a^i^o]]^u;e[n+0]^=f^t.aes.AES_xtime[r^i];e[n+1]^=l^t.aes.AES_xtime[i^s];e[n+2]^=f^t.aes.AES_xtime[s^o];e[n+3]^=l^t.aes.AES_xtime[o^r]}}},validate:{isNotEmpty:function(e){if(typeof e==="undefined"||e==""){t.appData.err+="\nA value you have entered is empty: "+e;return false}else{return true}},isValidKeyLength:function(e){if(e.length==16||e.length==24||e.length==32){return true}t.appData.err+="\nInvalid key length: "+e.length+": "+e;return false},isAlphaNumeric:function(e){if(/^[a-z0-9]+$/i.test(e)){return true}t.appData.err+="\nInvalid key, not alphanumeric: "+e;return false},isValid:function(e,n){t.appData.err="***INVALID DATA***";var r=false;switch(e){case"secret":if(t.validate.isNotEmpty(n)){r=true}break;case"key":if(t.validate.isNotEmpty(n)&&t.validate.isValidKeyLength(n)&&t.validate.isAlphaNumeric(n)){r=true}break;default:console.log("Could not match a switch case");break}if(!r){console.log(t.appData.err);return false}else{return true}}},util:{bytesToString:function(e){var t="";for(var n=0;n<e.length;n++){t+=String.fromCharCode(e[n])}return t},stringToBytes:function(e){var t,n,r=[];for(var i=0;i<e.length;i++){t=e.charCodeAt(i);n=[];do{n.push(t&255);t=t>>8}while(t);r=r.concat(n.reverse())}return r},randomStr:function(e){var e=e||9;s="",r="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";for(var t=0;t<e;t++){s+=r.charAt(Math.floor(Math.random()*r.length))}return s}}};return function(){var n=this;n.encrypt=function(e){t.aes.AES_Init();if(!e.key||e.key==""){var n=t.util.randomStr(16);if(t.validate.isValid("key",n)){console.log("This is your private key, keep it safe: "+n);e.key=n}}if(e.store&&t.validate.isValid("key",e.key)){localStorage.setItem("key",e.key)}if(t.validate.isValid("secret",e.secret)&&t.validate.isValid("key",e.key)){t.aes.AES_Init();t.appData.key=t.util.stringToBytes(e.key);t.appData.secret=t.util.stringToBytes(e.secret);t.aes.AES_ExpandKey(t.appData.key);t.aes.AES_Encrypt(t.appData.secret,t.appData.key);t.aes.AES_Done();return t.appData.secret.toString()}};n.decrypt=function(e){t.aes.AES_Init();if(!e.key||e.key==""||e.key==="undefined"){if(t.validate.isValid("key",localStorage.getItem("key"))){e.key=localStorage.getItem("key")}else{return false}}if(t.validate.isValid("secret",e.secret)&&t.validate.isValid("key",e.key)){t.appData.secret=JSON.parse("["+e.secret+"]");t.appData.key=t.util.stringToBytes(e.key);t.aes.AES_ExpandKey(t.appData.key);t.aes.AES_Decrypt(t.appData.secret,t.appData.key);t.appData.secret=t.util.bytesToString(t.appData.secret);t.aes.AES_Done();return t.appData.secret}};n.generateKey=function(e){var n=t.util.randomStr(e);if(t.validate.isValid("key",n)){return n}};t.aes.AES_Init()}}()