📄 blowfish.js
字号:
}); } // init the boxes var pos=0, data=0, res={ left:0, right:0 }, i, j; var box = { p: dojo.map(boxes.p.slice(0), function(item){ var l=k.length, j; for(j=0; j<4; j++){ data=(data*POW8)|k[pos++ % l]; } return (((item>>0x10)^(data>>0x10))<<0x10)|(((item&0xffff)^(data&0xffff))&0xffff); }), s0:boxes.s0.slice(0), s1:boxes.s1.slice(0), s2:boxes.s2.slice(0), s3:boxes.s3.slice(0) }; // encrypt p and the s boxes for(i=0, l=box.p.length; i<l;){ eb(res, box); box.p[i++]=res.left, box.p[i++]=res.right; } for(i=0; i<4; i++){ for(j=0, l=box["s"+i].length; j<l;){ eb(res, box); box["s"+i][j++]=res.left, box["s"+i][j++]=res.right; } } return box; }////////////////////////////////////////////////////////////////////////////// PUBLIC FUNCTIONS//////////////////////////////////////////////////////////////////////////// this.getIV=function(/* dojox.encoding.crypto.outputTypes? */ outputType){ // summary // returns the initialization vector in the output format specified by outputType var out=outputType||dojox.encoding.crypto.outputTypes.Base64; switch(out){ case dojox.encoding.crypto.outputTypes.Hex:{ return dojo.map(iv, function(item){ return item.toString(16); }).join(""); // string } case dojox.encoding.crypto.outputTypes.String:{ return iv.join(""); // string } case dojox.encoding.crypto.outputTypes.Raw:{ return iv; // array } default:{ return dojox.encoding.base64.encode(iv); // string } } }; this.setIV=function(/* string */data, /* dojox.encoding.crypto.outputTypes? */inputType){ // summary // sets the initialization vector to data (as interpreted as inputType) var ip=inputType||dojox.encoding.crypto.outputTypes.Base64; var ba=null; switch(ip){ case dojox.encoding.crypto.outputTypes.String:{ ba = dojo.map(data.split(""), function(item){ return item.charCodeAt(0); }); break; } case dojox.encoding.crypto.outputTypes.Hex:{ ba=[]; for(var i=0, l=data.length-1; i<l; i+=2){ ba.push(parseInt(data.substr(i,2), 16)); } break; } case dojox.encoding.crypto.outputTypes.Raw:{ ba=data; break; } default:{ ba=dojox.encoding.base64.decode(data); break; } } // make it a pair of words now iv={}; iv.left=ba[0]*POW24|ba[1]*POW16|ba[2]*POW8|ba[3]; iv.right=ba[4]*POW24|ba[5]*POW16|ba[6]*POW8|ba[7]; }; this.encrypt = function(/* string */plaintext, /* string */key, /* object? */ao){ // summary // encrypts plaintext using key; allows user to specify output type and cipher mode via keyword object "ao" var out=dojox.encoding.crypto.outputTypes.Base64; var mode=dojox.encoding.crypto.cipherModes.EBC; if (ao){ if (ao.outputType) out=ao.outputType; if (ao.cipherMode) mode=ao.cipherMode; } var bx = init(key), padding = 8-(plaintext.length&7); for (var i=0; i<padding; i++){ plaintext+=String.fromCharCode(padding); } var cipher=[], count=plaintext.length >> 3, pos=0, o={}, isCBC=(mode==dojox.encoding.crypto.cipherModes.CBC); var vector={left:iv.left||null, right:iv.right||null}; for(var i=0; i<count; i++){ o.left=plaintext.charCodeAt(pos)*POW24 |plaintext.charCodeAt(pos+1)*POW16 |plaintext.charCodeAt(pos+2)*POW8 |plaintext.charCodeAt(pos+3); o.right=plaintext.charCodeAt(pos+4)*POW24 |plaintext.charCodeAt(pos+5)*POW16 |plaintext.charCodeAt(pos+6)*POW8 |plaintext.charCodeAt(pos+7); if(isCBC){ o.left=(((o.left>>0x10)^(vector.left>>0x10))<<0x10)|(((o.left&0xffff)^(vector.left&0xffff))&0xffff); o.right=(((o.right>>0x10)^(vector.right>>0x10))<<0x10)|(((o.right&0xffff)^(vector.right&0xffff))&0xffff); } eb(o, bx); // encrypt the block if(isCBC){ vector.left=o.left; vector.right=o.right; } cipher.push((o.left>>24)&0xff); cipher.push((o.left>>16)&0xff); cipher.push((o.left>>8)&0xff); cipher.push(o.left&0xff); cipher.push((o.right>>24)&0xff); cipher.push((o.right>>16)&0xff); cipher.push((o.right>>8)&0xff); cipher.push(o.right&0xff); pos+=8; } switch(out){ case dojox.encoding.crypto.outputTypes.Hex:{ return dojo.map(cipher, function(item){ return item.toString(16); }).join(""); // string } case dojox.encoding.crypto.outputTypes.String:{ return cipher.join(""); // string } case dojox.encoding.crypto.outputTypes.Raw:{ return cipher; // array } default:{ return dojox.encoding.base64.encode(cipher); // string } } }; this.decrypt = function(/* string */ciphertext, /* string */key, /* object? */ao){ // summary // decrypts ciphertext using key; allows specification of how ciphertext is encoded via ao. var ip=dojox.encoding.crypto.outputTypes.Base64; var mode=dojox.encoding.crypto.cipherModes.EBC; if (ao){ if (ao.outputType) ip=ao.outputType; if (ao.cipherMode) mode=ao.cipherMode; } var bx = init(key); var pt=[]; var c=null; switch(ip){ case dojox.encoding.crypto.outputTypes.Hex:{ c = []; for(var i=0, l=ciphertext.length-1; i<l; i+=2){ c.push(parseInt(ciphertext.substr(i,2), 16)); } break; } case dojox.encoding.crypto.outputTypes.String:{ c = dojo.map(ciphertext.split(""), function(item){ return item.charCodeAt(0); }); break; } case dojox.encoding.crypto.outputTypes.Raw:{ c=ciphertext; // should be a byte array break; } default:{ c=dojox.encoding.base64.decode(ciphertext); break; } } var count=c.length >> 3, pos=0, o={}, isCBC=(mode==dojox.encoding.crypto.cipherModes.CBC); var vector={left:iv.left||null, right:iv.right||null}; for(var i=0; i<count; i++){ o.left=c[pos]*POW24|c[pos+1]*POW16|c[pos+2]*POW8|c[pos+3]; o.right=c[pos+4]*POW24|c[pos+5]*POW16|c[pos+6]*POW8|c[pos+7]; if(isCBC){ var left=o.left; var right=o.right; } db(o, bx); // decrypt the block if(isCBC){ o.left=(((o.left>>0x10)^(vector.left>>0x10))<<0x10)|(((o.left&0xffff)^(vector.left&0xffff))&0xffff); o.right=(((o.right>>0x10)^(vector.right>>0x10))<<0x10)|(((o.right&0xffff)^(vector.right&0xffff))&0xffff); vector.left=left; vector.right=right; } pt.push((o.left>>24)&0xff); pt.push((o.left>>16)&0xff); pt.push((o.left>>8)&0xff); pt.push(o.left&0xff); pt.push((o.right>>24)&0xff); pt.push((o.right>>16)&0xff); pt.push((o.right>>8)&0xff); pt.push(o.right&0xff); pos+=8; } // check for padding, and remove. if(pt[pt.length-1]==pt[pt.length-2]||pt[pt.length-1]==0x01){ var n=pt[pt.length-1]; pt.splice(pt.length-n, n); } // convert to string return dojo.map(pt, function(item){ return String.fromCharCode(item); }).join(""); // string }; this.setIV("0000000000000000", dojox.encoding.crypto.outputTypes.Hex);}();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -