[README.md] 补充一些测试记录
This commit is contained in:
parent
7ac41c9f32
commit
99e748e269
57
README.md
57
README.md
@ -38,4 +38,59 @@ let kp=ticCrypto.secword2keypair(sw) // 把密语转换成公私钥
|
|||||||
let address=ticCrypto.secword2address(sw) // 把密语转换成地址
|
let address=ticCrypto.secword2address(sw) // 把密语转换成地址
|
||||||
```
|
```
|
||||||
|
|
||||||
---
|
## 其他
|
||||||
|
|
||||||
|
```
|
||||||
|
const keyPair = crypto.generateKeyPairSync('rsa', {
|
||||||
|
modulusLength: 520,
|
||||||
|
publicKeyEncoding: {
|
||||||
|
type: 'spki',
|
||||||
|
format: 'pem'
|
||||||
|
},
|
||||||
|
|
||||||
|
privateKeyEncoding: {
|
||||||
|
type: 'pkcs8',
|
||||||
|
format: 'pem',
|
||||||
|
cipher: 'aes-256-cbc',
|
||||||
|
passphrase: ''
|
||||||
|
}
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
这样生成的 keyPair.privateKey 开头是 -----BEGIN ENCRYPTED PRIVATE KEY-----
|
||||||
|
|
||||||
|
如果直接
|
||||||
|
```
|
||||||
|
crypto.privateEncrypt(kp.privateKey, Buffer.from('sdafasfdsaf'))
|
||||||
|
```
|
||||||
|
会报错
|
||||||
|
```
|
||||||
|
Uncaught TypeError: Passphrase required for encrypted key
|
||||||
|
```
|
||||||
|
所以要这样才行
|
||||||
|
```
|
||||||
|
crypto.privateEncrypt({key:kp.privateKey, passphrase:''}, Buffer.from('sdafasfdsaf'))
|
||||||
|
```
|
||||||
|
我从 https://www.cnblogs.com/chyingp/p/nodejs-asymmetric-enc-dec.html 抄到一个 privateKey 可以直接使用,不需要 passphrase
|
||||||
|
|
||||||
|
返回 Buffer。每次结果都一样
|
||||||
|
|
||||||
|
|
||||||
|
这样生成的 keyPair.publicKey 开头是 -----BEGIN PUBLIC KEY-----
|
||||||
|
|
||||||
|
可以直接
|
||||||
|
```
|
||||||
|
crypto.publicEncrypt(kp.publicKey, Buffer.from('sdafasfdsaf'))
|
||||||
|
```
|
||||||
|
|
||||||
|
返回 Buffer。每次结果不一样
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
似乎 crypto 一定要 rsa 公私钥才可以用加解密,ticCrypto.randomKeypair() 生成的 ecc 公私钥不行。
|
||||||
|
|
||||||
|
而 eccrypto 和 eccrypto-js 可以用。eccrypto.generateKeyPair() 生成的和 ticCrypto.randomKeypair() 一样
|
||||||
|
|
||||||
|
eccrypto 在 windows 上的安装有麻烦,一来需要手工安装 OpenSSL 到 c:\openssl-win64\,二来 openssl 1.1.0 起把 libeay32.lib 改名为 libcrypto.dll,而 eccrypto 需要 c:\openssl-win64\lib\libeay32.lib,会报错
|
||||||
|
|
||||||
|
eccrypto-js 在 devDependencies 里继承了 eccrypto,因此 npm i --production 即可
|
||||||
|
Loading…
Reference in New Issue
Block a user