Using block ciphers - ivan-zaera/cipher GitHub Wiki

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

    var key = new Uint8List.fromList(
      [0x00,0x11,0x22,0x33,0x44,0x55,0x66,0x77,0x88,0x99,0xAA,0xBB,0xCC,0xDD,0xEE,0xFF] 
    );
    var params = new KeyParameter( key );
    var cipher = new BlockCipher( "AES" )
      ..init( true, params )
    ;

    var plainText = inputPlainText();
    
    var cipherText = cipher.process( plainText );

    outputCipherText( cipherText );

In general, you construct the BlockCipher 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 (AES) it consists of the secret key but, for other algorithms, it can specify more data (like, for instance, an initialization vector). 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 processBlock() method which doesn't create the output buffer. Keep in mind that the buffers must be of the corresponding block size (you can get the block size programmatically by calling blockSize getter on the cipher).

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.