sm4.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. (function(r) {
  2. var e;
  3. if (typeof window !== "undefined") {
  4. e = window;
  5. } else if (typeof global !== "undefined") {
  6. e = global;
  7. } else if (typeof self !== "undefined") {
  8. e = self;
  9. } else {
  10. e = this;
  11. }
  12. e.base64js = r();
  13. })(function() {
  14. var r, e, t;
  15. return (function r(e, t, n) {
  16. function o(i, a) {
  17. if (!t[i]) {
  18. if (!e[i]) {
  19. var u = typeof require == "function" && require;
  20. if (!a && u) return u(i, !0);
  21. if (f) return f(i, !0);
  22. var d = new Error("Cannot find module '" + i + "'");
  23. throw ((d.code = "MODULE_NOT_FOUND"), d);
  24. }
  25. var c = (t[i] = { exports: {} });
  26. e[i][0].call(
  27. c.exports,
  28. function(r) {
  29. var t = e[i][1][r];
  30. return o(t ? t : r);
  31. },
  32. c,
  33. c.exports,
  34. r,
  35. e,
  36. t,
  37. n
  38. );
  39. }
  40. return t[i].exports;
  41. }
  42. var f = typeof require == "function" && require;
  43. for (var i = 0; i < n.length; i++) o(n[i]);
  44. return o;
  45. })(
  46. {
  47. "/": [
  48. function(r, e, t) {
  49. "use strict";
  50. t.byteLength = c;
  51. t.toByteArray = v;
  52. t.fromByteArray = s;
  53. var n = [];
  54. var o = [];
  55. var f = typeof Uint8Array !== "undefined" ? Uint8Array : Array;
  56. var i =
  57. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  58. for (var a = 0, u = i.length; a < u; ++a) {
  59. n[a] = i[a];
  60. o[i.charCodeAt(a)] = a;
  61. }
  62. o["-".charCodeAt(0)] = 62;
  63. o["_".charCodeAt(0)] = 63;
  64. function d(r) {
  65. var e = r.length;
  66. if (e % 4 > 0) {
  67. throw new Error("Invalid string. Length must be a multiple of 4");
  68. }
  69. return r[e - 2] === "=" ? 2 : r[e - 1] === "=" ? 1 : 0;
  70. }
  71. function c(r) {
  72. return (r.length * 3) / 4 - d(r);
  73. }
  74. function v(r) {
  75. var e, t, n, i, a;
  76. var u = r.length;
  77. i = d(r);
  78. a = new f((u * 3) / 4 - i);
  79. t = i > 0 ? u - 4 : u;
  80. var c = 0;
  81. for (e = 0; e < t; e += 4) {
  82. n =
  83. (o[r.charCodeAt(e)] << 18) |
  84. (o[r.charCodeAt(e + 1)] << 12) |
  85. (o[r.charCodeAt(e + 2)] << 6) |
  86. o[r.charCodeAt(e + 3)];
  87. a[c++] = (n >> 16) & 255;
  88. a[c++] = (n >> 8) & 255;
  89. a[c++] = n & 255;
  90. }
  91. if (i === 2) {
  92. n = (o[r.charCodeAt(e)] << 2) | (o[r.charCodeAt(e + 1)] >> 4);
  93. a[c++] = n & 255;
  94. } else if (i === 1) {
  95. n =
  96. (o[r.charCodeAt(e)] << 10) |
  97. (o[r.charCodeAt(e + 1)] << 4) |
  98. (o[r.charCodeAt(e + 2)] >> 2);
  99. a[c++] = (n >> 8) & 255;
  100. a[c++] = n & 255;
  101. }
  102. return a;
  103. }
  104. function l(r) {
  105. return (
  106. n[(r >> 18) & 63] +
  107. n[(r >> 12) & 63] +
  108. n[(r >> 6) & 63] +
  109. n[r & 63]
  110. );
  111. }
  112. function h(r, e, t) {
  113. var n;
  114. var o = [];
  115. for (var f = e; f < t; f += 3) {
  116. n = (r[f] << 16) + (r[f + 1] << 8) + r[f + 2];
  117. o.push(l(n));
  118. }
  119. return o.join("");
  120. }
  121. function s(r) {
  122. var e;
  123. var t = r.length;
  124. var o = t % 3;
  125. var f = "";
  126. var i = [];
  127. var a = 16383;
  128. for (var u = 0, d = t - o; u < d; u += a) {
  129. i.push(h(r, u, u + a > d ? d : u + a));
  130. }
  131. if (o === 1) {
  132. e = r[t - 1];
  133. f += n[e >> 2];
  134. f += n[(e << 4) & 63];
  135. f += "==";
  136. } else if (o === 2) {
  137. e = (r[t - 2] << 8) + r[t - 1];
  138. f += n[e >> 10];
  139. f += n[(e >> 4) & 63];
  140. f += n[(e << 2) & 63];
  141. f += "=";
  142. }
  143. i.push(f);
  144. return i.join("");
  145. }
  146. },
  147. {}
  148. ]
  149. },
  150. {},
  151. []
  152. )("/");
  153. });
  154. /**
  155. * 国密SM4加密算法
  156. * @author Mr.Xiao
  157. * @email 97041437@qq.com
  158. * @company GDT-ZWZX-DEV-PT
  159. * @date 2017-08
  160. */
  161. var sm4 = {}
  162. window.mobileKey = "JeF8U9wHFOMfs2Y8";
  163. const encrypt=1;
  164. const decrypt=0;
  165. const round=32;
  166. const block=16;
  167. var sbox = [0xd6,0x90,0xe9,0xfe,0xcc,0xe1,0x3d,0xb7,0x16,0xb6,0x14,0xc2,0x28,0xfb,0x2c,0x05,
  168. 0x2b,0x67,0x9a,0x76,0x2a,0xbe,0x04,0xc3,0xaa,0x44,0x13,0x26,0x49,0x86,0x06,0x99,
  169. 0x9c,0x42,0x50,0xf4,0x91,0xef,0x98,0x7a,0x33,0x54,0x0b,0x43,0xed,0xcf,0xac,0x62,
  170. 0xe4,0xb3,0x1c,0xa9,0xc9,0x08,0xe8,0x95,0x80,0xdf,0x94,0xfa,0x75,0x8f,0x3f,0xa6,
  171. 0x47,0x07,0xa7,0xfc,0xf3,0x73,0x17,0xba,0x83,0x59,0x3c,0x19,0xe6,0x85,0x4f,0xa8,
  172. 0x68,0x6b,0x81,0xb2,0x71,0x64,0xda,0x8b,0xf8,0xeb,0x0f,0x4b,0x70,0x56,0x9d,0x35,
  173. 0x1e,0x24,0x0e,0x5e,0x63,0x58,0xd1,0xa2,0x25,0x22,0x7c,0x3b,0x01,0x21,0x78,0x87,
  174. 0xd4,0x00,0x46,0x57,0x9f,0xd3,0x27,0x52,0x4c,0x36,0x02,0xe7,0xa0,0xc4,0xc8,0x9e,
  175. 0xea,0xbf,0x8a,0xd2,0x40,0xc7,0x38,0xb5,0xa3,0xf7,0xf2,0xce,0xf9,0x61,0x15,0xa1,
  176. 0xe0,0xae,0x5d,0xa4,0x9b,0x34,0x1a,0x55,0xad,0x93,0x32,0x30,0xf5,0x8c,0xb1,0xe3,
  177. 0x1d,0xf6,0xe2,0x2e,0x82,0x66,0xca,0x60,0xc0,0x29,0x23,0xab,0x0d,0x53,0x4e,0x6f,
  178. 0xd5,0xdb,0x37,0x45,0xde,0xfd,0x8e,0x2f,0x03,0xff,0x6a,0x72,0x6d,0x6c,0x5b,0x51,
  179. 0x8d,0x1b,0xaf,0x92,0xbb,0xdd,0xbc,0x7f,0x11,0xd9,0x5c,0x41,0x1f,0x10,0x5a,0xd8,
  180. 0x0a,0xc1,0x31,0x88,0xa5,0xcd,0x7b,0xbd,0x2d,0x74,0xd0,0x12,0xb8,0xe5,0xb4,0xb0,
  181. 0x89,0x69,0x97,0x4a,0x0c,0x96,0x77,0x7e,0x65,0xb9,0xf1,0x09,0xc5,0x6e,0xc6,0x84,
  182. 0x18,0xf0,0x7d,0xec,0x3a,0xdc,0x4d,0x20,0x79,0xee,0x5f,0x3e,0xd7,0xcb,0x39,0x48];
  183. var ck = [
  184. 0x00070e15, 0x1c232a31, 0x383f464d, 0x545b6269,
  185. 0x70777e85, 0x8c939aa1, 0xa8afb6bd, 0xc4cbd2d9,
  186. 0xe0e7eef5, 0xfc030a11, 0x181f262d, 0x343b4249,
  187. 0x50575e65, 0x6c737a81, 0x888f969d, 0xa4abb2b9,
  188. 0xc0c7ced5, 0xdce3eaf1, 0xf8ff060d, 0x141b2229,
  189. 0x30373e45, 0x4c535a61, 0x686f767d, 0x848b9299,
  190. 0xa0a7aeb5, 0xbcc3cad1, 0xd8dfe6ed, 0xf4fb0209,
  191. 0x10171e25, 0x2c333a41, 0x484f565d, 0x646b7279];
  192. var rotl = function(x,y){
  193. return x<<y|x>>>(32-y);
  194. }
  195. var byteSub = function(a){
  196. return (sbox[a>>>24&0xFF]&0xFF)<<24|(sbox[a>>>16&0xFF]&0xFF)<<16|(sbox[a>>>8&0xFF]&0xFF)<<8|(sbox[a&0xFF]&0xFF);
  197. }
  198. var l1 = function(b){
  199. return b^rotl(b,2)^rotl(b,10)^rotl(b,18)^rotl(b,24);
  200. }
  201. var l2 = function(b){
  202. return b^rotl(b,13)^rotl(b,23);
  203. }
  204. var sms4Crypt = function(input,output,rk){
  205. var r,mid,x0,x1,x2,x3;
  206. var x = new Array(4);
  207. var tmp = new Array(4);
  208. for(var i=0;i<4;i++)
  209. {
  210. tmp[0]=input[0+4*i]&0xff;
  211. tmp[1]=input[1+4*i]&0xff;
  212. tmp[2]=input[2+4*i]&0xff;
  213. tmp[3]=input[3+4*i]&0xff;
  214. x[i]=tmp[0]<<24|tmp[1]<<16|tmp[2]<<8|tmp[3];
  215. }
  216. for (r = 0; r < 32; r += 4) {
  217. mid = x[1] ^ x[2] ^ x[3] ^ rk[r + 0];
  218. mid = byteSub(mid);
  219. x[0] = x[0] ^ l1(mid);
  220. mid = x[2] ^ x[3] ^ x[0] ^ rk[r + 1];
  221. mid = byteSub(mid);
  222. x[1] = x[1] ^ l1(mid);
  223. mid = x[3] ^ x[0] ^ x[1] ^ rk[r + 2];
  224. mid = byteSub(mid);
  225. x[2] = x[2] ^ l1(mid);
  226. mid = x[0] ^ x[1] ^ x[2] ^ rk[r + 3];
  227. mid = byteSub(mid);
  228. x[3] = x[3] ^ l1(mid);
  229. }
  230. for (var j = 0; j < 16; j += 4) {
  231. output[j] = (x[3 - j / 4] >>> 24 & 0xFF);
  232. output[j + 1] = (x[3 - j / 4] >>> 16 & 0xFF);
  233. output[j + 2] = (x[3 - j / 4] >>> 8 & 0xFF);
  234. output[j + 3] = (x[3 - j / 4] & 0xFF);
  235. }
  236. }
  237. var sms4KeyExt = function(key,rk,flag){
  238. var r,mid;
  239. var x= new Array();
  240. var tmp =new Array(4);
  241. for(var i=0;i<4;i++)
  242. {
  243. tmp[0] = key[0+4*i]&0xFF;
  244. tmp[1] = key[1+4*i]&0xff;
  245. tmp[2] = key[2+4*i]&0xff;
  246. tmp[3] = key[3+4*i]&0xff;
  247. x[i]=tmp[0]<<24|tmp[1]<<16|tmp[2]<<8|tmp[3];
  248. }
  249. x[0]^=0xa3b1bac6;
  250. x[1]^=0x56aa3350;
  251. x[2]^=0x677d9197;
  252. x[3]^=0xb27022dc;
  253. for(r=0;r<32;r+=4)
  254. {
  255. mid=x[1]^x[2]^x[3]^ck[r+0];
  256. mid=byteSub(mid);
  257. rk[r+0]=x[0]^=l2(mid);
  258. mid=x[2]^x[3]^x[0]^ck[r+1];
  259. mid=byteSub(mid);
  260. rk[r+1]=x[1]^=l2(mid);
  261. mid=x[3]^x[0]^x[1]^ck[r+2];
  262. mid=byteSub(mid);
  263. rk[r+2]=x[2]^=l2(mid);
  264. mid=x[0]^x[1]^x[2]^ck[r+3];
  265. mid=byteSub(mid);
  266. rk[r+3]=x[3]^=l2(mid);
  267. }
  268. if(flag === decrypt)
  269. {
  270. for(r=0;r<16;r++)
  271. {
  272. mid=rk[r];
  273. rk[r]=rk[31-r];
  274. rk[31-r]=mid;
  275. }
  276. }
  277. }
  278. var _gencode =function(d,flag){
  279. var point=0;
  280. var inLen = 16;
  281. var round_key=new Array(round);
  282. sms4KeyExt(d.key,round_key,flag);
  283. var input = new Array(16);
  284. var output = new Array(16);
  285. var out = [];
  286. while(inLen >= block)
  287. {
  288. input=d.source.slice(point,point+16);
  289. sms4Crypt(input,output,round_key);
  290. out = out.concat(output);
  291. inLen-=block;
  292. point+=block;
  293. }
  294. return out;
  295. };
  296. sm4._gencode = _gencode;
  297. function stringToByte(str) {
  298. var bytes = new Array();
  299. var len, c;
  300. len = str.length;
  301. for(var i = 0; i < len; i++) {
  302. c = str.charCodeAt(i);
  303. if(c >= 0x010000 && c <= 0x10FFFF) {
  304. bytes.push(((c >> 18) & 0x07) | 0xF0);
  305. bytes.push(((c >> 12) & 0x3F) | 0x80);
  306. bytes.push(((c >> 6) & 0x3F) | 0x80);
  307. bytes.push((c & 0x3F) | 0x80);
  308. } else if(c >= 0x000800 && c <= 0x00FFFF) {
  309. bytes.push(((c >> 12) & 0x0F) | 0xE0);
  310. bytes.push(((c >> 6) & 0x3F) | 0x80);
  311. bytes.push((c & 0x3F) | 0x80);
  312. } else if(c >= 0x000080 && c <= 0x0007FF) {
  313. bytes.push(((c >> 6) & 0x1F) | 0xC0);
  314. bytes.push((c & 0x3F) | 0x80);
  315. } else {
  316. bytes.push(c & 0xFF);
  317. }
  318. }
  319. return bytes;
  320. }
  321. function byteToString(arr) {
  322. if(typeof arr === 'string') {
  323. return arr;
  324. }
  325. var str = '',
  326. _arr = arr;
  327. for(var i = 0; i < _arr.length; i++) {
  328. var one = _arr[i].toString(2),
  329. v = one.match(/^1+?(?=0)/);
  330. if(v && one.length == 8) {
  331. var bytesLength = v[0].length;
  332. var store = _arr[i].toString(2).slice(7 - bytesLength);
  333. for(var st = 1; st < bytesLength; st++) {
  334. store += _arr[st + i].toString(2).slice(2);
  335. }
  336. str += String.fromCharCode(parseInt(store, 2));
  337. i += bytesLength - 1;
  338. } else {
  339. str += String.fromCharCode(_arr[i]);
  340. }
  341. }
  342. return str;
  343. }
  344. sm4.encodeNoKey = function(d){
  345. var input = stringToByte(d);
  346. var key =stringToByte(mobileKey);
  347. var c = [];
  348. for(var i=0;i<input.length;i+=16){
  349. c = c.concat(_gencode({
  350. source : input.slice(i,i+16),
  351. key : key
  352. },encrypt));
  353. }
  354. return base64js.fromByteArray(c);
  355. }
  356. sm4.encode = function(d){
  357. var input = stringToByte(d.input);
  358. var key = stringToByte(d.key);
  359. var c = [];
  360. for(var i=0;i<input.length;i+=16){
  361. c = c.concat(_gencode({
  362. source : input.slice(i,i+16),
  363. key : key
  364. },encrypt));
  365. }
  366. return base64js.fromByteArray(c);
  367. }
  368. sm4.decode = function(d){
  369. var input = base64js.toByteArray(d.input);
  370. var key = stringToByte(d.key);
  371. var c = [];
  372. for(var i=0;i<input.length;i+=16){
  373. c = c.concat(_gencode({
  374. source : input.slice(i,i+16),
  375. key : key
  376. },decrypt));
  377. }
  378. return byteToString(c).replace(/(\s*$)/g, "");
  379. }
  380. sm4.decodeNoKey = function(d){
  381. var input = base64js.toByteArray(d);
  382. var key = stringToByte(mobileKey);
  383. var c = [];
  384. for(var i=0;i<input.length;i+=16){
  385. c = c.concat(_gencode({
  386. source : input.slice(i,i+16),
  387. key : key
  388. },decrypt));
  389. }
  390. return byteToString(c).replace(/(\s*$)/g, "");
  391. }
  392. export default sm4