'Flutter - How to decrypt a RSA private key encrypted string if we have RSA public key with us?

Flutter app receives public key in the form of contents of public.pem file which would be a string like "-----BEGIN PUBLIC KEY----- MIICqq7DBi9sBXZfDYJC+G57JYUCAwEAAQ== .... -----END PUBLIC KEY-----".
We also have a hashed string like "yBuHq6gZ...dgWs=".
Algorithm used to hash was RSA and following nodejs code was executed to generate the keys:

crypto.generateKeyPairSync('rsa', 
    {
            modulusLength: modulusLength,
            namedCurve: 'secp256k1', 
            publicKeyEncoding: {
                type: 'spki',
                format: 'pem'     
            },     
            privateKeyEncoding: {
                type: 'pkcs8',
                format: 'pem',
                cipher: 'aes-256-cbc',
                passphrase: passphrase
            } 
    });

I wish to decrypt the hashed value. It was easy in nodejs by using the crypto library but I can't find how to do it in dart (Flutter).



Solution 1:[1]

Yeah, it was a misconception only. I needed both the key, original text and signature to verify the signature. I thought that with only key and signature I can get original text which is not possible.

Solution 2:[2]

This is my example. I hope that will be helpful.

dependencies:
   encrypt: ^5.0.1

import 'package:encrypt/encrypt.dart';
import 'package:pointycastle/export.dart';
import 'dart:convert';

/// RSA decrypt by public key
/// PKCS1
String decryptByPublicKey(String public, String content) {
  RSAKeyParser parser = RSAKeyParser();
  RSAPublicKey publicKey = parser.parse(public) as RSAPublicKey;
  AsymmetricBlockCipher cipher = PKCS1Encoding(RSAEngine());
  cipher
    ..init(false, PublicKeyParameter<RSAPublicKey>(publicKey));
  return utf8.decode(cipher.process(Encrypted.fromBase64(content).bytes));
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Akshat Khare
Solution 2 muzi