0

I am experimenting with developing rsa with flutter and nodejs. public.pem and private.pem are same for both. Both the code are working fine when run in same language. but when transmitting data over api. encrypting in flutter and decrypting in nodejs not working. Can it be resolved. node-rsa nodejs encrypt in flutter this is my nodejs code

static Encrypt(data){
    var publickeyvalue = new NodeRSA();
    var publickey = fs.readFileSync("./src/Keys/public.pem","utf8");
    publickeyvalue.importKey(publickey);
    console.log(data);
    const encrypt = publickeyvalue.encrypt(data,"base64");
    return encrypt;
}

static async Decrypt(data){
    try{
        var privatekeyvalue = new NodeRSA();
        var privatekey = fs.readFileSync("./src/Keys/private.pem","utf8");
        privatekeyvalue.importKey(privatekey);
        console.log(data);

        const decrypt = privatekeyvalue.decrypt(data);
        return decrypt;
        if(decrypt != null){
            return decrypt.toString();
        }
    }catch(e){
        console.log(e);
    }
}

this is my flutter code

encrypt(var value) async {
    final publicPem = await rootBundle.loadString('key/public.pem');
    final publicKey = RSAKeyParser().parse(publicPem) as RSAPublicKey;
    final privatePem = await rootBundle.loadString('key/private.pem');
    final privateKey = RSAKeyParser().parse(privatePem) as RSAPrivateKey;
    final encrypter = Encrypter(RSA(publicKey: publicKey, 
    privateKey:privateKey,encoding:RSAEncoding.OAEP));

    final encrypted = await encrypter.encrypt(value).base64;
    return encrypted;
}

decrypt(var value) async {
    final publicPem = await rootBundle.loadString('key/public.pem');
    final publicKey = RSAKeyParser().parse(publicPem) as RSAPublicKey;
    final privatePem = await rootBundle.loadString('key/private.pem');
    final privateKey = RSAKeyParser().parse(privatePem) as RSAPrivateKey;
    final encrypter = Encrypter(RSA(publicKey: publicKey, privateKey: privateKey));
    final decrypted = encrypter.decrypt(value);
    return decrypted;
}

error

Error during decryption (probably incorrect key). Original error: Error: error:02000079:rsa routines::oaep decoding error
1
  • The decrypt tries to remove an OAEP padding, that the encrypt properly did not add., and thus fails with: "oaep decoding error" Commented May 22 at 13:43

0

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.