bcgit / pc-dart

Pointy Castle - Dart Derived Bouncy Castle APIs
MIT License
237 stars 123 forks source link

SMIME decryption issue #204

Open ahsentanvir opened 1 year ago

ahsentanvir commented 1 year ago

I am trying to decrypt an SMIME encrypted email as received from an exchange server. The code block that I am using is derived from the example section of this repo -

String rsaDecrypt(RSAPrivateKey myPrivate, Uint8List cipherText) {
  final decryptor = RSAEngine()
    ..init(false, PrivateKeyParameter<RSAPrivateKey>(myPrivate));

  return String.fromCharCodes(_processInBlocks(decryptor, cipherText));
}

Uint8List _processInBlocks(AsymmetricBlockCipher engine, Uint8List input) {
  final numBlocks = input.length ~/ engine.inputBlockSize +
      ((input.length % engine.inputBlockSize != 0) ? 1 : 0);

  final output = Uint8List(numBlocks * engine.outputBlockSize);

  var inputOffset = 0;
  var outputOffset = 0;
  while (inputOffset < input.length) {
    final chunkSize = (inputOffset + engine.inputBlockSize <= input.length)
        ? engine.inputBlockSize
        : input.length - inputOffset;

    outputOffset += engine.processBlock(
        input, inputOffset, chunkSize, output, outputOffset);

    inputOffset += chunkSize;
  }

  return (output.length == outputOffset)
      ? output
      : output.sublist(0, outputOffset);
}

I am passing the right parameters to rsaDecrypt() method. i.e. the private key and Uint8List representation of mimeContent of the email. I have verified that private key & mimecontent is correct as the same work well for decryption using bouncycastle in java.

But I am unable to decrypt the email using this library in flutter and some garbage values are output. Any clues guys? Or is it like this flutter library is not mature enough to handle SMIME decryption?