/** * Generates a private key object from the provided key specification * (key material). * * @param keySpec the specification (key material) of the private key * * @return the private key * * @exception InvalidKeySpecException if the given key specification * is inappropriate for this key factory to produce a private key. */ protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { try { if (keySpec instanceof DHPrivateKeySpec) { DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec; return new DHPrivateKey(dhPrivKeySpec.getX(), dhPrivKeySpec.getP(), dhPrivKeySpec.getG()); } else if (keySpec instanceof PKCS8EncodedKeySpec) { return new DHPrivateKey (((PKCS8EncodedKeySpec)keySpec).getEncoded()); } else { throw new InvalidKeySpecException ("Inappropriate key specification"); } } catch (InvalidKeyException e) { throw new InvalidKeySpecException ("Inappropriate key specification", e); } }
/** * Generates a private key object from the provided key specification * (key material). * * @param keySpec the specification (key material) of the private key * * @return the private key * * @exception InvalidKeySpecException if the given key specification * is inappropriate for this key factory to produce a private key. */ protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { try { if (keySpec instanceof DHPrivateKeySpec) { DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)keySpec; return new DHPrivateKey(dhPrivKeySpec.getX(), dhPrivKeySpec.getP(), dhPrivKeySpec.getG()); } else if (keySpec instanceof PKCS8EncodedKeySpec) { return new DHPrivateKey (((PKCS8EncodedKeySpec)keySpec).getEncoded()); } else { throw new InvalidKeySpecException ("Inappropriate key specification"); } } catch (InvalidKeyException e) { throw new InvalidKeySpecException ("Inappropriate key specification"); } }
/** * DHPrivateKeySpec class testing. Tests the equivalence of parameters * specified in the constructor with the values returned by getters. */ public void testDHPrivateKeySpec() { BigInteger[] xs = {new BigInteger("-1000000000000"), BigInteger.ZERO, BigInteger.ONE, new BigInteger("1000000000000")}; BigInteger[] ps = {new BigInteger("-1000000000000"), BigInteger.ZERO, BigInteger.ONE, new BigInteger("1000000000000")}; BigInteger[] gs = {new BigInteger("-1000000000000"), BigInteger.ZERO, BigInteger.ONE, new BigInteger("1000000000000")}; for (int i=0; i<ps.length; i++) { DHPrivateKeySpec dhpks = new DHPrivateKeySpec(xs[i], ps[i], gs[i]); assertEquals("The value returned by getX() must be " + "equal to the value specified in the constructor", dhpks.getX(), xs[i]); assertEquals("The value returned by getP() must be " + "equal to the value specified in the constructor", dhpks.getP(), ps[i]); assertEquals("The value returned by getG() must be " + "equal to the value specified in the constructor", dhpks.getG(), gs[i]); } }
public byte[] createNodeKey(byte[] pubKeyNode) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, IllegalStateException { // add this public key node to agreement KeyFactory keyFac = KeyFactory.getInstance("DH"); BigInteger y = new BigInteger(1, pubKeyNode); DHPublicKeySpec spec = new DHPublicKeySpec(y, sP, sG); PublicKey nodePubKey = keyFac.generatePublic(spec); mKA.doPhase(nodePubKey, true); // complete this phase of agreement by generating secret BigInteger x = new BigInteger(1, mKA.generateSecret()); BigInteger v = sG.modPow(x, sP); DHPrivateKeySpec specX = new DHPrivateKeySpec(x, sP, sG); PrivateKey nodePrivKey = keyFac.generatePrivate(specX); mKA.doPhase(nodePubKey, true); mKA = KeyAgreement.getInstance("DH"); mKA.init(nodePrivKey); return getBytes(v); }
protected PrivateKey engineGeneratePrivate( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof DHPrivateKeySpec) { return new BCDHPrivateKey((DHPrivateKeySpec)keySpec); } return super.engineGeneratePrivate(keySpec); }
protected PrivateKey engineGeneratePrivate( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof ElGamalPrivateKeySpec) { return new BCElGamalPrivateKey((ElGamalPrivateKeySpec)keySpec); } else if (keySpec instanceof DHPrivateKeySpec) { return new BCElGamalPrivateKey((DHPrivateKeySpec)keySpec); } return super.engineGeneratePrivate(keySpec); }
/** * Translates a key object, whose provider may be unknown or potentially * untrusted, into a corresponding key object of this key factory. * * @param key the key whose provider is unknown or untrusted * * @return the translated key * * @exception InvalidKeyException if the given key cannot be processed by * this key factory. */ protected Key engineTranslateKey(Key key) throws InvalidKeyException { try { if (key instanceof javax.crypto.interfaces.DHPublicKey) { // Check if key originates from this factory if (key instanceof com.sun.crypto.provider.DHPublicKey) { return key; } // Convert key to spec DHPublicKeySpec dhPubKeySpec = engineGetKeySpec(key, DHPublicKeySpec.class); // Create key from spec, and return it return engineGeneratePublic(dhPubKeySpec); } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) { // Check if key originates from this factory if (key instanceof com.sun.crypto.provider.DHPrivateKey) { return key; } // Convert key to spec DHPrivateKeySpec dhPrivKeySpec = engineGetKeySpec(key, DHPrivateKeySpec.class); // Create key from spec, and return it return engineGeneratePrivate(dhPrivKeySpec); } else { throw new InvalidKeyException("Wrong algorithm type"); } } catch (InvalidKeySpecException e) { throw new InvalidKeyException("Cannot translate key", e); } }
public void bad10() throws Exception { BigInteger bigInteger = new BigInteger("12345", 5); new DSAPrivateKeySpec(bigInteger, null, null, null); new DSAPublicKeySpec(bigInteger, null, bigInteger, null); // report once new DHPrivateKeySpec(bigInteger, null, null); new DHPublicKeySpec(bigInteger, null, null); new ECPrivateKeySpec(bigInteger, null); new RSAPrivateKeySpec(bigInteger, null); new RSAMultiPrimePrivateCrtKeySpec(bigInteger, null, null, null, null, null, null, null, null); new RSAPrivateCrtKeySpec(bigInteger, null, null, null, null, null, null, null); new RSAPublicKeySpec(bigInteger, null); new DSAPublicKeyImpl(bigInteger, null, null, null); }
/** * Get merchant Private Key * @return PrivateKey object for the merchant */ 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; }
/** * @param spec an instance of {@link DHPrivateKeySpec} to decode. * @return an instance of a {@link DHPrivateKey} constructed from the * information in the designated key-specification. * @throws InvalidKeySpecException if no concrete implementation of the * {@link DHPrivateKey} interface exists at run-time, or if an * exception occurs during its instantiation. */ private DHPrivateKey decodeDHPrivateKey(DHPrivateKeySpec spec) throws InvalidKeySpecException { BigInteger p = spec.getP(); BigInteger g = spec.getG(); BigInteger x = spec.getX(); Object[] params = new Object[] {Integer.valueOf(Registry.PKCS8_ENCODING_ID), null, p, g, x}; Object obj = invokeConstructor("gnu.javax.crypto.key.dh.GnuDHPrivateKey", params); return (DHPrivateKey) obj; }
public KeyPair generateDHKeyPair() throws OtrCryptoException { // Generate a AsymmetricCipherKeyPair using BC. DHParameters dhParams = new DHParameters(MODULUS, GENERATOR, null, DH_PRIVATE_KEY_MINIMUM_BIT_LENGTH); DHKeyGenerationParameters params = new DHKeyGenerationParameters(new SecureRandom(), dhParams); DHKeyPairGenerator kpGen = new DHKeyPairGenerator(); kpGen.init(params); AsymmetricCipherKeyPair pair = kpGen.generateKeyPair(); // Convert this AsymmetricCipherKeyPair to a standard JCE KeyPair. DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic(); DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate(); try { KeyFactory keyFac = KeyFactory.getInstance("DH"); DHPublicKeySpec pubKeySpecs = new DHPublicKeySpec(pub.getY(), MODULUS, GENERATOR); DHPublicKey pubKey = (DHPublicKey) keyFac.generatePublic(pubKeySpecs); DHParameters dhParameters = priv.getParameters(); DHPrivateKeySpec privKeySpecs = new DHPrivateKeySpec(priv.getX(), dhParameters.getP(), dhParameters.getG()); DHPrivateKey privKey = (DHPrivateKey) keyFac.generatePrivate(privKeySpecs); return new KeyPair(pubKey, privKey); } catch (Exception e) { throw new OtrCryptoException(e); } }
/** * Translates a key object, whose provider may be unknown or potentially * untrusted, into a corresponding key object of this key factory. * * @param key the key whose provider is unknown or untrusted * * @return the translated key * * @exception InvalidKeyException if the given key cannot be processed by * this key factory. */ protected Key engineTranslateKey(Key key) throws InvalidKeyException { try { if (key instanceof javax.crypto.interfaces.DHPublicKey) { // Check if key originates from this factory if (key instanceof com.sun.crypto.provider.DHPublicKey) { return key; } // Convert key to spec DHPublicKeySpec dhPubKeySpec = (DHPublicKeySpec)engineGetKeySpec (key, DHPublicKeySpec.class); // Create key from spec, and return it return engineGeneratePublic(dhPubKeySpec); } else if (key instanceof javax.crypto.interfaces.DHPrivateKey) { // Check if key originates from this factory if (key instanceof com.sun.crypto.provider.DHPrivateKey) { return key; } // Convert key to spec DHPrivateKeySpec dhPrivKeySpec = (DHPrivateKeySpec)engineGetKeySpec (key, DHPrivateKeySpec.class); // Create key from spec, and return it return engineGeneratePrivate(dhPrivKeySpec); } else { throw new InvalidKeyException("Wrong algorithm type"); } } catch (InvalidKeySpecException e) { throw new InvalidKeyException("Cannot translate key"); } }