AesCtr class abstract
AES-CTR (counter mode) Cipher.
Available implementation
- In browsers,
BrowserAesCtr
is used by default. - Otherwise DartAesCtr is used by default.
- The package cryptography_flutter supports native implementations available in Android and iOS.
About the algorithm
- Three possible key lengths:
- 128 bits: AesCtr.with128bits
- 192 bits: AesCtr.with192bits
- 256 bits: AesCtr.with256bits
- Nonce length is 12 bytes by default. That means 4 bytes is used for
block counter.
- Because block is 16 bytes, maximum message size is 32 GB with a single nonce.
- You can choose another nonce length in the constructor if you need to.
- You must choose some macAlgorithm. If you are sure that you don't need one, use MacAlgorithm.empty.
Example
import 'package:better_cryptography/better_cryptography.dart';
Future<void> main() async {
final message = <int>[1,2,3];
// AES-CTR with 128 bit keys and HMAC-SHA256 authentication.
final algorithm = AesCtr.with128bits(
macAlgorithm: Hmac.sha256(),
);
final secretKey = await algorithm.newSecretKey();
final nonce = algorithm.newNonce();
// Encrypt
final secretBox = await algorithm.encrypt(
message,
secretKey: secretKey,
);
print('Nonce: ${secretBox.nonce}')
print('Ciphertext: ${secretBox.cipherText}')
print('MAC: ${secretBox.mac.bytes}')
// Decrypt
final clearText = await algorithm.encrypt(
secretBox,
secretKey: secretKey,
);
print('Cleartext: $clearText');
}
- Inheritance
-
- Object
- Cipher
- StreamingCipher
- AesCtr
- Implementers
Constructors
- AesCtr.constructor()
-
Constructor for classes that extend this class.
const
- AesCtr.with128bits({required MacAlgorithm macAlgorithm})
-
factory
- AesCtr.with192bits({required MacAlgorithm macAlgorithm})
-
factory
- AesCtr.with256bits({required MacAlgorithm macAlgorithm})
-
factory
Properties
- counterBits → int
-
Number of bits occupied by the counter.
no setter
- hashCode → int
-
The hash code for this object.
no setteroverride
- macAlgorithm → MacAlgorithm
-
Message authentication code (MacAlgorithm) used by the cipher.
no setterinherited
- nonceLength → int
-
Number of bytes in the nonce ("Initialization Vector", "IV", "salt").
no setteroverride
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- secretKeyLength → int
-
Number of bytes in the SecretKey.
no setterinherited
Methods
-
decrypt(
SecretBox secretBox, {required SecretKey secretKey, List< int> aad = const <int>[], int keyStreamIndex = 0}) → Future<List< int> > -
Decrypts a ciphertext.
inherited
-
encrypt(
List< int> clearText, {required SecretKey secretKey, List<int> ? nonce, List<int> aad = const <int>[], int keyStreamIndex = 0}) → Future<SecretBox> -
Encrypts a cleartext.
inherited
-
newNonce(
) → List< int> -
Generates a new nonce with the correct length (nonceLength).
inherited
-
newSecretKey(
) → Future< SecretKey> -
Generates a new SecretKey with the correct length (secretKeyLength).
inherited
-
newSecretKeyFromBytes(
List< int> bytes) → Future<SecretKey> -
Constructs a new SecretKey from the bytes.
inherited
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override