Node:Cipher Example, Previous:The CipherFactory Class, Up:Ciphers



Example

The following example transforms the plaintext to the ciphertext, and the ciphertext back to the plaintext, using the AES in electronic codebook mode with no padding. Note also the classes for cipher modes and padding schemes for more complex constructions.

     IBlockCipher cipher = CipherFactory.getInstance("AES");
     Map attributes = new HashMap();
     attributes.put(IBlockCipher.CIPHER_BLOCK_SIZE, new Integer(16));
     attributes.put(IBlockCipher.KEY_MATERIAL, key_bytes);
     cipher.init(attributes);
     int bs = cipher.currentBlockSize();
     
     for (int i = 0; i + bs < pt.length; i += bs)
        {
           cipher.encryptBlock(pt, i, ct, i);
        }
     
     for (int i = 0; i + bs < cpt.length; i += bs)
        {
           cipher.decryptBlock(ct, i, cpt, i);
        }