首页 > 编程语言 >前端加密算法之SM4

前端加密算法之SM4

时间:2022-09-28 11:11:36浏览次数:63  
标签:function return 16 前端 SM4 length var 加密算法 255

1、简介 

  1.1、国产加密算法,是一个分组算法,该算法的分组长度为128bit,密钥长度为128bit,SM4算法与AES算法具有相同的密钥长度分组长度128比特,因此在安全性上高于3DES算法

  1.2、经过SM4加密的明文都需要经过32轮变换后变换为密文

2、实现

  在项目中,我是直接执行的js,然后对js执行的调用,其中密钥是其他接口的响应体参数,这里先给出js

  1 var each = function(object, callback) {
  2     var type = (function() {
  3         switch (object.constructor) {
  4             case Object:
  5                 return 'Object';
  6                 break;
  7             case Array:
  8                 return 'Array';
  9                 break;
 10             case NodeList:
 11                 return 'NodeList';
 12                 break;
 13             default:
 14                 return 'null';
 15                 break;
 16         }
 17     })();
 18     if (type === 'Array' || type === 'NodeList') {
 19         [].every.call(object, function(v, i) {
 20             return callback.call(v, i, v) === false ? false : true;
 21         });
 22     } else if (type === 'Object') {
 23         for (var i in object) {
 24             if (callback.call(object[i], i, object[i]) === false) {
 25                 break;
 26             }
 27         }
 28     }
 29 }
 30 
 31 sm4 = function(r) {
 32     function n(e) {
 33         if (t[e]) return t[e].exports;
 34         var o = t[e] = {
 35             i: e,
 36             l: !1,
 37             exports: {}
 38         };
 39         return r[e].call(o.exports, o, o.exports, n),
 40         o.l = !0,
 41         o.exports
 42     }
 43     var t = {};
 44     return n.m = r,
 45     n.c = t,
 46     n.d = function(r, t, e) {
 47         n.o(r, t) || Object.defineProperty(r, t, {
 48             configurable: !1,
 49             enumerable: !0,
 50             get: e
 51         })
 52     },
 53     n.n = function(r) {
 54         var t = r && r.__esModule ? function() {
 55                 return r['default']
 56             } : function() {
 57                 return r
 58             };
 59         return n.d(t, "a", t),
 60         t
 61     },
 62     n.o = function(r, n) {
 63         return Object.prototype.hasOwnProperty.call(r, n)
 64     },
 65     n.p = "",
 66     n(n.s = 8)
 67 }({
 68     8: function(r, n, t) {
 69         "use strict";
 70 
 71         function e(r) {
 72             if (Array.isArray(r)) {
 73                 for (var n = 0, t = Array(r.length); n < r.length; n++)
 74                 t[n] = r[n];
 75                 return t
 76             }
 77             return Array.from(r)
 78         }
 79 
 80         function o(r) {
 81             for (var n = [], t = 0, e = r.length; t < e; t += 2)
 82             n.push(parseInt(r.substr(t, 2), 16));
 83             return n
 84         }
 85 
 86         function u(r) {
 87             var temp = "";
 88             each(r, function(i, p) {
 89                 temp = temp + (p = p.toString(16),
 90                 1 === p.length ? "0" + p : p);
 91             });
 92             return temp;
 93         }
 94 
 95         function i(r) {
 96             for (var n = [], t = 0, e = r.length; t < e; t++) {
 97                 var o = r.charCodeAt(t);
 98                 o <= 127 ? n.push(o) : o <= 2047 ? (n.push(192 | o >>> 6),
 99                 n.push(128 | 63 & o)) : (n.push(224 | o >>> 12),
100                 n.push(128 | o >>> 6 & 63),
101                 n.push(128 | 63 & o))
102             }
103             return n
104         }
105 
106         function f(r) {
107             for (var n = [], t = 0, e = r.length; t < e; t++)
108             r[t] >= 224 && r[t] <= 239 ? (n.push(String.fromCharCode(((15 & r[t]) << 12) + ((63 & r[t + 1]) << 6) + (63 & r[t + 2]))),
109             t += 2) : r[t] >= 192 && r[t] <= 223 ? (n.push(String.fromCharCode(((31 & r[t]) << 6) + (63 & r[t + 1]))),
110             t++) : n.push(String.fromCharCode(r[t]));
111             return n.join("")
112         }
113 
114         function a(r, n) {
115             return r << n | r >>> 32 - n
116         }
117 
118         function c(r) {
119             return (255 & w[r >>> 24 & 255]) << 24 | (255 & w[r >>> 16 & 255]) << 16 | (255 & w[r >>> 8 & 255]) << 8 | 255 & w[255 & r]
120         }
121 
122         function s(r) {
123             return r ^ a(r, 2) ^ a(r, 10) ^ a(r, 18) ^ a(r, 24)
124         }
125 
126         function p(r) {
127             return r ^ a(r, 13) ^ a(r, 23)
128         }
129 
130         function h(r, n, t) {
131             for (var e = new Array(4), o = new Array(4), u = 0; u < 4; u++)
132             o[0] = 255 & r[0 + 4 * u],
133             o[1] = 255 & r[1 + 4 * u],
134             o[2] = 255 & r[2 + 4 * u],
135             o[3] = 255 & r[3 + 4 * u],
136             e[u] = o[0] << 24 | o[1] << 16 | o[2] << 8 | o[3];
137             for (var i, f = 0; f < 32; f += 4)
138             i = e[1] ^ e[2] ^ e[3] ^ t[f + 0],
139             e[0] ^= s(c(i)),
140             i = e[2] ^ e[3] ^ e[0] ^ t[f + 1],
141             e[1] ^= s(c(i)),
142             i = e[3] ^ e[0] ^ e[1] ^ t[f + 2],
143             e[2] ^= s(c(i)),
144             i = e[0] ^ e[1] ^ e[2] ^ t[f + 3],
145             e[3] ^= s(c(i));
146             for (var a = 0; a < 16; a += 4)
147             n[a] = e[3 - a / 4] >>> 24 & 255,
148             n[a + 1] = e[3 - a / 4] >>> 16 & 255,
149             n[a + 2] = e[3 - a / 4] >>> 8 & 255,
150             n[a + 3] = 255 & e[3 - a / 4]
151         }
152 
153         function v(r, n, t) {
154             for (var e = new Array(4), o = new Array(4), u = 0; u < 4; u++)
155             o[0] = 255 & r[0 + 4 * u],
156             o[1] = 255 & r[1 + 4 * u],
157             o[2] = 255 & r[2 + 4 * u],
158             o[3] = 255 & r[3 + 4 * u],
159             e[u] = o[0] << 24 | o[1] << 16 | o[2] << 8 | o[3];
160             e[0] ^= 2746333894,
161             e[1] ^= 1453994832,
162             e[2] ^= 1736282519,
163             e[3] ^= 2993693404;
164             for (var i, f = 0; f < 32; f += 4)
165             i = e[1] ^ e[2] ^ e[3] ^ A[f + 0],
166             n[f + 0] = e[0] ^= p(c(i)),
167             i = e[2] ^ e[3] ^ e[0] ^ A[f + 1],
168             n[f + 1] = e[1] ^= p(c(i)),
169             i = e[3] ^ e[0] ^ e[1] ^ A[f + 2],
170             n[f + 2] = e[2] ^= p(c(i)),
171             i = e[0] ^ e[1] ^ e[2] ^ A[f + 3],
172             n[f + 3] = e[3] ^= p(c(i));
173             if (t === g) for (var a, s = 0; s < 16; s++)
174             a = n[s],
175             n[s] = n[31 - s],
176             n[31 - s] = a
177         }
178 
179         function l(r, n, t) {
180             var a = arguments.length > 3 && void 0 !== arguments[3] ? arguments[3] : {}, c = a.padding,
181                 s = void 0 === c ? "pkcs#5" : c,
182                 p = (a.mode,
183                 a.output),
184                 l = void 0 === p ? "string" : p;
185             if ("string" == typeof n && (n = o(n)),
186             16 !== n.length) throw new Error("key is invalid");
187             if (r = "string" == typeof r ? t !== g ? i(r) : o(r) : [].concat(e(r)),
188                 "pkcs#5" === s && t !== g) for (var w = d - r.length % d, A = 0; A < w; A++)
189             r.push(w);
190             var m = new Array(y);
191             v(n, m, t);
192             for (var C = [], x = r.length, b = 0; x >= d;) {
193                 var j = r.slice(b, b + 16),
194                     k = new Array(16);
195                 h(j, k, m);
196                 for (var S = 0; S < d; S++)
197                 C[b + S] = k[S];
198                 x -= d,
199                 b += d
200             }
201             if ("pkcs#5" === s && t === g) {
202                 var O = C[C.length - 1];
203                 C.splice(C.length - O, O)
204             }
205             return "array" !== l ? t !== g ? u(C) : f(C) : C
206         }
207         var g = 0,
208             y = 32,
209             d = 16,
210             w = [214, 144, 233, 254, 204, 225, 61, 183, 22, 182, 20, 194, 40, 251, 44, 5, 43, 103, 154, 118, 42, 190, 4, 195, 170, 68, 19, 38, 73, 134, 6, 153, 156, 66, 80, 244, 145, 239, 152, 122, 51, 84, 11, 67, 237, 207, 172, 98, 228, 179, 28, 169, 201, 8, 232, 149, 128, 223, 148, 250, 117, 143, 63, 166, 71, 7, 167, 252, 243, 115, 23, 186, 131, 89, 60, 25, 230, 133, 79, 168, 104, 107, 129, 178, 113, 100, 218, 139, 248, 235, 15, 75, 112, 86, 157, 53, 30, 36, 14, 94, 99, 88, 209, 162, 37, 34, 124, 59, 1, 33, 120, 135, 212, 0, 70, 87, 159, 211, 39, 82, 76, 54, 2, 231, 160, 196, 200, 158, 234, 191, 138, 210, 64, 199, 56, 181, 163, 247, 242, 206, 249, 97, 21, 161, 224, 174, 93, 164, 155, 52, 26, 85, 173, 147, 50, 48, 245, 140, 177, 227, 29, 246, 226, 46, 130, 102, 202, 96, 192, 41, 35, 171, 13, 83, 78, 111, 213, 219, 55, 69, 222, 253, 142, 47, 3, 255, 106, 114, 109, 108, 91, 81, 141, 27, 175, 146, 187, 221, 188, 127, 17, 217, 92, 65, 31, 16, 90, 216, 10, 193, 49, 136, 165, 205, 123, 189, 45, 116, 208, 18, 184, 229, 180, 176, 137, 105, 151, 74, 12, 150, 119, 126, 101, 185, 241, 9, 197, 110, 198, 132, 24, 240, 125, 236, 58, 220, 77, 32, 121, 238, 95, 62, 215, 203, 57, 72],
211             A = [462357, 472066609, 943670861, 1415275113, 1886879365, 2358483617, 2830087869, 3301692121, 3773296373, 4228057617, 404694573, 876298825, 1347903077, 1819507329, 2291111581, 2762715833, 3234320085, 3705924337, 4177462797, 337322537, 808926789, 1280531041, 1752135293, 2223739545, 2695343797, 3166948049, 3638552301, 4110090761, 269950501, 741554753, 1213159005, 1684763257];
212         r.exports = {
213             encrypt: function(r, n, t) {
214                 return l(r, n, 1, t)
215             },
216             decrypt: function(r, n, t) {
217                 return l(r, n, 0, t)
218             }
219         }
220     }
221 });
222 
223 function get_pointJson(coordinate,secretKey){
224     var result = sm4.encrypt(coordinate, secretKey);
225     return result;
226 }
227 
228 function get_captchCode(token,coordinate,secretKey) {
229     var message = token + "---" + coordinate;
230     var result = sm4.encrypt(message, secretKey);
231     return result;
232 }

