Exemplo n.º 1
0
  public void main(Provider p) throws Exception {
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", p);
    kpg.initialize(512);
    KeyPair kp = kpg.generateKeyPair();
    PrivateKey privateKey = kp.getPrivate();
    PublicKey publicKey = kp.getPublic();
    Signature sig = Signature.getInstance("MD5withRSA", p);
    byte[] data = new byte[10 * 1024];
    new Random().nextBytes(data);
    sig.initSign(privateKey);
    sig.initSign(privateKey);
    sig.update(data);
    sig.initSign(privateKey);
    sig.update(data);
    byte[] signature = sig.sign();
    sig.update(data);
    sig.initSign(privateKey);
    sig.update(data);
    sig.sign();
    sig.sign();
    sig.initSign(privateKey);
    sig.sign();

    System.out.println("All tests passed");
  }
Exemplo n.º 2
0
  public void run() {
    try {
      ObjectInputStream ois = new ObjectInputStream(s.getInputStream());
      ObjectOutputStream oos = new ObjectOutputStream(s.getOutputStream());

      BigInteger bg = dhSpec.getG();
      BigInteger bp = dhSpec.getP();
      oos.writeObject(bg);
      oos.writeObject(bp);

      KeyPairGenerator kpg = KeyPairGenerator.getInstance("DH");
      kpg.initialize(1024);
      KeyPair kpa = (KeyPair) ois.readObject();
      KeyAgreement dh = KeyAgreement.getInstance("DH");
      KeyPair kp = kpg.generateKeyPair();

      oos.writeObject(kp);

      dh.init(kp.getPrivate());
      Key pk = dh.doPhase(kpa.getPublic(), true);

      MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
      byte[] rawbits = sha256.digest(dh.generateSecret());

      Cipher c = Cipher.getInstance(CIPHER_MODE);
      SecretKey key = new SecretKeySpec(rawbits, 0, 16, "AES");
      byte ivbits[] = (byte[]) ois.readObject();
      IvParameterSpec iv = new IvParameterSpec(ivbits);
      c.init(Cipher.DECRYPT_MODE, key, iv);

      Mac m = Mac.getInstance("HmacSHA1");
      SecretKey mackey = new SecretKeySpec(rawbits, 16, 16, "HmacSHA1");
      m.init(mackey);

      byte ciphertext[], cleartext[], mac[];
      try {
        while (true) {
          ciphertext = (byte[]) ois.readObject();
          mac = (byte[]) ois.readObject();
          if (Arrays.equals(mac, m.doFinal(ciphertext))) {
            cleartext = c.update(ciphertext);
            System.out.println(ct + " : " + new String(cleartext, "UTF-8"));
          } else {
            // System.exit(1);
            System.out.println(ct + "error");
          }
        }
      } catch (EOFException e) {
        cleartext = c.doFinal();
        System.out.println(ct + " : " + new String(cleartext, "UTF-8"));
        System.out.println("[" + ct + "]");
      } finally {
        if (ois != null) ois.close();
        if (oos != null) oos.close();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
Exemplo n.º 3
0
 private static KeyPairGenerator getInstance(Instance instance, String algorithm) {
   KeyPairGenerator kpg;
   if (instance.impl instanceof KeyPairGenerator) {
     kpg = (KeyPairGenerator) instance.impl;
   } else {
     KeyPairGeneratorSpi spi = (KeyPairGeneratorSpi) instance.impl;
     kpg = new Delegate(spi, algorithm);
   }
   kpg.provider = instance.provider;
   return kpg;
 }
Exemplo n.º 4
0
  /**
   * Generates the public/private key pair from the init params
   *
   * @throws Exception
   */
  public void initKeyPair() throws Exception {
    // generate keys according to the specified algorithms
    // generate publicKey and Private Key
    KeyPairGenerator KpairGen = null;
    if (asymProvider != null && !asymProvider.trim().isEmpty())
      KpairGen = KeyPairGenerator.getInstance(getAlgorithm(asymAlgorithm), asymProvider);
    else KpairGen = KeyPairGenerator.getInstance(getAlgorithm(asymAlgorithm));
    KpairGen.initialize(asymInit, new SecureRandom());
    Kpair = KpairGen.generateKeyPair();

    // set up the Cipher to decrypt secret key responses encrypted with our key

    if (asymProvider != null && !asymProvider.trim().isEmpty())
      asymCipher = Cipher.getInstance(asymAlgorithm, asymProvider);
    else asymCipher = Cipher.getInstance(asymAlgorithm);

    asymCipher.init(Cipher.DECRYPT_MODE, Kpair.getPrivate());
    log.debug("asym algo initialized");
  }