private IMode getMode(byte[] key, byte[] iv, int state) {
      IBlockCipher cipher = CipherFactory.getInstance(properties.get("cipher"));
      if (cipher == null) {
         throw new IllegalArgumentException("no such cipher: " + properties.get("cipher"));
      }
      int blockSize = cipher.defaultBlockSize();
      if (properties.containsKey("block-size")) {
         try {
            blockSize = Integer.parseInt(properties.get("block-size"));
         } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("bad block size: " + nfe.getMessage());
         }
      }
      IMode mode = ModeFactory.getInstance(properties.get("mode"), cipher, blockSize);
      if (mode == null) {
         throw new IllegalArgumentException("no such mode: " + properties.get("mode"));
      }

      HashMap modeAttr = new HashMap();
      modeAttr.put(IMode.KEY_MATERIAL, key);
      modeAttr.put(IMode.STATE, new Integer(state));
      modeAttr.put(IMode.IV, iv);
      try {
         mode.init(modeAttr);
      } catch (InvalidKeyException ike) {
         throw new IllegalArgumentException(ike.toString());
      }
      return mode;
   }