后来我又发现python也有算法库支持实现国密

gmssl:开源的加密算法库,支持SM2/SM3/SM4等国密(国家商用密码)算法,pip install gmssl,这里仅列出ECB模式的加密

 1 from gmssl import sm4
 2 
 3 
 4 def encrypt(key, password):
 5     """
 6 
 7     Args:
 8         key: 密钥
 9         password:待加密文本
10 
11     Returns:
12 
13     """
14     # 实例加密对象
15     sm4_obj = sm4.CryptSM4()
16     # 设置密钥
17     sm4_obj.set_key(key.encode(), sm4.SM4_ENCRYPT)
18     encrypt_val = sm4_obj.crypt_ecb(str(password).encode())
19     return encrypt_val.hex()

3、调用

  输入密钥和待加密字符串

标签:function,return,16,前端,SM4,length,var,加密算法,255
From: https://www.cnblogs.com/shixiaogu/p/16737297.html

相关文章

  • 10个前端开发人员必须知道的CSS框架
    英文| https://www.geeksforgeeks.org/10-best-css-frameworks-for-frontend-developers-in-2020/?ref=rp翻译| web前端开发(ID:web_qdkf)对于UI/UX设计人员而言,要制作......
  • 12个功能强大的前端开发人员必备的在线工具,强烈推荐!
    英文| https://blog.bitsrc.io/12-useful-online-tools-for-frontend-developers-bf98f3bf7c63翻译|杨小二互联网上充满着各种出色的工具,这些工具使我们这些前端开发人......
  • 前端富文本编辑器总结
    1.Tinymce文档: TinyMCE中文文档中文手册(ax-z.cn)最强富文本编辑器,基础版demo代码实现比较容易,非常多的个性化配置,支持非常丰富的插件,大型项目首推 2. UEditor  ......
  • 前端加密算法之SHA1
    1、简介和前篇所讲的MD5加密算法一样,都属于哈希算法,尽管安全性要高于MD5,但运算速度要比MD5慢2、实现因为同属于哈希算法,所以也可以使用hashlib库实现1impo......
  • 地址识别(纯前端)
    最近有一个需求,是要在前端做一个地址识别功能。。。确认不交给后台调接口去用ai识别吗?看需求图:(识别出来的地址要给勾选上,这是个四级联动)   说一下解决方案:先将输......
  • 前端基础面试题
    1.前端如何进行性能优化?前端进行性能优化的方案很多,这里只列举部分。在实际应用中不要贪多,想着都用上,要对网站的主要用户群体进行针对性优化。降低请求量合并资源,......
  • 前端重点复习笔记
    JavaScriptJS特点不主动开启新的线程的情况下单线程如何开启新线程varworker=newWorker('js文件路径')js是弱类型语言所有的声明都是通过var在赋值的时......
  • 前端生成二维码
    往常我们二维码可能都是后端去处理,构建一个image对象,最后输出image二进制流,然后前端定义一个img控件去展示。但这有个弊端,就是流传输的比较大,当流量大的时候,不仅浪费流量......
  • 【前端必会】使用indexedDB,降低环境搭建成本
    背景学习前端新框架、新技术。如果需要做一些数据库的操作来增加demo的体验(CURD流程可以让演示的体验根据丝滑)最开始的时候一个演示程序我们会调用后台,这样其实有一点弊......
  • 前端三件套 HTML+CSS+JS基础知识内容笔记
    HTML基础目录HTML基础HTML5标签doctype标签html标签head标签meta标签title标签body标签文本和超链接标签标题标签段落标签换行标签水平标签强调标签图片标签与超链接标签......