public static VerifyingPublicKey createPublicKey(BigInteger y, BigInteger p, BigInteger q, BigInteger g) throws NoSuchAlgorithmException, InvalidKeySpecException { if (y == null) { throw new IllegalArgumentException("n must not be null"); } if (p == null) { throw new IllegalArgumentException("p must not be null"); } if (q == null) { throw new IllegalArgumentException("q must not be null"); } if (g == null) { throw new IllegalArgumentException("g must not be null"); } KeySpec keySpec = new DSAPublicKeySpec(y, p, q, g); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); DSAPublicKey publicKey = (DSAPublicKey) keyFactory.generatePublic(keySpec); return new DSAVerifyingPublicKey(publicKey); }
/** * Internal method to create a new key with inherited key parameters. * * @param keyValueKey key from which to obtain key value * @param keyParamsKey key from which to obtain key parameters * @return new public key having value and parameters * @throws CertPathValidatorException if keys are not appropriate types * for this operation */ static PublicKey makeInheritedParamsKey(PublicKey keyValueKey, PublicKey keyParamsKey) throws CertPathValidatorException { if (!(keyValueKey instanceof DSAPublicKey) || !(keyParamsKey instanceof DSAPublicKey)) throw new CertPathValidatorException("Input key is not " + "appropriate type for " + "inheriting parameters"); DSAParams params = ((DSAPublicKey)keyParamsKey).getParams(); if (params == null) throw new CertPathValidatorException("Key parameters missing"); try { BigInteger y = ((DSAPublicKey)keyValueKey).getY(); KeyFactory kf = KeyFactory.getInstance("DSA"); DSAPublicKeySpec ks = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG()); return kf.generatePublic(ks); } catch (GeneralSecurityException e) { throw new CertPathValidatorException("Unable to generate key with" + " inherited parameters: " + e.getMessage(), e); } }
private void saveKey() { PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); KeyFactory keyFactory; try { keyFactory = KeyFactory.getInstance("DSA"); DSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec( privateKey, DSAPrivateKeySpec.class); DSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(publicKey, DSAPublicKeySpec.class); this.account.setKey("otr_x", privateKeySpec.getX().toString(16)); this.account.setKey("otr_g", privateKeySpec.getG().toString(16)); this.account.setKey("otr_p", privateKeySpec.getP().toString(16)); this.account.setKey("otr_q", privateKeySpec.getQ().toString(16)); this.account.setKey("otr_y", publicKeySpec.getY().toString(16)); } catch (final NoSuchAlgorithmException | InvalidKeySpecException e) { e.printStackTrace(); } }
final void initFromJson() throws KeyczarException { BigInteger localBigInteger1 = new BigInteger(Base64Coder.decodeWebSafe(this.y)); BigInteger localBigInteger2 = new BigInteger(Base64Coder.decodeWebSafe(this.p)); BigInteger localBigInteger3 = new BigInteger(Base64Coder.decodeWebSafe(this.q)); BigInteger localBigInteger4 = new BigInteger(Base64Coder.decodeWebSafe(this.g)); try { this.jcePublicKey = ((DSAPublicKey)KeyFactory.getInstance("DSA").generatePublic(new DSAPublicKeySpec(localBigInteger1, localBigInteger2, localBigInteger3, localBigInteger4))); DSAParams localDSAParams = this.jcePublicKey.getParams(); byte[][] arrayOfByte = new byte[4][]; arrayOfByte[0] = Util.stripLeadingZeros(localDSAParams.getP().toByteArray()); arrayOfByte[1] = Util.stripLeadingZeros(localDSAParams.getQ().toByteArray()); arrayOfByte[2] = Util.stripLeadingZeros(localDSAParams.getG().toByteArray()); arrayOfByte[3] = Util.stripLeadingZeros(this.jcePublicKey.getY().toByteArray()); System.arraycopy(Util.prefixHash(arrayOfByte), 0, this.hash, 0, this.hash.length); return; } catch (GeneralSecurityException localGeneralSecurityException) { throw new KeyczarException(localGeneralSecurityException); } }
private static PublicKey toDSAPublicKey(KEYBase r) throws IOException, GeneralSecurityException, MalformedKeyException { DNSInput in = new DNSInput(r.getKey()); int t = in.readU8(); if (t > 8) throw new MalformedKeyException(r); BigInteger q = readBigInteger(in, 20); BigInteger p = readBigInteger(in, 64 + t*8); BigInteger g = readBigInteger(in, 64 + t*8); BigInteger y = readBigInteger(in, 64 + t*8); KeyFactory factory = KeyFactory.getInstance("DSA"); return factory.generatePublic(new DSAPublicKeySpec(y, p, q, g)); }
private void parseDsaKeyPair(byte[] blob) throws GeneralSecurityException, IOException { ASN1InputStream ain = new ASN1InputStream(new ByteArrayInputStream( blob)); ASN1Sequence seq = (ASN1Sequence) ain.readObject(); ain.close(); ASN1Integer p = (ASN1Integer) seq.getObjectAt(1); ASN1Integer q = (ASN1Integer) seq.getObjectAt(2); ASN1Integer g = (ASN1Integer) seq.getObjectAt(3); ASN1Integer y = (ASN1Integer) seq.getObjectAt(4); ASN1Integer x = (ASN1Integer) seq.getObjectAt(5); DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(x.getValue(), p.getValue(), q.getValue(), g.getValue()); DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(y.getValue(), p.getValue(), q.getValue(), g.getValue()); KeyFactory kf = KeyFactory.getInstance("DSA"); privateKey = kf.generatePrivate(privSpec); publicKey = kf.generatePublic(pubSpec); }
public void regenerateLocalPublicKey(KeyFactory factory, String fullUserId, DSAPrivateKey privKey) { String userId = Address.stripResource(fullUserId); BigInteger x = privKey.getX(); DSAParams params = privKey.getParams(); BigInteger y = params.getG().modPow(x, params.getP()); DSAPublicKeySpec keySpec = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG()); PublicKey pubKey; try { pubKey = factory.generatePublic(keySpec); storeLocalPublicKey(userId, pubKey); } catch (Exception e) { throw new RuntimeException(e); } }
public PublicKey getPublicKey() throws GeneralSecurityException { if (privateKey instanceof DSAPrivateKey) { DSAPrivateKey dsa = (DSAPrivateKey) privateKey; DSAParams params = dsa.getParams(); BigInteger g = params.getG(); BigInteger p = params.getP(); BigInteger q = params.getQ(); BigInteger x = dsa.getX(); BigInteger y = q.modPow( x, p ); DSAPublicKeySpec dsaKeySpec = new DSAPublicKeySpec(y, p, q, g); return KeyFactory.getInstance("DSA").generatePublic(dsaKeySpec); } else if (privateKey instanceof RSAPrivateCrtKey) { RSAPrivateCrtKey rsa = (RSAPrivateCrtKey) privateKey; RSAPublicKeySpec rsaKeySpec = new RSAPublicKeySpec( rsa.getModulus(), rsa.getPublicExponent() ); return KeyFactory.getInstance("RSA").generatePublic(rsaKeySpec); } else { throw new GeneralSecurityException("Not an RSA or DSA key"); } }
/** * This method generates a DSAPublicKey object from the provided key specification. * * @param * keySpec - the specification (key material) for the DSAPublicKey. * * @return * a DSAPublicKey object * * @throws InvalidKeySpecException * if "keySpec" is neither DSAPublicKeySpec nor X509EncodedKeySpec */ protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec != null) { if (keySpec instanceof DSAPublicKeySpec) { return new DSAPublicKeyImpl((DSAPublicKeySpec) keySpec); } if (keySpec instanceof X509EncodedKeySpec) { return new DSAPublicKeyImpl((X509EncodedKeySpec) keySpec); } } throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec"); }
/** * Test for <code>DSAPublicKeySpec</code> ctor */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "DSAPublicKeySpec", args = {java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class} ) public final void testDSAPublicKeySpec() { KeySpec ks = new DSAPublicKeySpec( new BigInteger("1"), // y new BigInteger("2"), // p new BigInteger("3"), // q new BigInteger("4"));// g assertTrue(ks instanceof DSAPublicKeySpec); }
/** * Test for <code>getG</code> method */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getG", args = {} ) public final void testGetG() { DSAPublicKeySpec dpks = new DSAPublicKeySpec( new BigInteger("1"), // y new BigInteger("2"), // p new BigInteger("3"), // q new BigInteger("4"));// g assertEquals(4, dpks.getG().intValue()); }
/** * Test for <code>getP</code> method */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getP", args = {} ) public final void testGetP() { DSAPublicKeySpec dpks = new DSAPublicKeySpec( new BigInteger("1"), // y new BigInteger("2"), // p new BigInteger("3"), // q new BigInteger("4"));// g assertEquals(2, dpks.getP().intValue()); }
/** * Test for <code>getQ</code> method */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getQ", args = {} ) public final void testGetQ() { DSAPublicKeySpec dpks = new DSAPublicKeySpec( new BigInteger("1"), // y new BigInteger("2"), // p new BigInteger("3"), // q new BigInteger("4"));// g assertEquals(3, dpks.getQ().intValue()); }
/** * Test for <code>getY</code> method */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getY", args = {} ) public final void testGetY() { DSAPublicKeySpec dpks = new DSAPublicKeySpec( new BigInteger("1"), // y new BigInteger("2"), // p new BigInteger("3"), // q new BigInteger("4"));// g assertEquals(1, dpks.getY().intValue()); }
/** * This method generates a DSAPublicKey object from the provided key specification. * * @param * keySpec - the specification (key material) for the DSAPublicKey. * * @return * a DSAPublicKey object * * @throws InvalidKeySpecException * if "keySpec" is neither DSAPublicKeySpec nor X509EncodedKeySpec */ protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec != null) { if (keySpec instanceof DSAPublicKeySpec) { return new DSAPublicKeyImpl((DSAPublicKeySpec) keySpec); } if (keySpec instanceof X509EncodedKeySpec) { return new DSAPublicKeyImpl((X509EncodedKeySpec) keySpec); } } throw new InvalidKeySpecException(Messages.getString("security.19D")); //$NON-NLS-1$ }
private static PublicKey decodeDSAPublicKey(SSH2DataBuffer buffer) throws PublicKeyParseException { BigInteger p = buffer.readMPint(); BigInteger q = buffer.readMPint(); BigInteger g = buffer.readMPint(); BigInteger y = buffer.readMPint(); try { KeyFactory dsaKeyFact = KeyFactory.getInstance("DSA"); DSAPublicKeySpec dsaPubSpec = new DSAPublicKeySpec(y, p, q, g); return dsaKeyFact.generatePublic(dsaPubSpec); } catch (Exception e) { throw new PublicKeyParseException( ErrorCode.SSH2DSA_ERROR_DECODING_PUBLIC_KEY_BLOB, e); } }
/** * The method generates a DSAPublicKey object from the provided key specification. * * @param * keySpec - the specification (key material) for the DSAPublicKey. * * @return * a DSAPublicKey object * * @throws InvalidKeySpecException * if "keySpec" is neither DSAPublicKeySpec nor X509EncodedKeySpec */ protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec != null) { if (keySpec instanceof DSAPublicKeySpec) { return new DSAPublicKeyImpl((DSAPublicKeySpec) keySpec); } if (keySpec instanceof X509EncodedKeySpec) { return new DSAPublicKeyImpl((X509EncodedKeySpec) keySpec); } } throw new InvalidKeySpecException(Messages.getString("security.19D")); //$NON-NLS-1$ }
public void regenerateLocalPublicKey(KeyFactory factory, String fullUserId, DSAPrivateKey privKey) { String userId = Address.stripResource(fullUserId); BigInteger x = privKey.getX(); DSAParams params = privKey.getParams(); BigInteger y = params.getG().modPow(x, params.getP()); DSAPublicKeySpec keySpec = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG()); PublicKey pubKey; try { pubKey = factory.generatePublic(keySpec); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } storeLocalPublicKey(userId, pubKey); }
protected PublicKey engineGeneratePublic( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof DSAPublicKeySpec) { return new BCDSAPublicKey((DSAPublicKeySpec)keySpec); } return super.engineGeneratePublic(keySpec); }
public static DSAPublicKey generateDSAPublicKey(DSAPublicKeySpec keySpec) throws InvalidKeySpecException { ParamUtil.requireNonNull("keySpec", keySpec); KeyFactory kf = getKeyFactory("DSA"); synchronized (kf) { return (DSAPublicKey) kf.generatePublic(keySpec); } }
/** * Generates a public key object from the provided key specification * (key material). * * @param keySpec the specification (key material) of the public key * * @return the public key * * @exception InvalidKeySpecException if the given key specification * is inappropriate for this key factory to produce a public key. */ protected PublicKey engineGeneratePublic(KeySpec keySpec) throws InvalidKeySpecException { try { if (keySpec instanceof DSAPublicKeySpec) { DSAPublicKeySpec dsaPubKeySpec = (DSAPublicKeySpec)keySpec; if (SERIAL_INTEROP) { return new DSAPublicKey(dsaPubKeySpec.getY(), dsaPubKeySpec.getP(), dsaPubKeySpec.getQ(), dsaPubKeySpec.getG()); } else { return new DSAPublicKeyImpl(dsaPubKeySpec.getY(), dsaPubKeySpec.getP(), dsaPubKeySpec.getQ(), dsaPubKeySpec.getG()); } } else if (keySpec instanceof X509EncodedKeySpec) { if (SERIAL_INTEROP) { return new DSAPublicKey (((X509EncodedKeySpec)keySpec).getEncoded()); } else { return new DSAPublicKeyImpl (((X509EncodedKeySpec)keySpec).getEncoded()); } } else { throw new InvalidKeySpecException ("Inappropriate key specification"); } } catch (InvalidKeyException e) { throw new InvalidKeySpecException ("Inappropriate key specification: " + e.getMessage()); } }
/** * 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 java.security.interfaces.DSAPublicKey) { // Check if key originates from this factory if (key instanceof sun.security.provider.DSAPublicKey) { return key; } // Convert key to spec DSAPublicKeySpec dsaPubKeySpec = engineGetKeySpec(key, DSAPublicKeySpec.class); // Create key from spec, and return it return engineGeneratePublic(dsaPubKeySpec); } else if (key instanceof java.security.interfaces.DSAPrivateKey) { // Check if key originates from this factory if (key instanceof sun.security.provider.DSAPrivateKey) { return key; } // Convert key to spec DSAPrivateKeySpec dsaPrivKeySpec = engineGetKeySpec(key, DSAPrivateKeySpec.class); // Create key from spec, and return it return engineGeneratePrivate(dsaPrivKeySpec); } else { throw new InvalidKeyException("Wrong algorithm type"); } } catch (InvalidKeySpecException e) { throw new InvalidKeyException("Cannot translate key: " + e.getMessage()); } }