blowfish.js
来自「Hippo CMS是一个以信息为中心的开源内容管理系统。Hippo CMS目标是」· JavaScript 代码 · 共 385 行 · 第 1/2 页
JS
385 行
for (var i = 0; i < l; ) {
var t = tab.indexOf(s[i++]) << 18 | tab.indexOf(s[i++]) << 12 | tab.indexOf(s[i++]) << 6 | tab.indexOf(s[i++]);
out.push((t >>> 16) & 255);
out.push((t >>> 8) & 255);
out.push(t & 255);
}
return out;
}
this.getIV = function (outputType) {
var out = outputType || dojo.crypto.outputTypes.Base64;
switch (out) {
case dojo.crypto.outputTypes.Hex:
var s = [];
for (var i = 0; i < iv.length; i++) {
s.push((iv[i]).toString(16));
}
return s.join("");
case dojo.crypto.outputTypes.String:
return iv.join("");
case dojo.crypto.outputTypes.Raw:
return iv;
default:
return toBase64(iv);
}
};
this.setIV = function (data, inputType) {
var ip = inputType || dojo.crypto.outputTypes.Base64;
var ba = null;
switch (ip) {
case dojo.crypto.outputTypes.String:
ba = [];
for (var i = 0; i < data.length; i++) {
ba.push(data.charCodeAt(i));
}
break;
case dojo.crypto.outputTypes.Hex:
ba = [];
var i = 0;
while (i + 1 < data.length) {
ba.push(parseInt(data.substr(i, 2), 16));
i += 2;
}
break;
case dojo.crypto.outputTypes.Raw:
ba = data;
break;
default:
ba = fromBase64(data);
break;
}
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 (plaintext, key, ao) {
var out = dojo.crypto.outputTypes.Base64;
var mode = dojo.crypto.cipherModes.EBC;
if (ao) {
if (ao.outputType) {
out = ao.outputType;
}
if (ao.cipherMode) {
mode = ao.cipherMode;
}
}
var bx = init(key);
var padding = 8 - (plaintext.length & 7);
for (var i = 0; i < padding; i++) {
plaintext += String.fromCharCode(padding);
}
var cipher = [];
var count = plaintext.length >> 3;
var pos = 0;
var o = {};
var isCBC = (mode == dojo.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 = xor(o.left, vector.left);
o.right = xor(o.right, vector.right);
}
eb(o, bx);
if (isCBC) {
vector.left = o.left;
vector.right = o.right;
dojo.crypto.outputTypes.Hex;
}
cipher.push((o.left >> 24) & 255);
cipher.push((o.left >> 16) & 255);
cipher.push((o.left >> 8) & 255);
cipher.push(o.left & 255);
cipher.push((o.right >> 24) & 255);
cipher.push((o.right >> 16) & 255);
cipher.push((o.right >> 8) & 255);
cipher.push(o.right & 255);
pos += 8;
}
switch (out) {
case dojo.crypto.outputTypes.Hex:
var s = [];
for (var i = 0; i < cipher.length; i++) {
s.push((cipher[i]).toString(16));
}
return s.join("");
case dojo.crypto.outputTypes.String:
return cipher.join("");
case dojo.crypto.outputTypes.Raw:
return cipher;
default:
return toBase64(cipher);
}
};
this.decrypt = function (ciphertext, key, ao) {
var ip = dojo.crypto.outputTypes.Base64;
var mode = dojo.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 dojo.crypto.outputTypes.Hex:
c = [];
var i = 0;
while (i + 1 < ciphertext.length) {
c.push(parseInt(ciphertext.substr(i, 2), 16));
i += 2;
}
break;
case dojo.crypto.outputTypes.String:
c = [];
for (var i = 0; i < ciphertext.length; i++) {
c.push(ciphertext.charCodeAt(i));
}
break;
case dojo.crypto.outputTypes.Raw:
c = ciphertext;
break;
default:
c = fromBase64(ciphertext);
break;
}
var count = c.length >> 3;
var pos = 0;
var o = {};
var isCBC = (mode == dojo.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);
if (isCBC) {
o.left = xor(o.left, vector.left);
o.right = xor(o.right, vector.right);
vector.left = left;
vector.right = right;
}
pt.push((o.left >> 24) & 255);
pt.push((o.left >> 16) & 255);
pt.push((o.left >> 8) & 255);
pt.push(o.left & 255);
pt.push((o.right >> 24) & 255);
pt.push((o.right >> 16) & 255);
pt.push((o.right >> 8) & 255);
pt.push(o.right & 255);
pos += 8;
}
if (pt[pt.length - 1] == pt[pt.length - 2] || pt[pt.length - 1] == 1) {
var n = pt[pt.length - 1];
pt.splice(pt.length - n, n);
}
for (var i = 0; i < pt.length; i++) {
pt[i] = String.fromCharCode(pt[i]);
}
return pt.join("");
};
this.setIV("0000000000000000", dojo.crypto.outputTypes.Hex);
}();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?