/**
   * Create a session key, encrypt it with the given request and claimDef. Return a serialized JSON
   * node with the session key and challenge.
   *
   * @param request Request from user: Base64 encoded bytes of the request element.
   * @param claimDef Claim definition to use: JSON encoded
   * @return
   * @throws Exception
   */
  public String createChallange(String request, String claimDef) throws Exception {

    ObjectMapper mapper = new ObjectMapper();
    ObjectNode claimDefOn = (ObjectNode) mapper.readTree(claimDef);
    IdentityClaimDefinition idClaimDef = new IdentityClaimDefinition(claimDefOn);

    byte[] reqElemBytes = Base64.decode(request);
    Element reqElem = idClaimDef.getParams().getPairing().getG1().newElement();
    reqElem.setFromBytes(reqElemBytes);

    Element sessionKey =
        idClaimDef.getParams().getPairing().getGT().newRandomElement().getImmutable();

    // Encrypt session key
    Encrypt encrypt = new Encrypt();
    encrypt.init(idClaimDef.getParams());
    AECipherTextBlock ct = encrypt.doEncrypt(sessionKey, reqElem);

    JsonNode rootNode = mapper.createObjectNode();
    ObjectNode on = (ObjectNode) rootNode;

    on.put(idClaimDef.getName(), ct.serializeJSON());
    String sk = new String(Base64.encode(sessionKey.toBytes()));
    sk = sk.replaceAll(" ", "");
    on.put("SessionKey", sk);

    return on.toString();
  }
  public String createChallangeNAClaims(String req, String claimDefs, int size) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode claimDefNodes = (ArrayNode) mapper.readTree(claimDefs);

    req = req.replaceAll("\"", "");
    byte[] reqElemBytes = Base64.decode(req);

    Element reqElem = null;
    ArrayList<IdentityClaimDefinition> icds = new ArrayList<IdentityClaimDefinition>();

    for (int i = 0; i < size; i++) {
      String onVal = claimDefNodes.get(i).getTextValue();

      ObjectNode claimDefOn = (ObjectNode) mapper.readTree(onVal);
      IdentityClaimDefinition idClaimDef = new IdentityClaimDefinition(claimDefOn);
      icds.add(idClaimDef);

      if (reqElem == null) {
        Pairing pairing = idClaimDef.getParams().getPairing();
        reqElem = pairing.getG1().newElement();
        reqElem.setFromBytes(reqElemBytes);
        //				System.out.println(reqElem);
      }
    }

    Pairing pairing = icds.get(0).getParams().getPairing();
    Field gt = pairing.getGT();
    Element sessionKey = gt.newRandomElement().getImmutable();
    Element sessionKeyOrig = sessionKey.getImmutable();
    // System.out.println("Key: " + sessionKey);

    JsonNode rootNode = mapper.createObjectNode();
    ObjectNode on = (ObjectNode) rootNode;
    Encrypt encrypt = new Encrypt();

    for (int i = 0; i < size; i++) {
      IdentityClaimDefinition claimDef = icds.get(i);

      Element share = null;
      if (i < (size - 1)) {
        share = gt.newRandomElement().getImmutable();
        sessionKey = sessionKey.sub(share).getImmutable();
      } else {
        // Last one should be the remaining part of session key
        share = sessionKey;
      }

      encrypt.init(claimDef.getParams());
      // System.out.println("Part : " + i + " : " + share);
      AECipherTextBlock ct = encrypt.doEncrypt(share, reqElem);

      on.put(claimDef.getName(), ct.serializeJSON());
    }

    //		System.out.println(sessionKeyOrig);
    String sk = new String(Base64.encode(sessionKeyOrig.toBytes()));
    sk = sk.replaceAll(" ", "");
    on.put("SessionKey", sk);
    return on.toString();
  }
 public void run() {
   Encrypt e = new Encrypt();
   e.init(this.params);
   AECipherTextBlock ct = e.doEncrypt(this.share, this.req);
   this.on.put(this.claimName, ct.serializeJSON());
 }