基于uniapp框架开发微信小程序,引入国密SM4的依赖gm-crypt,在h5中可以执行,但是到微信开发者工具报n is not a constructor,请问该如何解决?
先在Hbulder软件中基于uniapp框架下引入 npm install gm-crypt
之后node_modules下载了gm-crypt
然后启动微信开发者工具,执行以下代码
let SM4 = require('gm-crypt').sm4
let configmmm = {
key:'tjcsxhjk12345678',
mode:'cbc',
iv:'ivv61234yyer9876',
cipherType:'base64'
}
let def = new SM4(configmmm) //这句话报错 catch: TypeError: n is not a constructor,在H5中是不报错的,只在微信开发者工具中报错,请问这个错如何 解决?
let resu = def.encrypt(message)
你写法有问题吧,你按照官方文档写法来,我试了是没有报错的。
const { SM4 } = require('gm-crypto') const key = '0123456789abcdeffedcba9876543210' // Any string of 32 hexadecimal digits const originalData = 'SM4 国标对称加密' /** * Block cipher modes: * - ECB: electronic codebook * - CBC: cipher block chaining */ let encryptedData, decryptedData // ECB encryptedData = SM4.encrypt(originalData, key, { inputEncoding: 'utf8', outputEncoding: 'base64' }) decryptedData = SM4.decrypt(encryptedData, key, { inputEncoding: 'base64', outputEncoding: 'utf8' }) // CBC const iv = '0123456789abcdeffedcba9876543210' // Initialization vector(any string of 32 hexadecimal digits) encryptedData = SM4.encrypt(originalData, key, { iv: iv, mode: SM4.constants.CBC, inputEncoding: 'utf8', outputEncoding: 'hex' }) decryptedData = SM4.decrypt(encryptedData, key, { iv: iv, mode: SM4.constants.CBC, inputEncoding: 'hex', outputEncoding: 'utf8' })
return unescape(encodeURIComponent(str)).split("").map(val => val.charCodeAt());
return decodeURIComponent(escape(String.fromCharCode(...strBuffer)));