Package javax.crypto.spec

Examples of javax.crypto.spec.DHParameterSpec


        return elSpec;
    }
   
    public DHParameterSpec getParams()
    {
        return new DHParameterSpec(elSpec.getP(), elSpec.getG());
    }
View Full Code Here


        int         privateValueSize,
        BigInteger  g,
        BigInteger  p)
        throws Exception
    {
        DHParameterSpec  elParams = new DHParameterSpec(p, g, privateValueSize);
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ElGamal", "BC");
        byte[]           in = "This is a test".getBytes();

        keyGen.initialize(elParams);
       
        KeyPair         keyPair = keyGen.generateKeyPair();
        SecureRandom    rand = new SecureRandom();

        checkKeySize(privateValueSize, keyPair);

        Cipher  cipher = Cipher.getInstance("ElGamal", "BC");
       
        cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), rand);
       
        if (cipher.getOutputSize(in.length) != (size / 8) * 2)
        {
            fail("getOutputSize wrong on encryption");
        }

        byte[]  out = cipher.doFinal(in);
       
        cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
       
        if (cipher.getOutputSize(out.length) != (size / 8) - 1)
        {
            fail("getOutputSize wrong on decryption");
        }
       
        //
        // No Padding - maximum length
        //
        byte[]  modBytes = ((DHPublicKey)keyPair.getPublic()).getParams().getP().toByteArray();
        byte[]  maxInput = new byte[modBytes.length - 1];

        maxInput[0] |= 0x7f;

        cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), rand);

        out = cipher.doFinal(maxInput);

        cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());

        out = cipher.doFinal(out);

        if (!areEqual(out, maxInput))
        {
            fail("NoPadding test failed on decrypt expected " + new String(Hex.encode(maxInput)) + " got " + new String(Hex.encode(out)));
        }

        //
        // encrypt/decrypt
        //

        Cipher  c1 = Cipher.getInstance("ElGamal", "BC");
        Cipher  c2 = Cipher.getInstance("ElGamal", "BC");

        c1.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), rand);

        byte[]  out1 = c1.doFinal(in);

        c2.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());

        byte[]  out2 = c2.doFinal(out1);

        if (!areEqual(in, out2))
        {
            fail(size + " encrypt test failed");
        }
       
        //
        // encrypt/decrypt with update
        //
        int outLen = c1.update(in, 0, 2, out1, 0);
       
        outLen += c1.doFinal(in, 2, in.length - 2, out1, outLen);

        outLen = c2.update(out1, 0, 2, out2, 0);
       
        outLen += c2.doFinal(out1, 2, out1.length - 2, out2, outLen);

        if (!areEqual(in, out2))
        {
            fail(size + " encrypt with update test failed");
        }

        //
        // public key encoding test
        //
        byte[]                  pubEnc = keyPair.getPublic().getEncoded();
        KeyFactory              keyFac = KeyFactory.getInstance("ElGamal", "BC");
        X509EncodedKeySpec      pubX509 = new X509EncodedKeySpec(pubEnc);
        DHPublicKey             pubKey = (DHPublicKey)keyFac.generatePublic(pubX509);
        DHParameterSpec         spec = pubKey.getParams();

        if (!spec.getG().equals(elParams.getG()) || !spec.getP().equals(elParams.getP()))
        {
            fail(size + " bit public key encoding/decoding test failed on parameters");
        }

        if (!((DHPublicKey)keyPair.getPublic()).getY().equals(pubKey.getY()))
        {
            fail(size + " bit public key encoding/decoding test failed on y value");
        }

        //
        // public key serialisation test
        //
        ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
        ObjectOutputStream      oOut = new ObjectOutputStream(bOut);

        oOut.writeObject(keyPair.getPublic());

        ByteArrayInputStream   bIn = new ByteArrayInputStream(bOut.toByteArray());
        ObjectInputStream      oIn = new ObjectInputStream(bIn);

        pubKey = (DHPublicKey)oIn.readObject();
        spec = pubKey.getParams();

        if (!spec.getG().equals(elParams.getG()) || !spec.getP().equals(elParams.getP()))
        {
            fail(size + " bit public key serialisation test failed on parameters");
        }

        if (!((DHPublicKey)keyPair.getPublic()).getY().equals(pubKey.getY()))
        {
            fail(size + " bit public key serialisation test failed on y value");
        }

        //
        // private key encoding test
        //
        byte[]              privEnc = keyPair.getPrivate().getEncoded();
        PKCS8EncodedKeySpec privPKCS8 = new PKCS8EncodedKeySpec(privEnc);
        DHPrivateKey        privKey = (DHPrivateKey)keyFac.generatePrivate(privPKCS8);

        spec = privKey.getParams();

        if (!spec.getG().equals(elParams.getG()) || !spec.getP().equals(elParams.getP()))
        {
            fail(size + " bit private key encoding/decoding test failed on parameters");
        }

        if (!((DHPrivateKey)keyPair.getPrivate()).getX().equals(privKey.getX()))
        {
            fail(size + " bit private key encoding/decoding test failed on y value");
        }

        //
        // private key serialisation test
        //
        bOut = new ByteArrayOutputStream();
        oOut = new ObjectOutputStream(bOut);

        oOut.writeObject(keyPair.getPrivate());

        bIn = new ByteArrayInputStream(bOut.toByteArray());
        oIn = new ObjectInputStream(bIn);

        privKey = (DHPrivateKey)oIn.readObject();
        spec = privKey.getParams();

        if (!spec.getG().equals(elParams.getG()) || !spec.getP().equals(elParams.getP()))
        {
            fail(size + " bit private key serialisation test failed on parameters");
        }

        if (!((DHPrivateKey)keyPair.getPrivate()).getX().equals(privKey.getX()))
