Exemple #1
0
  public boolean dec(String pubfile, String prvfile, String encfile, String decfile)
      throws Exception {
    byte[] plt, prv_byte, pub_byte;
    CpabeEncryptedFile ciphertext;
    BswabeCph cph;
    BswabePrv prv;
    BswabePub pub;

    /* get BswabePub from pubfile */
    pub_byte = Common.suckFile(pubfile);
    pub = new BswabePub();
    pub.initFromBuffer(pub_byte);

    /* read ciphertext */
    ciphertext = new CpabeEncryptedFile(pub, encfile);
    cph = ciphertext.getCph();

    /* get BswabePrv form prvfile */
    prv_byte = Common.suckFile(prvfile);
    prv = new BswabePrv(pub);
    prv.initFromBuffer(prv_byte);

    BswabeElementBoolean beb = Bswabe.dec(pub, prv, cph);
    if (beb.isSuccessful() == false) {
      return false;
    }

    plt = AESCoder.decrypt(beb.getElement().toBytes(), ciphertext.getAesBuf());
    Common.spitFile(decfile, plt);

    return true;
  }
Exemple #2
0
  public boolean enc(String pubfile, String policy, String inputfile, String encfile)
      throws Exception {
    CpabeEncryptedFile encFile;
    BswabePub pub;
    BswabeCph cph;
    BswabeCphKey keyCph;
    byte[] plt, aesBuf, pub_byte;
    Element m;

    /* get BswabePub from pubfile */
    pub_byte = Common.suckFile(pubfile);
    pub = new BswabePub();
    pub.initFromBuffer(pub_byte);

    /* encrypt */
    keyCph = Bswabe.enc(pub, policy);
    cph = keyCph.cph;
    m = keyCph.key;

    if (cph == null) {
      return false;
    }

    /* write file to encrypted */
    plt = Common.suckFile(inputfile);
    aesBuf = AESCoder.encrypt(m.toBytes(), plt);

    encFile = new CpabeEncryptedFile(cph, aesBuf);
    Common.spitFile(encfile, encFile.serialize());

    return true;
  }
  @Test
  public void httpclientTest() {
    // 创建默认的httpClient实例.
    CloseableHttpClient httpclient = HttpClients.createDefault();

    // 创建httppost
    HttpPost httppost = new HttpPost(POST_URL);

    String secrectKey = null;
    try {
      secrectKey = AESCoder.initKeyString();
    } catch (Exception e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    try {

      byte[] secrectKyeByteArray = secrectKey.getBytes();
      byte[] encryptByPublicKey = RSACoder.encryptByPublicKey(secrectKyeByteArray, publicKey);

      httppost.setEntity(new ByteArrayEntity(encryptByPublicKey));
    } catch (Exception e1) {
      // TODO Auto-generated catch block
      e1.printStackTrace();
    }

    System.out.println("executing request " + httppost.getURI());

    CloseableHttpResponse response = null;
    try {
      response = httpclient.execute(httppost);

      //			if (entity != null) {
      //                System.out.println("--------------------------------------");
      //                String responseString = EntityUtils.toString(entity);
      //				System.out.println("Response content: " + responseString);
      //                System.out.println("--------------------------------------");

      try {

        byte[] input =
            AESCoder.decrypt(IOUtils.toByteArray(response.getEntity().getContent()), secrectKey);

        String serverData = new String(input);

        System.out.println("server data: " + serverData);

        org.junit.Assert.assertNotNull(input);
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      //            }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      try {
        response.close();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }

    try {
      httpclient.close();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }