encryptBytes abstract method

Future<Uint8List> encryptBytes(
  1. List<int> data,
  2. List<int> iv
)

Encrypt data with this AesCbcSecretKey using AES in Cipher Block Chaining mode, as specified in NIST SP800-38A.

The operation requires a 16 bytes initalization vector iv. The iv needs not be secret, but it must be unpredictable. In particular, for a given plaintext it must not be possible to predict the iv that will be used to encrypt the plaintext. For detailed discussion of the initialization vector requirements for AES-CBC, see Appendix C of NIST SP800-38A.

Encrypted output is always padded in PKCS#7 mode, as described in RFC 2315 Section 10.3 step 2. This padding is stripped when the message is decrypted.

Example

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

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

// Use a unique IV for each message.
final iv = Uint8List(16);
fillRandomBytes(iv);

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

// Decrypt message (requires the same iv)
print(utf8.decode(await k.decryptBytes(c, iv))); // hello world

Implementation

Future<Uint8List> encryptBytes(List<int> data, List<int> iv);