Stream Ciphers

Everything you need to know about PointyCastle's stream ciphers.

PointyCastle, as of now, supports three stream ciphers: ChaCha20 (original implementation), ChaCha20 (RFC-7539 implementation), and Salsa20. The parameters each needs are below in table form.

Because these are stream ciphers, no padding is necessary; each cipher can process any amount of data.

Constructing these algorithms is done either via the registry or via the class itself (see External API for more):

var salsa20 = StreamCipher('Salsa20'); //With registry
var chacha20 = ChaCha20Engine();

If you are using one of the ChaCha ciphers, you may also select the number of rounds.

var chacha20 = ChaCha20Engine.fromRounds(8); //8-round
var chacha7539 = StreamCipher('ChaCha7539/12') //12-round

Note: to use one of the ChaCha ciphers with the registry, you must append a /(rounds) to the end of the registry string. If you are unsure how many rounds to use, use 20.

Initializing looks pretty much the same with all these machines. Note the pattern of introducing parameters through the init method:

initialization.dart
var key = KeyParameter([]); //Replace [] with a 32 length Uint8List
var iv = []; //Replace [] with a 8 length Uint8List
var params = ParametersWithIV<KeyParameter>(key, iv);
var chacha20 = ChaCha20Engine();

// The bool below is inherited and represents whether this is for 
// encryption or decryption. Since stream ciphers have a symmetric 
// encryption process, it doesn't matter here.
chacha20.init(true, params);  

// Now, we can either encrypt or decrypt using this instance of
// ChaCha20Engine.

Finally, encryption (continuing from initialization.dart):

var encrypted = chacha20.process(data); // data is a Uint8List

Decryption looks the same as encryption, but you pass the encrypted data and get back the original data. (This also continues from initialization.dart):

// encrypted is a Uint8List returned from encryption
var data = chacha20.process(encrypted);

Isn't that simple? Most PointyCastle algorithms boil down like this: construct, initialize, and use.

Last updated