Node:Mode Example, Previous:The ModeFactory Class, Up:Modes


The following example encrypts and decrypts a byte array with the AES in CFB mode. See the next chapter on padding for instances where the input is not a multiple of the cipher or mode's block size.

     IMode mode = ModeFactory.getInstance("CFB", "AES", 16);
     Map attributes = new HashMap();
     // These attributes are defined in gnu.crypto.cipher.IBlockCipher.
     attributes.put(IMode.KEY_MATERIAL, key_bytes);
     attributes.put(IMode.CIPHER_BLOCK_SIZE, new Integer(16));
     // These attributes are defined in IMode.
     attributes.put(IMode.STATE, new Integer(IMode.ENCRYPTION));
     attributes.put(IMode.IV, iv_bytes);
     int bs = mode.currentBlockSize();
     for (int i = 0; i + bs < pt.length; i += bs)
           mode.update(pt, i, ct, i);
     attributes.put(IMode.STATE, new Integer(IMode.DECRYPTION);
     for (int i = 0; i + bs < ct.length; i += bs)
           mode.update(ct, i, cpt, i);