Sample code for 30+ languages & platforms
Dart

Find a JWE Recipient by Header Value

See more JSON Web Encryption (JWE) Examples

Demonstrates Jwe.FindRecipient, which searches the per-recipient unprotected headers for a member with a given name and string value, and returns the matching recipient index.

The file path is relative to the application's current working directory. An absolute path may also be used. Supply the path appropriate to your own environment.

Background. This locates the recipient that a particular key belongs to (for example by kid) before decrypting. The method returns -1 when no match is found.

Chilkat Dart Downloads

Dart
import 'package:chilkat/chilkat.dart';

void main() {
  final jwe = CkJwe();

  // The file path is relative to the application's current working directory.  An absolute path
  // may also be used.  Supply the path appropriate to your own environment.
  // Load a multi-recipient JWE (JSON serialization) from a file.
  final sbJwe = CkStringBuilder();
  try {
    sbJwe.loadFile('qa_data/message.jwe', 'utf-8');
  } on ChilkatException {
    print('Failed to load the JWE file.');
    return;
  }

  try {
    jwe.loadJweSb(sbJwe);
  } on ChilkatException catch (e) {
    print(e.lastErrorText);
    return;
  }

  // Search the per-recipient unprotected headers for a member named "kid" whose value equals the
  // given string.  Returns the recipient index, or -1 if not found.
  final bCaseSensitive = true;
  final index = jwe.findRecipient('kid', 'recipient-key-1', bCaseSensitive);
  if (index < 0) {
    print('Recipient not found.');
    return;
  }

  print('Found recipient at index $index');
}