public void init(int mode, byte[] key, byte[] iv) throws Exception{
    String pad="NoPadding";      
    byte[] tmp;
    if(iv.length>ivsize){
      tmp=new byte[ivsize];
      System.arraycopy(iv, 0, tmp, 0, tmp.length);
      iv=tmp;
    }
    if(key.length>bsize){
      tmp=new byte[bsize];
      System.arraycopy(key, 0, tmp, 0, tmp.length);
      key=tmp;
    }

    try{
      SecretKeySpec keyspec=new SecretKeySpec(key, "AES");
      cipher=javax.crypto.Cipher.getInstance("AES/CTR/"+pad);
      cipher.init((mode==ENCRYPT_MODE?
                   javax.crypto.Cipher.ENCRYPT_MODE:
                   javax.crypto.Cipher.DECRYPT_MODE),
                  keyspec, new IvParameterSpec(iv));
    }
    catch(Exception e){
      cipher=null;
      throw e;
    }
  }