Using stream ciphers - ivan-zaera/cipher GitHub Wiki

To use a stream cipher for encryption you can use the following code:

    var keyBytes = new Uint8List.fromList(
      [0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF] 
    );
    var key = new KeyParameter( keyBytes );
    var iv = new Uint8List.fromList( 
      [0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77] 
    );
    var params = new ParametersWithIV( key, iv );

    var cipher = new StreamCipher( "Salsa20" )
      ..init( true, params )
    ;

    var plainText = inputPlainText();

    var cipherText = cipher.process( plainText );

    outputCipherText( cipherText );

In general, you construct the StreamCipher with the factory constructor which receives the standard algorithm name and then init() the cipher with its corresponding CipherParameters. See Algorithm nomenclature for documentation on standard algorithm names.

The type of CipherParameters to be used depends on the algorithm. For the example (Salsa20) it consists of the secret key plus an initialization vector but, for other algorithms, it can change. See the Table of provided algorithms to get information on which CipherParameters to use for the algorithm of your choice.

To encrypt you make a call to process() with the plain text and the cipher text buffer is returned. Alternatively, if you want more performance, you can use the processBytes() method which doesn't create the output buffer.

To decrypt you also use process() or processBlock() but pass false as the first parameter when calling init().

Finally, you can reset an algorithm to its initial state with the reset() method.