decryptStream abstract method
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:io' show File;
import 'dart:convert' show utf8;
import 'dart:typed_data' show Uint8List;
import 'package:async/async.dart' show collectBytes;
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 from file and write to file
final inputFile = File('message.txt');
final encryptedFile = File('encrypted-message.binary');
final c = await k.encryptStream(
inputFile.openRead(),
ctr,
N,
).pipe(encryptedFile.openWrite());
// Decrypt message (requires the same counter ctr and length N)
final decryptedBytes = await collectBytes(k.decryptStream(
encryptedFile.openRead(),
ctr, // same ctr as used for encryption
N, // same N as used for encryption
));
// decryptedBytes should be equal to contents of inputFile
assert(utf8.decode(decryptedBytes) == inputFile.readAsStringSync());
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
Stream<Uint8List> decryptStream(
Stream<List<int>> data,
List<int> counter,
int length,
);