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