1

I need to implement RSA. I am generating public and private keys from nodejs i am sending private key to client. I am able to encrypt data using public key from nodejs but i am unable to decrypt from flutter i had tried various libraries in flutter(simple_rsa, encrypt etc…) but those are not working i am getting padding errors, invalid private key errors. Can any one suggest me how to implement this ?

Here is my code

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

i am sending encrypt function return value to client which is msg in flutter
Flutter Code (Tried with many libraries this is one among them using 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);

1
  • It would be great if you could share your solution or some useful links Commented Sep 2, 2020 at 17:24

1 Answer 1

2

Keygen (NodeJS)

Docs: 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 encryption via JSEncrypt library

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 encryption via 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);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.