juneandgreen / sm-crypto Goto Github PK
View Code? Open in Web Editor NEW国密算法js版
License: MIT License
国密算法js版
License: MIT License
你好:
请问一下webpack babel-polyfill 打包后ios10 (含ios10)以下系统不能使用,是我什么地方配置有误么?
用sm3计算100kb的内容差不多要1秒多,这个怎么优化?
现在看说明公钥和私钥是通过generateKeyPairHex生成的,有办法从pem文件或者p12之类的文件中读取到吗?
这个我不知道是加密导致的,还是我base64或者buffer等转换中导致。pdf加密,然后以表单上传到后端,下载,解密,生成pdf.
解密下载后的pdf可以打开,但是会提醒我修改了,用ultraEdit查看,最后一行EOF消失了。
这个issue可以打个question标签
doDecrypt似乎期待解码出来的是utf8的数据,如果明文是二进制的,会报错 Malformed UTF-8 data
。
有办法开选项返回个Buffer、Uint8Array之类的吗,还是我应该直接改代码?
`let sigValueHex6 = sm2.doSignature("这是加密123456", privateKeyWeb, {
hash: true,
publicKeyWeb,
userId: '1234567812345678',
})
console.log(sigValueHex6);
let verifyResult6 = sm2.doVerifySignature("这是加密123456", sigValueHex6, publicKeyWeb, {
hash: true,
userId: '1234567812345678',
})
console.log(verifyResult6);`
错误如下 :
[Running] node "d:\Workspace\newProject\aggregate\tianjinfarmer\src\service\tempCodeRunnerFile.js"
0897d7bf791a538f32e5d18a17f5b4578c9434800d884c89d554ebbcbded056708adb68956f3d74baa80d3d1d3e57211158b2d0960c27678ed126d45a4059cc7
D:\Workspace\newProject\aggregate\tianjinfarmer\node_modules_sm-crypto@0.2.1@sm-crypto\src\sm2\index.js:154
const x1y1 = G.multiply(s).add(PA.multiply(t))
^
TypeError: Cannot read property 'multiply' of null
at Object.doVerifySignature (D:\Workspace\newProject\aggregate\tianjinfarmer\node_modules_sm-crypto@0.2.1@sm-crypto\src\sm2\index.js:154:37)
at Object. (d:\Workspace\newProject\aggregate\tianjinfarmer\src\service\tempCodeRunnerFile.js:36:25)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
这个问题很懵啊 所有的签名都能过去 但就是 验签 过不了 都会报** Cannot read property 'multiply' of null**
var sm2 = require('sm-crypto').sm2;
var sm3 = require('sm-crypto').sm3;
var sm4 = require('sm-crypto').sm4;
var assert = require('assert');
var plainMsg = 'hello world!';
var keypair = sm2.generateKeyPairHex();
console.log('keypair:' + keypair);
var i = 0
for (;;) {
i++;
if (i == 1000) {
i = 0;
}
var encryptData = sm2.doEncrypt(plainMsg, keypair.publicKey);
console.log('encrypt result:' + encryptData);
var decryptData = sm2.doDecrypt(encryptData, keypair.privateKey);
console.log('decrypt result:' + decryptData);
assert.equal(plainMsg == decryptData, true, 'error');
// var hashResult = sm3(plainMsg);
// console.log('sm3 hash:' + hashResult);
}
createPoint(x, y) {
let publicKey = '04' + x + y;
let point = _.getGlobalCurve().decodePointHex(publicKey);
return point;
}
SM2自己签名验签应该没啥问题,但是和别的语言是不通的
目前很多后端语言的SM4算法都已支持CBC模式, 比如: java的bouncycastle
期待作者支持一下SM4的CBC模式加解密
通过npm install @types/sm-crypto --save
安装成功,文件中也成功import,但是项目启动的时候报错:
Module not found: Error: Can't resolve 'sm-crypto' in 'D:\idea workspace\XXX\src\app'
angular 2+和react 项目都一样。
后端我用的bouncycastle,有时候生成的密钥能解开,有时候就解不开。
发现一个现象, 前端用sm4加密, 原文包含emoji表情,以字符串形式传入, 后端解密发现emoji表情信息为乱码, 其余内容正常
前端自己加密, 自己解密, emoji正常;
后端对相同原文自己加密自己解密, emoji正常;
不进行加解密, emoji正常;
调试了一下发现, 如果原文入参转成数组, 比如 Buffer.from("😃"), 后端可以正常处理,
对后端包含emoji表情的密文, 解密后以数组输出, 再用Buffer.from("解密后的数组").toString(), 也可以正常显示了
感觉 utf8ToArray()以及arrayToUtf8()这两个函数对于emoji的处理需要优化支持一下
非前端开发, 能力有限, 只是找到了一个暂时解决的办法.
sm-crypto:0.3.6
后端采用java 公钥进行sm2加密,前端使用私钥进行解密。
前端解密时,计算出c3与checkC3校验时因字母大小写,导致验证失败。无法解密。
前端js加解密无此问题。
应加上字符串转为小写,判断是否一致。
104行
if (checkC3 === c3) { #此处修改为 if (checkC3.toLowerCase() === c3.toLowerCase() ) {
return output === 'array' ? msg : _.arrayToUtf8(msg)
} else {
return output === 'array' ? [] : ''
}
ec.js 源码引用到工程使用报错,将88行代码修改为 this.z = z === undefined ? BigInteger.ONE : z
后即可正常使用。
前端用Java后端传递的16进制公钥正常加解密,后端拿到前端给的密文无法解密。
但是用https://github.com/Saberization/SM2所生成的密文,后端可以正常解密。但是没有前端解密的方法。
密文是 16进制格式
sm2 前端去掉 04 后端加上 04 可以相互加解密,sm3 加密结果也是一致的。但是进行 sm4 加解密时候就出现没办法解密的问题,前端传给后端的代码后端解密不了,前端解后端的加密最后也输出的空......最后又找了一个 gm-crypto 解密的。请问这是什么原因啊 QAQ
sm4加密的时候如果需要加密的串太短且当padding为none时不进入加密循环 while (restLen >= BLOCK),使得outArray为空,加密出来为空
ERROR in static-apph5/js/vendor.386ad02019772f8f622f.js from UglifyJs
Unexpected token name «i», expected punc «;» [./node_modules/sm-crypto/src/sm4/index.js:41,0][static-apph5/js/vendor.386ad02019772f8f622f.js:128191,11]
建议webpack中的 libraryTarget: 'window',改成 umd
这样支持多种前端的模块使用场景
2222
function doSignature(msg, privateKey, { pointPool, der, hash, publicKey } = {}) {
let hashHex = typeof msg === 'string' ? _.parseUtf8StringToHex(msg) : _.parseArrayBufferToHex(msg);
if (hash) {
// sm3杂凑
let publicKey = publicKey || getPublicKeyFromPrivateKey(privateKey);
hashHex = doSm3Hash(hashHex, publicKey);
}
修改为:
function doSignature(msg, privateKey, { pointPool, der, hash, publicKey } = {}) {
let hashHex = typeof msg === 'string' ? _.parseUtf8StringToHex(msg) : _.parseArrayBufferToHex(msg);
if (hash) {
// sm3杂凑
let pubKey= publicKey || getPublicKeyFromPrivateKey(privateKey);
hashHex = doSm3Hash(hashHex, pubKey);
}
dist 中SM2随机出错,加密的数据 别的加密机或解密卡不能解密(自己加自己解没有问题,自己加的数据送到加密机不能解密 出错率 10%左右)以下抓取了一条出错数据:
明文:"11111111"
密文:1db3c72f5f0d012e2e56c6b92ea7f4081b49e2fd93e4a89c59cbe2dd028d8c8328d357b8e5e4e43ed395aa439d7fe032f92858ea0d53a02526a36eadb34faaa92ea1646f642f53812a4fc38876dbf253d38ea2ba4096d876462840b7b51c33b5719c09b08a170655
私钥:46D77D6E14C1DDC632159FA6E2AC3E204F6961869342DC99BC2684EDD71C57AB
Originally posted by @wangjun796 in #1 (comment)
生成的SM2密钥对的公钥为什么是130位的16进制数据?不应该是128位的吗?
ie不兼容
实际开发中遇到个问题,想请教下大佬们:
后端用Go语言生成的密钥对,前端用公钥能加密成功,在Go和Java上也能用对应的私钥解密成功,但前端无法解密
公钥:(取后130位)3059301306072a8648ce3d020106082a811ccf5501822d0342000410ee64f73e0acb2380bada2697e80a22b39968ac72833b7692968ff2c21bc88e8afcbdc1b89c3382089415cea907e61b02060194675a16c4d48c5e3a57c49388
私钥:308193020100301306072a8648ce3d020106082a811ccf5501822d047930770201010420ff54e3914a41191006633b5f08fcc5e42dbdf2c26c18d55dd9501f0d889c151ea00a06082a811ccf5501822da1440342000410ee64f73e0acb2380bada2697e80a22b39968ac72833b7692968ff2c21bc88e8afcbdc1b89c3382089415cea907e61b02060194675a16c4d48c5e3a57c49388
原文:你好啊
密文:(已添加 04 标识)044fd2407ae9b39d7e8080c15fa39ad392ce0ba26829345c6422c9fa1a8746ad4c5f42c3a1d6aaab0a8f16405afdcb3fb708c2a200b48ebd9b12adcaf686e4a060c6182903dd43e9b401afccc8504d9360d572a14d372dc7665fc2eb762f2e34c45cf50283c7c07b6e4d
Go和Java均能解密出原文,但是用sm-crypto解密后结果是空的。。
有实现基于sm2的秘钥交换算法吗?
最近做项目遇到一个问题,我是用硬件里面的sm2加密(解密),使用sm-crypto解密(解密),发现大多数情况下都能成功,但是也存在不成功的时候。但是只使用硬件加密解密都能成功(测试1000次),使用sm-crypto加密解密也能成功(测试1000次),但是用交互使用的时候就存在不能解密的可能。请教一下这个问题是怎么回事?
你好,我在webpack上面使用es5打包后,android9.0手机上面报错了
Uncaught TypeError: Cannot assign to read only property 'window' of object '#'
以下是报错段代码:
catch(e){t.window={}}e.exports={sm2:n(306),sm3:n(309),sm4:n(310)}}).call(this,n(26))},function(e,t,n){"use strict";var r=n(38).BigInteger,o=n(307),i=o.encodeDer,a=o.decodeDer,s=n(88).ECPointFp,u=n(132),l=n(308),c=n(89),f=c.generateEcparam(),d=f.G,h=f.curve,p=f.n,m=0;function y(e,t){var n=new u,r=(new u).getZ(d,t.substr(2,128)),o=c.hexToArray(c.arrayToHex(r).toString()),i=e,a=c.hexToArray(i),s=new Array(n.getDigestSize());return n.blockUpdate(o,0,o.length),n.blockUpdate(a,0,a.length),n.doFinal(s,0),c.arrayToHex(s).toString()}function v(){var e=c.generateKeyPairHex(),t=s.decodeFromHex(h,e.publicKey);return e.k=new r(e.privateKey,16),e.x1=t.getX().toBigInteger(),e}e.exports={generateKeyPairHex:c.generateKeyPairHex,doEncrypt:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1,r=new l;e=c.hexToArray(c.parseUtf8StringToHex(e)),t.length>128&&(t=t.substr(t.length-128));var o=t.substr(0,64),i=t.substr(64);t=r.createPoint(o,i);var a=r.initEncipher(t);r.encryptBlock(e);var s=c.arrayToHex(e),u=new Array(32);return r.doFinal(u),u=c.arrayToHex(u),n===m?a+s+u:a+u+s},doDecrypt:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:1,o=new l(n);t=new r(t,16);var i=e.substr(0,64),a=e.substr(0+i.length,64),s=i.length+a.length,u=e.substr(s,64),f=e.substr(s+64);n===m&&(u=e.substr(e.length-64),f=e.substr(s,e.length-s-64));var d=c.hexToArray(f),h=o.createPoint(i,a);o.initDecipher(t,h),o.decryptBlock(d);var p=new Array(32);return o.doFinal(p),c.arrayToHex(p)==u?c.arrayToUtf8(d):""},doSignature:function(e,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},o=n.pointPool,a=n.der,s=n.hash,u=n.publicKey,l="string"==typeof e?c.parseUtf8StringToHex(e):c.parseArrayBufferToHex(e);s&&(l=y(l,u=u||function(e){var t=d.multiply(new r(e,16)),n=c.leftPad(t.getX().toBigInteger().toString(16),64),o=c.leftPad(t.getY().toBigInteger().toString(16),64);return"04"+n+o}(t)));var f=new r(t,16),h=new r(l,16),m=null,g=null,b=null;do{do{var =void 0;m=(=o&&o.length?o.pop():v()).k,g=h.add(.x1).mod(p)}while(g.equals(r.ZERO)||g.add(m).equals(p));b=f.add(r.ONE).modInverse(p).multiply(m.subtract(g.multiply(f))).mod(p)}while(b.equals(r.ZERO));return a?i(g,b):c.leftPad(g.toString(16),64)+c.leftPad(b.toString(16),64)},doVerifySignature:function(e,t,n){var o=arguments.length>3&&void 0!==arguments[3]?arguments[3]:{},i=o.der,u=o.hash,l="string"==typeof e?c.parseUtf8StringToHex(e):c.parseArrayBufferToHex(e);u&&(l=y(l,n));var f=void 0,m=void 0;if(i){var v=a(t);f=v.r,m=v.s}else f=new r(t.substring(0,64),16),m=new r(t.substring(64),16);var g=s.decodeFromHex(h,n),b=new r(l,16),=f.add(m).mod(p);if(.equals(r.ZERO))return!1;var w=d.multiply(m).add(g.multiply()),S=b.add(w.getX().toBigInteger()).mod(p);return f.equals(S)},getPoint:v}},function(e,t,n){"use strict";var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.proto=t)}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var s=n(38).BigInteger;var u=function(){function e(){a(this,e),this.isModified=!0,this.hTLV=null,this.hT="00",this.hL="00",this.hV=""}return r(e,[{key:"getLengthHexFromValue",value:function(){var e=this.hV.length/2,t=e.toString(16);return t.length%2==1&&(t="0"+t),e<128?t:(128+t.length/2).toString(16)+t}},{key:"getEncodedHex",value:function(){return(null==this.hTLV||this.isModified)&&(this.hV=this.getFreshValueHex(),this.hL=this.getLengthHexFromValue(),this.hTLV=this.hT+this.hL+this.hV,this.isModified=!1),this.hTLV}},{key:"getFreshValueHex",value:function(){return""}}]),e}(),l=function(e){function t(e){a(this,t);var n=o(this,(t.proto||Object.getPrototypeOf(t)).call(this));return n.hT="02",e&&e.bigint&&(n.hTLV=null,n.isModified=!0,n.hV=function(e){var t=e.toString(16);if("-"!==t.substr(0,1))t.length%2==1?t="0"+t:t.match(/^[0-7]/)||(t="00"+t);else{var n=t.substr(1).length;n%2==1?n+=1:t.match(/^[0-7]/)||(n+=2);for(var r="",o=0;o<n;o++)r+="f";t=new s(r,16).xor(e).add(s.ONE).toString(16).replace(/^-/,"")}return t}(e.bigint)),n}return i(t,u),r(t,[{key:"getFreshValueHex",value:function(){return this.hV}}]),t}(),c=function(e){function t(e){a(this,t);var n=o(this,(t.proto||Object.getPrototypeOf(t)).call(this));return n.hT="30",n.asn1Array=[],e&&e.array&&(n.asn1Array=e.array),n}return i(t,u),r(t,[{key:"getFreshValueHex",value:function(){for(var e="",t=0;t<this.asn1Array.length;t++){e+=this.asn1Array[t].getEncodedHex()}return this.hV=e,this.hV}}]),t}();function f(e,t){if("8"!==e.substring(t+2,t+3))return 1;var n=parseInt(e.substring(t+3,t+4));return 0===n?-1:0<n&&n<10?n+1:-2}function d(e,t){var n=function(e,t){var n=f(e,t);return n<1?"":e.substring(t+2,t+2+2n)}(e,t);if(""===n)return-1;return(parseInt(n.substring(0,1))<8?new s(n,16):new s(n.substring(2),16)).intValue()}function h(e,t){var n=f(e,t);return n<0?l_len:t+2(n+1)}function p(e,t){var n=h(e,t),r=d(e,t);return e.substring(n,n+2r)}function m(e,t){return h(e,t)+2d(e,t)}e.exports={encodeDer:function(e,t){var n=new l({bigint:e}),r=new l({bigint:t});return new c({array:[n,r]}).getEncodedHex()},decodeDer:function(e){var t=function(e,t){var n=[],r=h(e,t);n.push(r);for(var o=d(e,t),i=r,a=0;;){var s=m(e,i);if(null===s||s-r>=2o)break;if(a>=200)break;n.push(s),i=s,a++}return n}(e,0),n=t[0],r=t[1],o=p(e,n),i=p(e,r);return{r:new s(o,16),s:new s(i,16)}}}},function(e,t,n){"use strict";var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();var o=n(38).BigInteger,i=n(88).ECPointFp,a=n(132),s=n(89),u=function(){function e(){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,e),this.ct=1,this.p2=null,this.sm3keybase=null,this.sm3c3=null,this.key=new Array(32),this.keyOff=0}return r(e,[{key:"reset",value:function(){this.sm3keybase=new a,this.sm3c3=new a;var e=s.hexToArray(this.p2.getX().toBigInteger().toRadix(16)),t=s.hexToArray(this.p2.getY().toBigInteger().toRadix(16));this.sm3keybase.blockUpdate(e,0,e.length),this.sm3c3.blockUpdate(e,0,e.length),this.sm3keybase.blockUpdate(t,0,t.length),this.ct=1,this.nextKey()}},{key:"nextKey",value:function(){var e=new a(this.sm3keybase);e.update(this.ct>>24&255),e.update(this.ct>>16&255),e.update(this.ct>>8&255),e.update(255&this.ct),e.doFinal(this.key,0),this.keyOff=0,this.ct++}},{key:"initEncipher",value:function(e){var t=s.generateKeyPairHex(),n=new o(t.privateKey,16),r=t.publicKey;return this.p2=e.multiply(n),this.reset(),r.length>128&&(r=r.substr(r.length-128)),r}},{key:"encryptBlock",value:function(e){this.sm3c3.blockUpdate(e,0,e.length);for(var t=0;t<e.length;t++)this.keyOff===this.key.length&&this.nextKey(),e[t]^=this.key[this.keyOff++]}},{key:"initDecipher",value:function(e,t){this.p2=t.multiply(e),this.reset()}},{key:"decryptBlock",value:function(e){for(var t=0;t<e.length;t++)this.keyOff===this.key.length&&this.nextKey(),e[t]^=this.key[this.keyOff++];this.sm3c3.blockUpdate(e,0,e.length)}},{key:"doFinal",value:function(e){var t=s.hexToArray(this.p2.getY().toBigInteger().toRadix(16));this.sm3c3.blockUpdate(t,0,t.length),this.sm3c3.doFinal(e,0),this.reset()}},{key:"createPoint",value:function(e,t){var n="04"+e+t;return i.decodeFromHex(s.generateEcparam().curve,n)}}]),e}();e.exports=u},function(e,t,n){"use strict";function r(e,t){return e.length>=t?e:new Array(t-e.length+1).join("0")+e}function o(e){for(var t="",n=0;n<e.length/2;n++)t+=r(parseInt(e.substr(2n,2),16).toString(2),8);return t}function i(e,t){return e.substring(t%e.length)+e.substr(0,t%e.length)}function a(e,t,n){for(var r=e||"",o=t||"",i=[],a=void 0,s=r.length-1;s>=0;s--)a=n(r[s],o[s],a),i[s]=a[0];return i.join("")}function s(e,t){return a(e,t,function(e,t){return[e===t?"0":"1"]})}function u(e,t){return a(e,t,function(e,t){return["1"===e&&"1"===t?"1":"0"]})}function l(e,t){return a(e,t,function(e,t){return["1"===e||"1"===t?"1":"0"]})}function c(e,t){return a(e,t,function(e,t,n){var r=n?n[1]:"0";return e!==t?["0"===r?"1":"0",r]:[r,e]})}function f(e){return function(){for(var t=arguments.length,n=Array(t),r=0;r<t;r++)n[r]=arguments[r];return n.reduce(function(t,n){return e(t,n)})}}function d(e){return f(s)(e,i(e,9),i(e,17))}function h(e,t,n,r){return r>=0&&r<=15?f(s)(e,t,n):f(l)(u(e,t),u(e,n),u(t,n))}function p(e,t,n,r){return r>=0&&r<=15?f(s)(e,t,n):l(u(e,t),u(a(e,void 0,function(e){return["1"===e?"0":"1"]}),n))}function m(e){return o(e>=0&&e<=15?"79cc4519":"7a879d8a")}function y(e,t){for(var n,r=[],o=[],a=0;a<16;a++)r.push(t.substr(32a,32));for(var u=16;u<68;u++)r.push(f(s)((n=f(s)(r[u-16],r[u-9],i(r[u-3],15)),f(s)(n,i(n,15),i(n,23))),i(r[u-13],7),r[u-6]));for(var l=0;l<64;l++)o.push(s(r[l],r[l+4]));for(var y=[],v=0;v<8;v++)y.push(e.substr(32v,32));for(var g=y[0],b=y[1],=y[2],w=y[3],S=y[4],E=y[5],k=y[6],x=y[7],T=void 0,O=void 0,M=void 0,C=void 0,P=0;P<64;P++)O=s(T=i(f(c)(i(g,12),S,i(m(P),P)),7),i(g,12)),M=f(c)(h(g,b,,P),w,O,o[P]),C=f(c)(p(S,E,k,P),x,T,r[P]),w=,=i(b,9),b=g,g=M,x=k,k=i(E,19),E=S,S=d(C);return s([g,b,,w,S,E,k,x].join(""),e)}e.exports=function(e){for(var t=function(e){var t="",n=!0,o=!1,i=void 0;try{for(var a,s=eSymbol.iterator;!(n=(a=s.next()).done);n=!0)t+=r(a.value.codePointAt(0).toString(2),8)}catch(e){o=!0,i=e}finally{try{!n&&s.return&&s.return()}finally{if(o)throw i}}return t}(e),n=t.length,i=n%512,a=(t+"1"+r("",i=i>=448?512-i%448-1:448-i-1)+r(n.toString(2),64)).toString(),s=(n+i+65)/512,u=o("7380166f4914b2b9172442d7da8a0600a96f30bc163138aae38dee4db0fb0e4e"),l=0;l<=s-1;l++){u=y(u,a.substr(512l,512))}return function(e){for(var t="",n=0;n<e.length/8;n++)t+=r(parseInt(e.substr(8n,8),2).toString(16),2);return t}(u)}},function(e,t,n){"use strict";var r=0,o=32,i=16,a=[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],s=[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];function u(e,t){return e<<t|e>>>32-t}function l(e){return(255&a[e>>>24&255])<<24|(255&a[e>>>16&255])<<16|(255&a[e>>>8&255])<<8|255&a[255&e]}function c(e){return e^u(e,2)^u(e,10)^u(e,18)^u(e,24)}function f(e){return e^u(e,13)^u(e,23)}function d(e,t,n){for(var r=void 0,o=void 0,i=new Array(4),a=new Array(4),s=0;s<4;s++)a[0]=255&e[0+4s],a[1]=255&e[1+4s],a[2]=255&e[2+4s],a[3]=255&e[3+4s],i[s]=a[0]<<24|a[1]<<16|a[2]<<8|a[3];for(r=0;r<32;r+=4)o=l(o=i[1]^i[2]^i[3]^n[r+0]),i[0]=i[0]^c(o),o=l(o=i[2]^i[3]^i[0]^n[r+1]),i[1]=i[1]^c(o),o=l(o=i[3]^i[0]^i[1]^n[r+2]),i[2]=i[2]^c(o),o=l(o=i[0]^i[1]^i[2]^n[r+3]),i[3]=i[3]^c(o);for(var u=0;u<16;u+=4)t[u]=i[3-u/4]>>>24&255,t[u+1]=i[3-u/4]>>>16&255,t[u+2]=i[3-u/4]>>>8&255,t[u+3]=255&i[3-u/4]}function h(e,t,n){var a=[],u=0,c=new Array(o);!function(e,t,n){for(var o=void 0,i=void 0,a=new Array(4),u=new Array(4),c=0;c<4;c++)u[0]=255&e[0+4c],u[1]=255&e[1+4c],u[2]=255&e[2+4c],u[3]=255&e[3+4c],a[c]=u[0]<<24|u[1]<<16|u[2]<<8|u[3];for(a[0]^=2746333894,a[1]^=1453994832,a[2]^=1736282519,a[3]^=2993693404,o=0;o<32;o+=4)i=l(i=a[1]^a[2]^a[3]^s[o+0]),t[o+0]=a[0]^=f(i),i=l(i=a[2]^a[3]^a[0]^s[o+1]),t[o+1]=a[1]^=f(i),i=l(i=a[3]^a[0]^a[1]^s[o+2]),t[o+2]=a[2]^=f(i),i=l(i=a[0]^a[1]^a[2]^s[o+3]),t[o+3]=a[3]^=f(i);if(n===r)for(o=0;o<16;o++)i=t[o],t[o]=t[31-o],t[31-o]=i}(t,c,n);new Array(16);for(var h=new Array(16),p=e.length;p>=i;){d(e.slice(u,u+16),h,c);for(var m=0;m<i;m++)a[u+m]=h[m];p-=i,u+=i}return a}e.exports={encrypt:function(e,t){return h(e,t,1)},decrypt:function(e,t){return h(e,t,0)}}},function(e,t,n){"use strict";var r,o,i,a,s="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};a=function(e){return e},"object"===s(t)?e.exports=t=a(n(0),n(32),n(133),n(134),n(20),n(21),n(39),n(60),n(135),n(61),n(136),n(137),n(138),n(40),n(139),n(14),n(2),n(140),n(141),n(142),n(143),n(144),n(145),n(146),n(147),n(148),n(149),n(150),n(151),n(152),n(153),n(154),n(155)):(o=[n(0),n(32),n(133),n(134),n(20),n(21),n(39),n(60),n(135),n(61),n(136),n(137),n(138),n(40),n(139),n(14),n(2),n(140),n(141),n(142),n(143),n(144),n(145),n(146),n(147),n(148),n(149),n(150),n(151),n(152),n(153),n(154),n(155)],void 0===(i="function"==typeof(r=a)?r.apply(t,o):r)||(e.exports=i))},function(e,t,n){"use strict";var r=n(313),o=n(314),i=n(157);e.exports={formats:i,parse:o,stringify:r}},function(e,t,n){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},o=n(156),i=n(157),a={brackets:function(e){return e+"[]"},indices:function(e,t){return e+"["+t+"]"},repeat:function(e){return e}},s=Date.prototype.toISOString,u={delimiter:"&",encode:!0,encoder:o.encode,encodeValuesOnly:!1,serializeDate:function(e){return s.call(e)},skipNulls:!1,strictNullHandling:!1},l=function e(t,n,r,i,a,s,l,c,f,d,h,p){var m=t;if("function"==typeof l)m=l(n,m);else if(m instanceof Date)m=d(m);else if(null===m){if(i)return s&&!p?s(n,u.encoder):n;m=""}if("string"==typeof m||"number"==typeof m||"boolean"==typeof m||o.isBuffer(m))return s?[h(p?n:s(n,u.encoder))+"="+h(s(m,u.encoder))]:[h(n)+"="+h(String(m))];var y,v=[];if(void 0===m)return v;if(Array.isArray(l))y=l;else{var g=Object.keys(m);y=c?g.sort(c):g}for(var b=0;b<y.length;++b){var =y[b];a&&null===m[]||(v=Array.isArray(m)?v.concat(e(m[],r(n,),r,i,a,s,l,c,f,d,h,p)):v.concat(e(m[],n+(f?"."+:"["++"]"),r,i,a,s,l,c,f,d,h,p)))}return v};e.exports=function(e,t){var n=e,s=t?o.assign({},t):{};if(null!==s.encoder&&void 0!==s.encoder&&"function"!=typeof s.encoder)throw new TypeError("Encoder has to be a function.");var c=void 0===s.delimiter?u.delimiter:s.delimiter,f="boolean"==typeof s.strictNullHandling?s.strictNullHandling:u.strictNullHandling,d="boolean"==typeof s.skipNulls?s.skipNulls:u.skipNulls,h="boolean"==typeof s.encode?s.encode:u.encode,p="function"==typeof s.encoder?s.encoder:u.encoder,m="function"==typeof s.sort?s.sort:null,y=void 0!==s.allowDots&&s.allowDots,v="function"==typeof s.serializeDate?s.serializeDate:u.serializeDate,g="boolean"==typeof s.encodeValuesOnly?s.encodeValuesOnly:u.encodeValuesOnly;if(void 0===s.format)s.format=i.default;else if(!Object.prototype.hasOwnProperty.call(i.formatters,s.format))throw new TypeError("Unknown format option provided.");var b,,w=i.formatters[s.format];"function"==typeof s.filter?n=(=s.filter)("",n):Array.isArray(s.filter)&&(b==s.filter);var S,E=[];if("object"!==(void 0===n?"undefined":r(n))||null===n)return"";S=s.arrayFormat in a?s.arrayFormat:"indices"in s?s.indices?"indices":"repeat":"indices";var k=a[S];b||(b=Object.keys(n)),m&&b.sort(m);for(var x=0;x<b.length;++x){var T=b[x];d&&null===n[T]||(E=E.concat(l(n[T],T,k,f,d,h?p:null,,m,y,v,w,g)))}var O=E.join(c),M=!0===s.addQueryPrefix?"?":"";return O.length>0?M+O:""}},function(e,t,n){"use strict";var r=n(156),o=Object.prototype.hasOwnProperty,i={allowDots:!1,allowPrototypes:!1,arrayLimit:20,decoder:r.decode,delimiter:"&",depth:5,parameterLimit:1e3,plainObjects:!1,strictNullHandling:!1},a=function(e,t,n){if(e){var r=n.allowDots?e.replace(/.([^.[]+)/g,"[$1]"):e,i=/(\[[^[\]]*])/g,a=/(\[[^[\]]*])/.exec(r),s=a?r.slice(0,a.index):r,u=[];if(s){if(!n.plainObjects&&o.call(Object.prototype,s)&&!n.allowPrototypes)return;u.push(s)}for(var l=0;null!==(a=i.exec(r))&&l<n.depth;){if(l+=1,!n.plainObjects&&o.call(Object.prototype,a[1].slice(1,-1))&&!n.allowPrototypes)return;u.push(a[1])}return a&&u.push("["+r.slice(a.index)+"]"),function(e,t,n){for(var r=t,o=e.length-1;o>=0;--o){var i,a=e[o];if("[]"===a)i=(i=[]).concat(r);else{i=n.plainObjects?Object.create(null):{};var s="["===a.charAt(0)&&"]"===a.charAt(a.length-1)?a.slice(1,-1):a,u=parseInt(s,10);!isNaN(u)&&a!==s&&String(u)===s&&u>=0&&n.parseArrays&&u<=n.arrayLimit?(i=[])[u]=r:i[s]=r}r=i}return r}(u,t,n)}};e.exports=function(e,t){var n=t?r.assign({},t):{};if(null!==n.decoder&&void 0!==n.decoder&&"function"!=typeof n.decoder)throw new TypeError("Decoder has to be a function.");if(n.ignoreQueryPrefix=!0===n.ignoreQueryPrefix,n.delimiter="string"==typeof n.delimiter||r.isRegExp(n.delimiter)?n.delimiter:i.delimiter,n.depth="number"==typeof n.depth?n.depth:i.depth,n.arrayLimit="number"==typeof n.arrayLimit?n.arrayLimit:i.arrayLimit,n.parseArrays=!1!==n.parseArrays,n.decoder="function"==typeof n.decoder?n.decoder:i.decoder,n.allowDots="boolean"==typeof n.allowDots?n.allowDots:i.allowDots,n.plainObjects="boolean"==typeof n.plainObjects?n.plainObjects:i.plainObjects,n.allowPrototypes="boolean"==typeof n.allowPrototypes?n.allowPrototypes:i.allowPrototypes,n.parameterLimit="number"==typeof n.parameterLimit?n.parameterLimit:i.parameterLimit,n.strictNullHandling="boolean"==typeof n.strictNullHandling?n.strictNullHandling:i.strictNullHandling,""===e||null==e)return n.plainObjects?Object.create(null):{};for(var s="string"==typeof e?function(e,t){for(var n={},r=t.ignoreQueryPrefix?e.replace(/^\?/,""):e,a=t.parameterLimit===1/0?void 0:t.parameterLimit,s=r.split(t.delimiter,a),u=0;u<s.length;++u){var l,c,f=s[u],d=f.indexOf("]="),h=-1===d?f.indexOf("="):d+1;-1===h?(l=t.decoder(f,i.decoder),c=t.strictNullHandling?null:""):(l=t.decoder(f.slice(0,h),i.decoder),c=t.decoder(f.slice(h+1),i.decoder)),o.call(n,l)?n[l]=[].concat(n[l]).concat(c):n[l]=c}return n}(e,n):e,u=n.plainObjects?Object.create(null):{},l=Object.keys(s),c=0;c<l.length;++c){var f=l[c],d=a(f,s[f],n);u=r.merge(u,d,n)}return r.compact(u)}},function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=c(n(11)),o=c(n(3)),i=c(n(4)),a=c(n(5)),s=c(n(6)),u=c(n(7)),l=c(n(1));function c(e){return e&&e.__esModule?e:{default:e}}var f=function(e,t){var n={};for(var r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.indexOf(r)<0&&(n[r]=e[r]);if(null!=e&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(r=Object.getOwnPropertySymbols(e);o<r.length;o++)t.indexOf(r[o])<0&&(n[r[o]]=e[r[o]])}return n},d=function(e){function t(){return(0,o.default)(this,t),(0,a.default)(this,(t.proto||Object.getPrototypeOf(t)).apply(this,arguments))}return(0,s.default)(t,e),(0,i.default)(t,[{key:"render",value:function(){var e=this.props,t=e.prefixCls,n=e.className,o=e.children,i=e.mode,a=e.icon,s=e.onLeftClick,c=e.leftContent,d=e.rightContent,h=f(e,["prefixCls","className","children","mode","icon","onLeftClick","leftContent","rightContent"]);return l.default.createElement("div",(0,r.default)({},h,{className:(0,u.default)(n,t,t+"-"+i)}),l.default.createElement("div",{className:t+"-left",role:"button",onClick:s},a?l.default.createElement("span",{className:t+"-left-icon","aria-hidden":"true"},a):null,c),l.default.createElement("div",{className:t+"-title"},o),l.default.createElement("div",{className:t+"-right"},d))}}]),t}(l.default.Component);t.default=d,d.defaultProps={prefixCls:"am-navbar",mode:"dark",onLeftClick:function(){}},e.exports=t.default},function(e,t,n){"use strict";n(30),n(317)},function(e,t,n){},,function(e,t,n){},,function(e,t,n){e.exports=n.p+"3Q-Az0gCDO.png"},function(e,t,n){},,,function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),o=n(1),i=d(o),a=n(23),s=n(24),u=d(n(15)),l=d(n(7)),c=d(n(8)),f=d(n(44));function d(e){return e&&e.__esModule?e:{default:e}}function h(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}n(326);var p=function(e){function t(){var e,n,r;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t);for(var o=arguments.length,i=Array(o),a=0;a<o;a++)i[a]=arguments[a];return n=r=h(this,(e=t.proto||Object.getPrototypeOf(t)).call.apply(e,[this].concat(i))),r.initDatas=function(){var e=[],t=[];if(r.props.location.query){console.log("query is ",r.props.location.query);for(var n=0;n<r.props.location.query.length;n++)for(var o=0;o<c.default.customRouters.length;o++)if(r.props.location.query[n]===c.default.customRouters[o].routeName){e.push(c.default.customRouters[o]);break}}else for(n=0;n<c.default.pagesStore.searchTicket.searchResults.length;n++)for(o=0;o<c.default.customRouters.length;o++)if(c.default.pagesStore.searchTicket.searchResults[n]===c.default.customRouters[o].routeName){e.push(c.default.customRouters[o]);break}for(n=0;n<e.length;n++)for(o=0;o<e[n].stationCount;o++){if(c.default.pagesStore.indexPage.startStationRecord===e[n].stations[o].stationName){t.push(!1);break}if(c.default.pagesStore.indexPage.endStationRecord.stationName===e[n].stations[o].stationName){t.push(!0);break}}r.setState({searchResults:e,reverselist:t})},r.goToRouterMapPage=function(e,t){console.log("routeItem is ",t)},r.getRouterInfo=function(e,t){r.getRouterIndex=t,(0,s.postMethod)("/Controller/Csbs/Route/Query/findTripByRouteId",{routeCode:e.routeNumber,periodDays:30},r.getRouterInfoSuccess,r.getRouterInfoFail)},r.getRouterInfoSuccess=function(e){console.log("get router info success");var t={router:e,reverse:r.state.reverselist[r.getRouterIndex]};c.default.routerInfo=t,r.props.history.push({pathname:"./router-map",query:t})},r.getRouterInfoFail=function(e){console.log("get router info fail")},h(r,n)}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.proto=t)}(t,o.Component),r(t,[{key:"componentWillMount",value:function(){this.initDatas()}},{key:"componentDidMount",value:function(){}},{key:"render",value:function(){var e=this,t=this.state,n=t.searchResults,r=t.reverselist;return i.default.createElement("div",null,i.default.createElement(u.default,{title:"線路列表"}),i.default.createElement("div",{className:"tickets-all-div"},i.default.createElement("div",{className:"search-tickets-scrollview"},n.map(function(t,o){return i.default.createElement("div",{className:(0,l.default)("router-info-view",{"end-margin":o!==n.length-1}),key:"router-"+o,onClick:function(){e.getRouterInfo(t,o)}},i.default.createElement("div",{className:"router-info-row-1-view"},i.default.createElement("div",{className:"router-info-row-1-left-all-view"},i.default.createElement("div",{className:"router-info-row-1-left-router-text-view"},"網約公交"),i.default.createElement("div",{className:"router-info-row-1-left-router-view"},t.routeName+"路線")),i.default.createElement("div",{className:"router-info-row-1-first-time-view"},""+(r[o]?t.negFirstTripTime:t.firstTripTime))),i.default.createElement("div",{className:"router-info-row-2-view"},i.default.createElement("div",{className:"router-info-row-2-start-view"},""+(r[o]?t.finalStation.stationName:t.firstStation.stationName)),i.default.createElement("div",{className:"router-info-row-2-middle-view"},i.default.createElement("img",{src:f.default})),i.default.createElement("div",{className:"router-info-row-2-start-view"},""+(r[o]?t.firstStation.stationName:t.finalStation.stationName))),i.default.createElement("div",{className:"router-info-row-3-view"},i.default.createElement("div",{className:"left"},i.default.createElement("div",{className:"router-info-row-3-text-view"},"途徑"+t.stationCount+"站"),i.default.createElement("div",{className:"router-info-row-3-round-view"}),i.default.createElement("div",{className:"router-info-row-3-text-view"},"票價 MOP "+(t.routePrice/10).toFixed(1)),i.default.createElement("div",{className:"router-info-row-3-round-view"}),i.default.createElement("div",{className:"router-info-row-3-text-view"},"路程 約"+t.consumingTime+"分鐘")),i.default.createElement("div",{className:"right"},"一人一座")),i.default.createElement("div",{className:"router-info-row-4-view"},i.default.createElement("div",{className:"router-info-row-check-button",onClick:function(n){e.getRouterInfo(t,o)}},"查看")))}))))}}]),t}();t.default=(0,a.withRouter)(p)},function(e,t,n){},,function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=h(n(12)),o=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}();n(13);var i=n(1),a=h(i),s=n(23),u=n(31),l=n(24),c=(n(41),h(n(15))),f=h(n(329)),d=h(n(8));function h(e){return e&&e.__esModule?e:{default:e}}n(342);r.default.alert;var p=function(e){function t(e){!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,t),console.log("RouterMap constructor, ",e.location.query);var n=function(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}(this,(t.proto||Object.getPrototypeOf(t)).call(this,e));return n.checkTicketInfo=function(e){(0,l.postMethod)("/Controller/Csbs/Ticket/Biz/Query/findByTripId",{tripId:e},n.getTripTicketInfoSuccess)},n.getTripTicketInfoSuccess=function(e){e.length&&(console.log("getTripTicketInfoSuccess data ",e),d.default.routerTripTicketInfo=e,n.props.history.push({pathname:"./buy-ticket",query:!0}))},n.props.location.query?(n.routerInfo=n.props.location.query,n.routeNumber=n.routerInfo.router.routeNumber,n.title="網約公交-"+n.routerInfo.router.trip.routeName+"路線",n.mapCenter={longitude:113.320148,latitude:23.12256},console.log("this.routerInfo.router ",n.routerInfo.router)):(console.log("enter constructor else ",n.props.history),n.props.history.replace("./index-page")),n}return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.proto=t)}(t,i.Component),o(t,[{key:"componentWillMount",value:function(){this.offsetTop=515,this.paidOrders=[]}},{key:"componentDidMount",value:function(){var e=window.AMap,t=document.getElementById("map-container"),n=document.getElementById("draw-up-content-view");this.offsetTop=n.offsetTop,t.style.height=this.offsetTop-45+"px";var r=d.default.routeStations,o=this.getMapCenter(r),i=new e.Map("map-container",{resizeEnable:!0,dragEnable:!0,zoomEnable:!0,lang:"zh_hk",center:[o.longitude,o.latitude],zoom:13});if(i.plugin(["AMap.ToolBar"],function(){var t=new e.ToolBar(i);i.addControl(t)}),r&&r.length>0){var a=void 0,s=void 0,l=[];r.forEach(function(t,n){var o=(0,u.gps84ToGcj02)(t.latitude,t.longitude),c=t.stationName,f="
这边测试,数据为200k左右时,sm.js返回的时间需要7s左右
测试发现 key 不是 16 位的长度直接会无法加解密, 待解加密字符串低于16位也无法加解密,大于16位仅加密16 位
测试代码:
const sm4key = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10]
// const sm4OriginData = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10]
const sm4OriginData = 'abcdefghijklmnopqrstuvwxyz'.split('').map(x => x.charCodeAt(0))
let sm4EncData = sm4.encrypt(sm4OriginData, sm4key)
console.log(sm4EncData)
let sm4DecData = sm4.decrypt(sm4EncData, sm4key)
console.log(sm4DecData.map(x => String.fromCharCode(x)).join(''))
output:
[
5, 96, 145,
84, 199, 170,
36, 101, 198,
106, 212, 61,
8, 117, 167,
14
]
abcdefghijklmnop
能给个demo吗,比如我想加密字符串"test",要怎么做?谢谢
用 bouncycastle
实现的加解密(SM2/SM4-cbc)和本项目实现的加解密不兼容,互相无法解密另一方加密的内容。
目前的 SM4 加解密模式是 ecb 模式,实际上很多其他语言都是用cbc 模式来做…希望能支持cbc模式
如题,希望获取33位的压缩公钥,请问是否有办法提供
#1 因为我是直接引入网页使用,所以被这个问题折腾了好几天,既然dist目录提交到git了,还是更新一下吧:joy:
能实现下sm2的密钥交换算法吗?请求参考。
let sm2 = require('sm-crypto').sm2是会报错C1C2C3 is not defined
这里SM2是通过generateKeyPairHex()方法获取的公私钥,现需要使用别的语言生成的pem格式的公私钥,如何读取啊
经过多方面测试, 发现js在处理数据时有可能出现数据位数对不上的BUG, 以下是经修改后的代码片段, 经测试可与java、C#、C++等其他语言正常对接10万次以上
var SM2Cipher = function () {
function SM2Cipher() {
_classCallCheck(this, SM2Cipher);
this.ct = 1;
this.p2 = null;
this.sm3keybase = null;
this.sm3c3 = null;
this.key = new Array(32);
this.keyOff = 0;
}
SM2Cipher.prototype.GetHexString64 = function GetHexString64(h) {
if (h.length == 64) {
return h;
} else {
// console.log(h);
for (var i = h.length; i < 64; i++) {
h = "0" + h;
}
return h;
}
},
SM2Cipher.prototype.reset = function reset() {
this.sm3keybase = new SM3Digest();
this.sm3c3 = new SM3Digest();
// var xWords = _.hexToArray(this.p2.getX().toBigInteger().toRadix(16));
// var yWords = _.hexToArray(this.p2.getY().toBigInteger().toRadix(16));
var xWords = _.hexToArray(this.GetHexString64(this.p2.getX().toBigInteger().toRadix(16)));
var yWords = _.hexToArray(this.GetHexString64(this.p2.getY().toBigInteger().toRadix(16)));
// var xWords = this.GetWords(this.GetHexString64(this.p2.getX().toBigInteger().toRadix(16)));
// var yWords = this.GetWords(this.GetHexString64(this.p2.getY().toBigInteger().toRadix(16)));
this.sm3keybase.blockUpdate(xWords, 0, xWords.length);
this.sm3c3.blockUpdate(xWords, 0, xWords.length);
this.sm3keybase.blockUpdate(yWords, 0, yWords.length);
this.ct = 1;
this.nextKey();
};
SM2Cipher.prototype.doFinal = function doFinal(c3) {
// var yWords = _.hexToArray(this.p2.getY().toBigInteger().toRadix(16));
var yWords = _.hexToArray(this.GetHexString64(this.p2.getY().toBigInteger().toRadix(16)));
// var yWords = this.GetWords(this.GetHexString64(this.p2.getY().toBigInteger().toRadix(16)));
this.sm3c3.blockUpdate(yWords, 0, yWords.length);
this.sm3c3.doFinal(c3, 0);
this.reset();
};
SM2Cipher.prototype.nextKey = function nextKey() {
var sm3keycur = new SM3Digest(this.sm3keybase);
sm3keycur.update(this.ct >> 24 & 0x00ff);
sm3keycur.update(this.ct >> 16 & 0x00ff);
sm3keycur.update(this.ct >> 8 & 0x00ff);
sm3keycur.update(this.ct & 0x00ff);
sm3keycur.doFinal(this.key, 0);
for (var i = 0; i < this.key.length; i++) {
this.key[i] = this.key[i] & 255
}
this.keyOff = 0;
this.ct++;
};
SM2Cipher.prototype.initEncipher = function initEncipher(userKey) {
var keypair = _.generateKeyPairHex();
var k = new BigInteger(keypair.privateKey, 16);
var publicKey = keypair.publicKey;
this.p2 = userKey.multiply(k); // [k](Pb)
this.reset();
if (publicKey.length > 128) {
publicKey = publicKey.substr(publicKey.length - 128);
}
return publicKey;
};
SM2Cipher.prototype.encryptBlock = function encryptBlock(data) {
this.sm3c3.blockUpdate(data, 0, data.length);
for (var i = 0; i < data.length; i++) {
if (this.keyOff === this.key.length) {
this.nextKey();
}
data[i] ^= this.key[this.keyOff++] & 0xff;
}
};
SM2Cipher.prototype.initDecipher = function initDecipher(userD, c1) {
this.p2 = c1.multiply(userD);
this.reset();
};
SM2Cipher.prototype.decryptBlock = function decryptBlock(data) {
for (var i = 0; i < data.length; i++) {
if (this.keyOff === this.key.length) {
this.nextKey();
}
data[i] ^= this.key[this.keyOff++] & 0xff;
}
this.sm3c3.blockUpdate(data, 0, data.length);
};
SM2Cipher.prototype.createPoint = function createPoint(x, y) {
var publicKey = '04' + x + y;
var point = _.getGlobalCurve().decodePointHex(publicKey);
return point;
};
return SM2Cipher;
}();
module.exports = SM2Cipher;
/***/
})
通过cdn网站查询到
https://cdn.jsdelivr.net/npm/[email protected]/src/index.min.js
但是打开内部是引用,根本不能用,希望提供可用的cdn资源
是有什么兼容问题需要注意吗
本地自行加解密没有问题,但是服务端有百分之一的概率解密失败,求大神帮助
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.