encrypt icon indicating copy to clipboard operation
encrypt copied to clipboard

how to decrypt data using Public key (RSA algorithm) which was previously encrypted using private key ?

Open nateshmbhat opened this issue 5 years ago • 6 comments

I have a backend that encrypts the data using private key (java backend) and when i get the encrypted data at the client side , i'm trying to decrypt it using the corresponding public key.

But the encrypter complains that i cannot use public key for decryption.

Can\'t decrypt without a private key, null given

nateshmbhat avatar Nov 21 '20 05:11 nateshmbhat

Please, share a small snippet that reproduces the errors.

leocavalcante avatar Nov 21 '20 14:11 leocavalcante

String decrypt(String publicKey, Uint8List encryptedData) {
    final rsaPublicKey = RSAKeyParser().parse(publicKey);
    final encrypter = Encrypter(RSA(publicKey: rsaPublicKey));
    final encrypted = Encrypted(encryptedData);
    return encrypter.decrypt(encrypted);
}

Error occurs in line encrypter.decrypt(encrypted)

The error happens becoz of this line :

https://github.com/leocavalcante/encrypt/blob/bf5794c68be1821f919e0197232ca80e28395548/lib/src/algorithms/rsa.dart#L46

For RSA , it should be possible to encrypt using either public or private key and decrypt it using the other key.

nateshmbhat avatar Nov 21 '20 23:11 nateshmbhat

any updates on this ? We are stuck with this problem :/

nateshmbhat avatar Nov 23 '20 05:11 nateshmbhat

Sorry, not much time to dig on this. Have you some resources on the RSA spec about decrypting using a public key? Can you link them here? I'd like to read more about it before doing something like this.

leocavalcante avatar Nov 23 '20 13:11 leocavalcante

Sorry, not much time to dig on this. Have you some resources on the RSA spec about decrypting using a public key? Can you link them here? I'd like to read more about it before doing something like this.

https://blog.csdn.net/rznice/article/details/104486712/

YourksChan avatar Jan 15 '21 04:01 YourksChan

  Uint8List _encrypt(AsymmetricBlockCipher cipher, String plain) {
    final data = Uint8List.fromList(utf8.encode(plain));
    final length = data.length;
    final sizeBlock = cipher.inputBlockSize;
    final iteration = (length / sizeBlock).ceil();

    final List<int> encrypted =
        List.generate(iteration, (index) => index * sizeBlock)
            .map((offset) =>
                data.sublist(offset, (offset + sizeBlock).clamp(0, length)))
            .map((chunk) => cipher.process(chunk))
            .fold([], (acc, chunk) => acc..addAll(chunk));
    return Uint8List.fromList(encrypted);
  }

  String _decrypt(AsymmetricBlockCipher cipher, Uint8List data) {
    final length = data.length;
    final sizeBlock = cipher.inputBlockSize;
    final iteration = length ~/ sizeBlock;

    final List<int> decrypted =
        List.generate(iteration, (index) => index * sizeBlock)
            .map((offset) => data.sublist(offset, offset + sizeBlock))
            .map((chunk) => cipher.process(chunk))
            .fold([], (acc, chunk) => acc..addAll(chunk));

    return utf8.decode(decrypted);
  }

  AsymmetricBlockCipher _createCipher({
    required bool encryption,
    required AsymmetricKeyParameter key,
  }) {
    return AsymmetricBlockCipher('RSA/PKCS1')
      ..reset()
      ..init(encryption, key);
  }

  encryptByPrivateKey(BuildContext context) {
    AsymmetricBlockCipher cipher = _createCipher(
      encryption: true,
      key: PrivateKeyParameter<RSAPrivateKey>(getPrivateKey()),
    );

    final result = _encrypt(cipher, getPlainText());
  }

  // Check this
  decryptByPublicKey(BuildContext context, Uint8List encrypted) {
    final AsymmetricBlockCipher cipher = _createCipher(
      encryption: false,
      key: PublicKeyParameter<RSAPublicKey>(getPublicKey()),
    );

    String result = _decrypt(cipher, encrypted);
  }

  _encryptKeyByPublicKey(BuildContext context) {
    AsymmetricBlockCipher cipher = _createCipher(
      encryption: true,
      key: PublicKeyParameter<RSAPublicKey>(getPublicKey()),
    );

    Uint8List result = _encrypt(cipher, getPlainText());
  }

  _decryptByPrivateKey(BuildContext context, Uint8List encrypted) {
    final AsymmetricBlockCipher cipher = _createCipher(
      encryption: false,
      key: PrivateKeyParameter<RSAPrivateKey>(getPrivateKey()),
    );

    String result = _decrypt(cipher, encrypted);
  }

deviankim avatar Feb 10 '22 00:02 deviankim