2014年3月14日 星期五

AzDG可逆加密演算法 For Javascript

老狗玩不出新把戲( ╮╯▂╰╭)AzDG再度上場,本次的版本是給Node.js於後端使用,請勿使用在前端頁面中,這樣的話私鑰就曝光了。
使用前請先用NPM安裝MD5的套件( MD5 ),此外也沒有提供額外的字元集支持,請使用UTF-8字元編碼。
程式中的 byte to utf-8 字串的程式碼來源在這裡
/*Sava as AzDG.js*/
(function() {
    var md5 = require('MD5');    
    var utf8 =
    {
        encode: function(str) {
            var len = str.length;
            var result = [];
            var code;
            var i;
            for (i = 0; i < len; i++) {
                code = str.charCodeAt(i);
                if (code <= 0x7f) {
                    result.push(code);
                } else if (code <= 0x7ff) { // 2 bytes                     
                    result.push(0xc0 | (code >>> 6 & 0x1f),
                        0x80 | (code & 0x3f));
                } else if (code <= 0xd700 || code >= 0xe000) { // 3 bytes
                    result.push(0xe0 | (code >>> 12 & 0x0f),
                        0x80 | (code >>> 6 & 0x3f),
                        0x80 | (code & 0x3f));
                } else { // 4 bytes, surrogate pair
                    code = (((code - 0xd800) << 10) | (str.charCodeAt(++i) - 0xdc00)) + 0x10000;
                    result.push(0xf0 | (code >>> 18 & 0x07),
                        0x80 | (code >>> 12 & 0x3f),
                        0x80 | (code >>> 6 & 0x3f),
                        0x80 | (code & 0x3f));
                }
            }
            return result;
        },
        decode: function(array) {
            var out, i, len, c;
            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
                    out += String.fromCharCode(((c & 0x1F) << 6) | (array[i++] & 0x3F));
                    break;
                case 14:
                    // 1110 xxxx  10xx xxxx  10xx xxxx
                    out += String.fromCharCode(((c & 0x0F) << 12) | 
                      ((array[i++] & 0x3F) << 6) |
                      ((array[i++] & 0x3F) << 0));
                    break;
                }
            }
            return out;
        }
    }

    var AzDG = {
        cipher: 'Private key',
        cipherEncode: function(inputData) {
            var cipherHash = utf8.encode(md5(this.cipher));
            var outData = new Array(inputData.length);
            for (var i = 0; i < inputData.length; i++) {
                outData[i] = (inputData[i] ^ cipherHash[i % 32]);
            }
            return outData;
        },
        encrypt: function(sourceText) {
            var inputData  = utf8.encode(sourceText); 
            var noise = md5(Math.round((new Date()).getTime()).toString()); 
            var outData = new Array(inputData.length * 2);
            for (var j = 0; j < inputData.length; j++) {
                outData[(j * 2)] = noise[j % 32];
                outData[(j * 2) + 1] = inputData[j] ^ noise[j % 32];
            }           
            return new Buffer(this.cipherEncode(outData)).toString('base64')
        },
        decrypt: function(sourceText) {
            var decodeSourceText = this.cipherEncode(new Buffer(sourceText, 'base64'));
            var size = Math.ceil(decodeSourceText.length / 2);
            var outData = new Array(size);
            for (var j = 0; j < size; ++j) {
                outData[j] = (decodeSourceText[(j * 2)] ^ decodeSourceText[(j * 2) + 1]);
            }
            return utf8.decode(outData);
        }
    }
    module.exports.getInstance = function() {
        return AzDG; 
    };
})();

使用方式

var azdg =  require('./AzDG').getInstance();
encode = azdg.encrypt('她來聽我 的演唱會 在十七歲的初戀 第一次約會');
console.log('decode:' +azdg.decrypt(encode));

沒有留言: