新人想用node.js实现下面的一些代码
发布于 11 年前 作者 mypayne 7789 次浏览 最后一次编辑是 8 年前

下面是Crypto-JS照搬过来的,不过在node.js下貌似会不行

    function util_bytesToBase64(a) {
    if ("function" == typeof btoa) return btoa(f_bytesToString(a));
    for (var b = [], c = 0; c < a.length; c += 3) for (var d = a[c] << 16 | a[c + 1] << 8 | a[c + 2], e = 0; 4 > e; e++) 8 * c + 6 * e <= 8 * a.length ? b.push("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".charAt(d >>> 6 * (3 - e) & 63)) : b.push("=");
    return b.join("");
}
    function util_hexToBytes(a) {
        for (var b = [], c = 0; c < a.length; c += 2) b.push(parseInt(a.substr(c, 2), 16));
        return b;
    }
    
    function util_bytesToHex(a) {
        for (var b = [], c = 0; c < a.length; c++) b.push((a[c] >>> 4).toString(16)),
        b.push((a[c] & 15).toString(16));
        return b.join("");
    }
    
    function charenc_UTF8_bytesToString(a){
        return decodeURIComponent(escape(f_bytesToString(a)));
    }
    
    function charenc_UTF8_stringToBytes(a) {
        return f_stringToBytes(unescape(encodeURIComponent(a)));
    }
    
    function f_stringToBytes(a) {
        for (var b = [], c = 0; c < a.length; c++) b.push(a.charCodeAt(c) & 255);
        return b;
    }
    
    function f_bytesToString(a) {
        for (var b = [], c = 0; c < a.length; c++) b.push(String.fromCharCode(a[c]));
        return b.join("");
    }
    function util_base64ToBytes(a) {
        if ("function" == typeof atob) return f_stringToBytes(atob(a));
        for (var a = a.replace(/[^A-Z0-9+\/]/ig, ""), b = [], c = 0, d = 0; c < a.length; d = ++c % 4) 0 != d && b.push(("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(a.charAt(c - 1)) & Math.pow(2, -2 * d + 8) - 1) << 2 * d | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".indexOf(a.charAt(c)) >>> 6 - 2 * d);
        return b;
    }

报错:

SyntaxError: Unexpected token ) at Module.wrappedCompile [as _compile] (instance1/nodejs/runtime/src/sandbox/modules/module.js:104:24) at Object.Module._extensions…js (module.js:502:10) at Module.load (module.js:392:32) at Function.Module._load (module.js:350:12) at Module.require (module.js:398:17) at require (instance1/nodejs/runtime/src/sandbox/modules/module.js:42:15) at Object.<anonymous> (instance1/nodejs/test/app/routes/index.js:1:74) at Module.wrappedCompile [as _compile] (instance1/nodejs/runtime/src/sandbox/modules/module.js:118:25) at Object.Module._extensions…js (module.js:502:10) at Module.load (module.js:392:32)

新人不知道怎么解决好。无限感谢

6 回复

//将Ansi编码的字符串进行Base64编码 function encode64(input) { var keyStr = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=”; var output = “”; var chr1, chr2, chr3 = “”; var enc1, enc2, enc3, enc4 = “”; var i = 0;

do {
	chr1 = input.charCodeAt(i++);
	chr2 = input.charCodeAt(i++);
	chr3 = input.charCodeAt(i++);

	enc1 = chr1 >> 2;
	enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
	enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
	enc4 = chr3 & 63;

	if (isNaN(chr2)) {
		enc3 = enc4 = 64;
	} else if (isNaN(chr3)) {
		enc4 = 64;
	}

	output = output +
	keyStr.charAt(enc1) +
	keyStr.charAt(enc2) +
	keyStr.charAt(enc3) +
	keyStr.charAt(enc4);
	chr1 = chr2 = chr3 = "";
	enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);

return output;

}

这个编码必须UTF-8的 一个是byte转Base64,另一个是Base64转byte 这个在前台js是没问题的。主要我是想在后台的node.js服务器也能执行

自己考代码 都考错了好不,还有语法错误

神马意思? 这段Crypto-JS是被改过的,不过还是可以用的。只是最近想移植到node.js都能用罢了 想用buffer来弄,但是还是少了点什么 这个主要是用来加密东西的

@Jackson

//错误日志

instance1/nodejs/oauth1.duapp.com/app/models/eLib.js:194 }); ^

SyntaxError: Unexpected token ) at Module.wrappedCompile [as _compile] (instance1/nodejs/runtime/src/sandbox/modules/module.js:104:24) at Object.Module._extensions…js (module.js:502:10) at Module.load (module.js:392:32) at Function.Module._load (module.js:350:12) at Module.require (module.js:398:17) at require (instance1/nodejs/runtime/src/sandbox/modules/module.js:42:15) at Object.<anonymous> (instance1/nodejs/example.com/app/routes/index.js:1:74) at Module.wrappedCompile [as _compile] (instance1/nodejs/runtime/src/sandbox/modules/module.js:118:25) at Object.Module._extensions…js (module.js:502:10) at Module.load (module.js:392:32)

回到顶部