|
| 1 | +/** |
| 2 | + * Created by zhanghe on 15-6-2. |
| 3 | + */ |
| 4 | + |
| 5 | +(function () { |
| 6 | + var tool = window.JsEncrpt || {}; |
| 7 | + |
| 8 | + var base64EncodeChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 9 | + var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 10 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 11 | + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, |
| 12 | + 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, |
| 13 | + 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 14 | + 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, |
| 15 | + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, |
| 16 | + 50, 51, -1, -1, -1, -1, -1); |
| 17 | + |
| 18 | + tool.base64encode = function (str) { //加密 |
| 19 | + var out, i, len; |
| 20 | + var c1, c2, c3; |
| 21 | + len = str.length; |
| 22 | + i = 0; |
| 23 | + out = ""; |
| 24 | + while (i < len) { |
| 25 | + c1 = str.charCodeAt(i++) & 0xff; |
| 26 | + if (i == len) { |
| 27 | + out += base64EncodeChars.charAt(c1 >> 2); |
| 28 | + out += base64EncodeChars.charAt((c1 & 0x3) << 4); |
| 29 | + out += "=="; |
| 30 | + break; |
| 31 | + } |
| 32 | + c2 = str.charCodeAt(i++); |
| 33 | + if (i == len) { |
| 34 | + out += base64EncodeChars.charAt(c1 >> 2); |
| 35 | + out += base64EncodeChars.charAt(((c1 & 0x3) << 4) |
| 36 | + | ((c2 & 0xF0) >> 4)); |
| 37 | + out += base64EncodeChars.charAt((c2 & 0xF) << 2); |
| 38 | + out += "="; |
| 39 | + break; |
| 40 | + } |
| 41 | + c3 = str.charCodeAt(i++); |
| 42 | + out += base64EncodeChars.charAt(c1 >> 2); |
| 43 | + out += base64EncodeChars.charAt(((c1 & 0x3) << 4) | ((c2 & 0xF0) >> 4)); |
| 44 | + out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >> 6)); |
| 45 | + out += base64EncodeChars.charAt(c3 & 0x3F); |
| 46 | + } |
| 47 | + return out; |
| 48 | + }; |
| 49 | + |
| 50 | + tool.base64decode = function (str) { //解密 |
| 51 | + var c1, c2, c3, c4; |
| 52 | + var i, len, out; |
| 53 | + len = str.length; |
| 54 | + i = 0; |
| 55 | + out = ""; |
| 56 | + while (i < len) { |
| 57 | + /* c1 */ |
| 58 | + do { |
| 59 | + c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]; |
| 60 | + } while (i < len && c1 == -1); |
| 61 | + if (c1 == -1) |
| 62 | + break; |
| 63 | + /* c2 */ |
| 64 | + do { |
| 65 | + c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]; |
| 66 | + } while (i < len && c2 == -1); |
| 67 | + if (c2 == -1) |
| 68 | + break; |
| 69 | + out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4)); |
| 70 | + /* c3 */ |
| 71 | + do { |
| 72 | + c3 = str.charCodeAt(i++) & 0xff; |
| 73 | + if (c3 == 61) |
| 74 | + return out; |
| 75 | + c3 = base64DecodeChars[c3]; |
| 76 | + } while (i < len && c3 == -1); |
| 77 | + if (c3 == -1) |
| 78 | + break; |
| 79 | + out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2)); |
| 80 | + /* c4 */ |
| 81 | + do { |
| 82 | + c4 = str.charCodeAt(i++) & 0xff; |
| 83 | + if (c4 == 61) |
| 84 | + return out; |
| 85 | + c4 = base64DecodeChars[c4]; |
| 86 | + } while (i < len && c4 == -1); |
| 87 | + if (c4 == -1) |
| 88 | + break; |
| 89 | + out += String.fromCharCode(((c3 & 0x03) << 6) | c4); |
| 90 | + } |
| 91 | + return out; |
| 92 | + }; |
| 93 | + |
| 94 | + tool.utf16to8 = function (str) { //中文转成字符 |
| 95 | + var out, i, len, c; |
| 96 | + out = ""; |
| 97 | + len = str.length; |
| 98 | + for (i = 0; i < len; i++) { |
| 99 | + c = str.charCodeAt(i); |
| 100 | + if ((c >= 0x0001) && (c <= 0x007F)) { |
| 101 | + out += str.charAt(i); |
| 102 | + } else if (c > 0x07FF) { |
| 103 | + out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F)); |
| 104 | + out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F)); |
| 105 | + out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); |
| 106 | + } else { |
| 107 | + out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F)); |
| 108 | + out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F)); |
| 109 | + } |
| 110 | + } |
| 111 | + return out; |
| 112 | + }; |
| 113 | + |
| 114 | + tool.utf8to16 = function (str) { //字符转成中文 |
| 115 | + var out, i, len, c; |
| 116 | + var char2, char3; |
| 117 | + out = ""; |
| 118 | + len = str.length; |
| 119 | + i = 0; |
| 120 | + while (i < len) { |
| 121 | + c = str.charCodeAt(i++); |
| 122 | + switch (c >> 4) { |
| 123 | + case 0: |
| 124 | + case 1: |
| 125 | + case 2: |
| 126 | + case 3: |
| 127 | + case 4: |
| 128 | + case 5: |
| 129 | + case 6: |
| 130 | + case 7: |
| 131 | + // 0xxxxxxx |
| 132 | + out += str.charAt(i - 1); |
| 133 | + break; |
| 134 | + case 12: |
| 135 | + case 13: |
| 136 | + // 110x xxxx 10xx xxxx |
| 137 | + char2 = str.charCodeAt(i++); |
| 138 | + out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F)); |
| 139 | + break; |
| 140 | + case 14: |
| 141 | + // 1110 xxxx 10xx xxxx 10xx xxxx |
| 142 | + char2 = str.charCodeAt(i++); |
| 143 | + char3 = str.charCodeAt(i++); |
| 144 | + out += String.fromCharCode(((c & 0x0F) << 12) |
| 145 | + | ((char2 & 0x3F) << 6) | ((char3 & 0x3F) << 0)); |
| 146 | + break; |
| 147 | + } |
| 148 | + } |
| 149 | + return out; |
| 150 | + }; |
| 151 | + |
| 152 | + tool.encode = function (str) { |
| 153 | + var result = tool.base64encode(tool.utf16to8(str)); |
| 154 | + return result; |
| 155 | + }; |
| 156 | + tool.decode = function (str) { |
| 157 | + var result = tool.utf8to16(tool.base64decode(str)); |
| 158 | + return result; |
| 159 | + }; |
| 160 | + |
| 161 | + window.JsEncrpt = tool; |
| 162 | +})(); |
0 commit comments