View Full Code Here

        if (!areEqual(encodeParams, encodeParams_2))
        {
            fail(this.getName() + ": encode/decode parameters failed");
        }

        DHParameterSpec elP = (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);

        testGP(size, 0, elP.getG(), elP.getP());
    }
View Full Code Here

            AlgorithmParameters params;

            try
            {
                params = AlgorithmParameters.getInstance("ElGamal", "BC");
                params.init(new DHParameterSpec(p.getP(), p.getG(), l));
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.getMessage());
            }
View Full Code Here

     * @throws InvalidAlgorithmParameterException
     */
    public KeyPair createKeys() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeySpecException {
        // initialize the parameter spec
        DHPublicKey publicKey = (DHPublicKey) this.getValueLinkPublicKey();
        DHParameterSpec dhParamSpec = publicKey.getParams();
        //Debug.logInfo(dhParamSpec.getP().toString() + " / " + dhParamSpec.getG().toString(), module);

        // create the public/private key pair using parameters defined by valuelink
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DH");
        keyGen.initialize(dhParamSpec);
View Full Code Here

     */
    public PrivateKey getPrivateKey() throws InvalidKeySpecException, NoSuchAlgorithmException {
        byte[] privateKeyBytes = this.getPrivateKeyBytes();

        // initialize the parameter spec
        DHParameterSpec dhParamSpec = this.getDHParameterSpec();

        // load the private key
        KeyFactory keyFactory = KeyFactory.getInstance("DH");
        BigInteger privateKeyInt = new BigInteger(privateKeyBytes);
        DHPrivateKeySpec dhPrivateSpec = new DHPrivateKeySpec(privateKeyInt, dhParamSpec.getP(), dhParamSpec.getG());
        PrivateKey privateKey = keyFactory.generatePrivate(dhPrivateSpec);

        return privateKey;
    }
View Full Code Here

        byte[] primeByte = StringUtil.fromHexString(primeHex);
        BigInteger prime = new BigInteger(1, primeByte); // force positive (unsigned)
        BigInteger generator = new BigInteger(genString);

        // initialize the parameter spec
        DHParameterSpec dhParamSpec = new DHParameterSpec(prime, generator, 1024);

        return dhParamSpec;
    }
View Full Code Here

           InvalidParameterSpecException,
           InvalidKeySpecException  {
    AlgorithmParameterGenerator pgen = AlgorithmParameterGenerator.getInstance("DH");
    pgen.init(512);
    AlgorithmParameters params = pgen.generateParameters();
    DHParameterSpec dhspec = (DHParameterSpec)params.getParameterSpec(DHParameterSpec.class);
    KeyPairGenerator keypairgen = KeyPairGenerator.getInstance("DH");
    keypairgen.initialize(dhspec);  
    keyPair = keypairgen.generateKeyPair();
    p = dhspec.getP();
    g = dhspec.getG();
    l = dhspec.getL();
  }
View Full Code Here

    int l,
    byte[] key)
      throws NoSuchAlgorithmException,
             InvalidAlgorithmParameterException,
             InvalidKeySpecException {
    DHParameterSpec spec = new DHParameterSpec(p,g,l);
    KeyPairGenerator keypairgen = KeyPairGenerator.getInstance("DH");
    keypairgen.initialize(spec);  
    keyPair = keypairgen.generateKeyPair();
    publicKey = decode(key);
  }
View Full Code Here

        this.factory = factory;
    }

    public byte[] getE() throws Exception {
        if (e == null) {
            DHParameterSpec dhSkipParamSpec = new DHParameterSpec(p, g);
            myKpairGen.initialize(dhSkipParamSpec);
            KeyPair myKpair = myKpairGen.generateKeyPair();
            myKeyAgree.init(myKpair.getPrivate());
            e = ((javax.crypto.interfaces.DHPublicKey) (myKpair.getPublic())).getY();
            e_array = e.toByteArray();
View Full Code Here

TOP

Related Classes of javax.crypto.spec.DHParameterSpec

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.