node-rsa使用

简单介绍使用node-rsa模块生成/导出秘钥、加密/解密、签名/验证

Github:https://github.com/rzcoder/node-rsa
API:https://www.npmjs.com/package/node-rsa

基本使用

引入模块

1
npm install node-rsa
1
var NodeRSA = require('node-rsa');

生成及导入导出秘钥

1
2
3
4
5
var key = new NodeRSA({b: 512}); //生成512位秘钥
var pubkey = key.exportKey('pkcs8-public'); //导出公钥
var prikey = key.exportKey('pkcs8-private'); //导出私钥
var pubKey = new NodeRSA(pubKey, 'pkcs8-public'); //导入公钥
var priKey = new NodeRSA(priKey, 'pkcs8-private'); //导入私钥

加密解密

公钥加密(返回密文):

1
2
pubKey = new NodeRSA(publicKey, 'pkcs8-public');
var encrypted = pubKey.encrypt(buffer, 'base64');

私钥解密(返回明文):

1
2
priKey = new NodeRSA(privateKey, 'pkcs8-private');
var decrypted = priKey.decrypt(buffer, 'utf8');

签名验证

私钥签名(返回签名):

1
2
priKey = new NodeRSA(privateKey, 'pkcs8-private');
var signature = priKey.sign(buffer);

公钥验证(返回true或false):

1
2
pubKey = new NodeRSA(publicKey, 'pkcs8-public');
var flag = pubKey.verify(buffer, signature);

node-rsa加密,java解密调试

用NODE RSA JS加解密正常,用Java工具类加解密正常。但是使用node加密的串Java不能解密,报错如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
Exception in thread "main" javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)
at javax.crypto.Cipher.doFinal(Cipher.java:2223)
at com.cjk.mqtt.RSAUtils.decryptByPrivateKey(RSAUtils.java:161)
at com.cjk.mqtt.Main.main(Main.java:14)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

原因:node中默认的是DEFAULT_ENCRYPTION_SCHEME = 'pkcs1_oaep',而Java中默认的是pkcs1。
解决:手动设置:pubKey.setOptions({encryptionScheme: 'pkcs1'})

1
2
3
4
5
6
7
8
9
10
11
var NodeRSA = require('node-rsa');

pubKey = new NodeRSA('MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgiifXEil1vEIUOA1dHDqm/OJtaZ7+u0uSA+ZkjhXNR4saXuMYrOZs7KjydkYAnFUmRKURSRIwaRUvYUSonZopcFThskE9hmx9i6ouDC6O/GhZ/t2pjsmVS09NEIZwYWIAtMiHmWx6sBP+SLm6P8eF5/08PZsn+mLYi5ayv5BCao7o/M1qX0JKDHQID29mQ/oX9K7Cfk59HA2IBOXfmDJnoaYFGjUEikxiYIY3fFEiIi+MR6XnYixhQocLL5dnUZ4snD6GrugsKpbqwdjtqqXtrtzj4GZlgZVl/uniEbS+oOqIvbZhOyER/wOZe4KiIwBWkkHfQox6vvj1/MmXv9NUwIDAQAB','pkcs8-public');
pubKey.setOptions({encryptionScheme: 'pkcs1'}) //适配Java解密
var encrypted = pubKey.encrypt('{"name":"Joe.Ye","homepage":"http://www.appblog.cn"', 'base64');
console.log(encrypted)

priKey = new NodeRSA('MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCCKJ9cSKXW8QhQ4DV0cOqb84m1pnv67S5ID5mSOFc1Hixpe4xis5mzsqPJ2RgCcVSZEpRFJEjBpFS9hRKidmilwVOGyQT2GbH2Lqi4MLo78aFn+3amOyZVLT00QhnBhYgC0yIeZbHqwE/5Iubo/x4Xn/Tw9myf6YtiLlrK/kEJqjuj8zWpfQkoMdAgPb2ZD+hf0rsJ+Tn0cDYgE5d+YMmehpgUaNQSKTGJghjd8USIiL4xHpediLG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u8Y/LGMvYUIe5DM1ssAdiolVsvri1Hglup3vgN6Ds3bA+E76BS3ljUqgXINLuXgn2tXdNF30ji/tavfa8+zbQuAaUCgYEAtV4GK4nq8t7acKZzLSy5qMIJVyftP5UmhyXE6Rb3rjb0tA23SdbaWCTyffZ/n+QYS8pfpNTLMjjxgL3vp03EkJcdudRHjFwMcmrha2RL0yiIc3BCD2JuOWbvGKyGqCquvxhqBrxuBCK8zXzCSMqnYh57bIViayCyXdGreu0V0AI=','pkcs8-private');
priKey.setOptions({encryptionScheme: 'pkcs1'}) //适配Java加密
var decrypted = priKey.decrypt(encrypted, 'utf8');
console.log(decrypted)

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :