weaver_encrypt.js 2.29 KB
var encryptSeparator = "----weaversecurity----";
var __WEAVEREncrypt__ = (function () {
    return (function () {
        var AES_UUID = null;
        var RSA_PUB = null;
        return {
            weaver_encrypt: function (value) {

                if (!value && !AES_UUID) return value;
                var key = AES_UUID;
                var encodeKey = CryptoJS.enc.Utf8.parse(key);
                value = CryptoJS.enc.Utf8.parse(value);
                var encryptedStr = CryptoJS.AES.encrypt(value, encodeKey, {
                    mode: CryptoJS.mode.ECB,
                    padding: CryptoJS.pad.Pkcs7
                }).toString();
                return encodeURIComponent(encryptedStr)
            },

            getKey: function () {
                var key = AES_UUID;
                //对密钥进行RSA加密处理
                var encrypt = new JSEncrypt();
                encrypt.setPublicKey(RSA_PUB);
                return encodeURIComponent(encrypt.encrypt(key) + "``RSA``");
            },

            initWEAVERCode: function (callback) {
                if (RSA_PUB && AES_UUID) {
                    if (callback) {
                        callback();
                    }
                } else {
                    var uuid = function () {
                        /**
                         * @return {string}
                         */
                        function S4() {
                            return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
                        }

                        return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4()).substr(0, 16);
                    }
                    AES_UUID = uuid();
                    jQuery.ajax({
                        url: "/rsa/weaver.rsa.GetRsaInfo?ts=" + new Date().getTime(),
                        type: "post",
                        dataType: "json",
                        success: function (data) {
                            RSA_PUB = data.rsa_pub;
                            if (callback) {
                                callback();
                            }
                        }
                    });
                }
            }
        }
    })();
})();

jQuery(document).ready(function () {
    __WEAVEREncrypt__.initWEAVERCode();
});