3

Node和ThinkJS常见问题

 1 year ago
source link: https://www.daguanren.cc/post/ThinkJS-chang-jian-wen-ti.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Node和ThinkJS常见问题

在使用Node和ThinkJS的过程中会遇到很多问题,这里主要用来记录这些问题。

定时器中,设置type=one ,依然所有子进程都执行任务?

解决方式:

npm install think-cluster安装1.5.7以上版本

https://github.com/thinkjs/thinkjs/issues/1491

如何解决 Node 的 aes 加密结果和 JAVA 加密结果不一致的问题?

以使用aes-192-ecb加密算法为例,参考Node文档官网写的加解密示例,仅适用于Node语言本身,即如果只用Node,加密解密都没问题。

但是如果用Node加密,用JAVA解密。或者用JAVA加密,用Node解密,就会有问题。

对于同一段需要加密的内容,两种语言加密出来的结果并不相同,例如其他同行遇到的:

https://segmentfault.com/q/1010000021976121

https://www.jb51.net/article/47872.htm

https://www.coder.work/article/105860

https://stackoverflow.com/questions/7787773/encrypt-with-node-js-crypto-module-and-decrypt-with-java-in-android-app

中间使用Node的时候还会遇到类似:

https://stackoverflow.com/questions/29540801/crypto-createdecipheriv-error-when-generating-iv

Error: Invalid key length at Error (native) at new Decipheriv (crypto.js:282:16) at Object.Decipheriv (crypto.js:279:12)subunit

当你试了以上博客所提的所有方法后,可能仍然发现问题无法解决。

那么问题究竟出在哪里呢?

我的理解,如果是128/192/256,分别需要不同的key长度。例如: aes-256-cbc requires a 256-bit key, meaning 32 bytes. However the (hex) key you are supplying is only 20 bytes. 不够的Node或JAVA就会使用语言默认的方式给你补全,就是在这个补全的过程,两个语言出现了不一致,那么为了解决这个问题,其实只需要你在设置key的时候直接按照所需长度设置就可以了。

# aes-256-cbc需要设置32个长度的key,例如设置成01234567890123456789012345678912即可256/8 = 32#aes-192-cbc需要设置24个长度的key,例如设置成0123456789012345678901234即可192/8 = 24#aes-128-cbc需要设置16个长度的key,例如设置成01234567890123456即可128/8 = 16lsl

然后使用AES在线加密/解密就可以测试成功啦。

贴下我的Node的代码吧:

async aes192ecbEncrypt(password, text) { var crypto = require('crypto') var cipher = crypto.createCipheriv("aes-192-ecb", password, ""); var crypted = cipher.update(text, 'utf8', 'hex') crypted += cipher.final('hex') return crypted; // aesEncrypt解码 async aes192ecbDecrypt(password, encrypted) { // // The IV is usually passed along with the ciphertext. const decipher = crypto.createDecipheriv("aes-192-ecb", password, ""); // // Encrypted using same algorithm, key and iv. let decrypted = decipher.update(encrypted, 'hex', 'utf8'); decrypted += decipher.final('utf8'); // console.log(decrypted); return decrypted;pgsql

其他代码示例:

https://gist.github.com/fratuz610/9f8c733dc1277ecee51f


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK