node.js - 如何在flutter(解密)和Node(加密)中实现RSA?

标签 node.js flutter dart rsa public-key-encryption

我需要实现RSA。我从nodejs生成公钥和私钥,我将私钥发送给客户端。我能够使用来自nodejs的公钥加密数据,但无法从flutter解密,我曾在flutter中尝试过各种库(simple_rsa,加密等),但这些都不起作用,我收到填充错误,无效的私钥错误。有人可以建议我如何实现吗?

这是我的代码

Nodejs

    crypto.generateKeyPair('rsa', {
      modulusLength: 4096,
      publicKeyEncoding: {
        type: 'pkcs1',
        format: 'pem'
      },
      privateKeyEncoding: {
        type: 'pkcs1',
        format: 'pem',
        cipher: 'aes-256-cbc',
        passphrase: '',
      }
    }, (err, publicKey, privateKey) => {
      // Handle errors and use the generated key pair.
      if(err)
        throw err
        //Publickey, PrivateKey
    });
encrypt = function(data, publicKey) {
    var buffer = Buffer.from(data);
    var encrypted = crypto.publicEncrypt(publicKey, buffer);
    return encrypted.toString("base64");
};

我正在向客户端发送加密功能返回值,这是味精
Flutter代码
(尝试过许多库,这是其中一个使用simple_rsa的库)
    final decpKey = "-----BEGIN RSA PRIVATE KEY-----\nProc-Type:.........";
    final msg = "fH2EBmBS4fRHG1............."; 

    final decryptedText = await decryptString(msg, decpKey); //Error: Invalid private key
    print(decryptedText);

最佳答案

Keygen(NodeJS)
文件:https://nodejs.org/api/crypto.html#crypto_crypto_generatekeypair_type_options_callback

const { generateKeyPair } = require('crypto');

generateKeyPair('rsa', {
    modulusLength: 4096,    // key size in bits
    publicKeyEncoding: {
        type: 'spki',
        format: 'pem',
    },
    privateKeyEncoding: {   
        type: 'pkcs8',      // !!! pkcs1 doesn't work for me
        format: 'pem',
    },
}, (err, publicKey, privateKey) => {
    // Handle errors and use the generated key pair.
});
NodeJS 通过 JSEncrypt 库进行加密
节点jsencrypt:https://www.npmjs.com/package/node-jsencrypt
JSEncrypt:https://travistidwell.com/jsencrypt/#
const JSEncrypt = require('node-jsencrypt');  

function encrypt(text, key) {
    const crypt = new JSEncrypt();
    crypt.setKey(key);
    return crypt.encrypt(text);
}

function decrypt(encrypted, privateKey) {
    const crypt = new JSEncrypt();
    crypt.setPrivateKey(privateKey);
    return crypt.decrypt(encrypted);
}
Dart 通过加密加密
GitHub上的https://github.com/konstantinullrich/crypton
import 'package:crypton/crypton.dart';

import 'encryption.dart';

class AsymmetricCrypt implements Encryption {
  final String _key;
  RSAPublicKey _publicKey;
  RSAPrivateKey _privateKey;

  AsymmetricCrypt._(this._key);

  @override
  String encrypt(String plain) {
    _publicKey ??= RSAPublicKey.fromPEM(_key);
    return _publicKey.encrypt(plain);
  }

  @override
  String decrypt(String data) {
    _privateKey ??= RSAPrivateKey.fromPEM(_key);
    return _privateKey.decrypt(data);
  }
}

关于node.js - 如何在flutter(解密)和Node(加密)中实现RSA?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58960959/

相关文章:

javascript - const 已经在 ES6 开关 block 中声明

flutter - 如何限制WebviewScaffold显示有限的内容

flutter - ValueListenableBuilder不重建CheckboxListTile Flutter

firebase - 如何从Firestore Flutter中读取数据

javascript - 从另一台服务器读取 Node.js 中的大文件

node.js - Mongo 将foreignField对象转换为字符串

node.js - 浏览器 Angular 5 控制台中来自服务器的消息

flutter - 错误 : A non-null value must be returned since the return type 'Widget' doesn't allow null

dart - 字符串比较不适用于从网络抓取中收到的文本

dart - Flutter:构造函数中List参数的默认赋值