【问题标题】:How to implement RSA in flutter(decryption) and Node(encryption)?如何在flutter(解密)和Node(加密)中实现RSA?
【发布时间】:2019-11-20 18:19:08
【问题描述】:

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

这是我的代码

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 中的 msg
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);

【问题讨论】:

  • 如果您能分享您的解决方案或一些有用的链接,那就太好了

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


【解决方案1】:

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

node-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 加密通过 crypton

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);
  }
}

【讨论】:

    猜你喜欢
    • 2019-10-12
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2020-08-05
    相关资源
    最近更新 更多