0

I'm getting the following error while trying to decode recipient encrypted key in Dart (jose library):

Unhandled Exception: FormatException: Unexpected extension byte (at offset 0)

I'm decoding header with following method:

JoseHeader joseHeader = JoseHeader.fromBase64EncodedString(token!);

then getting List:

List recipients = joseHeader['recipients'];

header:

var recipientHeader = const Utf8Decoder().convert(base64Decode(jaData['header']));

and encrypted_key:

var recipientEncryptedKey = const Utf8Decoder().convert(base64Decode(jaData['encrypted_key']));

while header decrypts nicely, I cannot get the encrypted key - which results in the error I mentioned above.

Is there an issue with how I'm decoding it? or is there an issue with jose library?

I'd appreciate your support.

using jose library to decode JoseHeader and afterwards using dart:convert to decode base64 and utf8 bytes to string.

I'm expecting to get the recipient encrypted key string and use it along with RSA private key to decrypt ciphertext.

sharing code snippet I'm using:

Future<String?> decryptMessage() async {

    JoseHeader joseHeader = JoseHeader.fromBase64EncodedString(token!);
    print('decoded token: $joseHeader');

    try {

      List recipients = joseHeader['recipients'];
      var jaData = recipients[0];

      try {

        var recipientHeader = const Utf8Decoder().convert(base64Decode(jaData['header']));

        print('recipient_header: $recipientHeader');

        var recipientEncryptedKey = jaData['encrypted_key'];

        print('encrypted_key: $recipientEncryptedKey');
        
        var decodeEncryptedKey = const Utf8Decoder().convert(base64Decode(recipientEncryptedKey));
        print('decoded encrypted_key: $decodeEncryptedKey');
        
        try {

          String privateKeyFromFile = await rootBundle.loadString('assets/keys/privateky.pem');


          String iv = joseHeader['iv'];
          String ciphertext = joseHeader['ciphertext'];

          String protected = joseHeader['protected'];
          String tag = joseHeader['tag'];

          String decryptedStuff = decrypt(recipientEncryptedKey, rsaSup.parsePrivateKeyFromPem(privateKeyFromFile));
          print(decryptedStuff);

        } catch(e) {
          print(e);
        }
      } catch(e) {
        print(e);
      }
    } catch(e) {
      print(e);
    }
  }
dave
  • 1
  • 1
  • A key is usually a random byte sequence which is generally not UTF-8 decodable. The error message indicates a failed UTF-8 decoding. Why are you doing this in the first place? Apart from the output, `decodeEncryptedKey` is not used at all in the rest of the code. If you want to convert a random byte sequence into a string, a binary-to-text encoding is needed, like Base64 (`recipientEncryptedKey` seems to be Base64 encoded) or hex encoding. – Topaco Apr 29 '23 at 15:07
  • I want to decrypt tag and ciphertext - I'm learning how cryptography works, so maybe it is unnecessary to use encrypted_key... – dave Apr 29 '23 at 20:04

0 Answers0