decryptBytes abstract method

Future<Uint8List> decryptBytes(
  1. List<int> data,
  2. List<int> counter,
  3. int length
)

Decrypt data with this AesCtrSecretKey using AES in Counter mode, as specified in NIST SP800-38A.

To decrypt data the same initial counter block counter and length as was used for encryption must be specified. The counter must always be 16 bytes. See encryptBytes for further discussion of the initial counter block and length.

Example

import 'dart:convert' show utf8;
import 'dart:typed_data' show Uint8List;
import 'package:webcrypto/webcrypto.dart';

// Generate a new random AES-CTR secret key for AES-256.
final k = await AesCtrSecretKey.generate(256);

// Use a unique counter for each message.
final ctr = Uint8List(16); // always 16 bytes
fillRandomBytes(ctr);

// Length of the counter, the N'th right most bits of ctr are incremented
// for each block, the left most 128 - N bits are used as static nonce.
// Thus, messages must be less than 2^64 * 16 bytes.
final N = 64;

// Encrypt a message
final c = await k.encryptBytes(utf8.encode('hello world'), ctr, N);

// Decrypt message (requires the same counter ctr and length N)
print(utf8.decode(await k.decryptBytes(c, ctr, N))); // hello world

Remark Firefox does not implement counter rollover for AES-CTR correctly. Picking a sufficiently large length and using a counter that isn't filled with 0xff will likely avoid counter rollovers. See bug 1803105 for details.

Implementation

Future<Uint8List> decryptBytes(
  List<int> data,
  List<int> counter,
  int length,
);