public static SigningPrivateKey createPrivateKey(BigInteger x, BigInteger p, BigInteger q, BigInteger g) throws NoSuchAlgorithmException, InvalidKeySpecException { if (x == null) { throw new IllegalArgumentException("x 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 DSAPrivateKeySpec(x, p, q, g); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); DSAPrivateKey privateKey = (DSAPrivateKey) keyFactory.generatePrivate(keySpec); return new DSASigningPrivateKey(privateKey); }
/** * 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 DSAPrivateKeySpec) { DSAPrivateKeySpec dsaPrivKeySpec = (DSAPrivateKeySpec)keySpec; return new DSAPrivateKey(dsaPrivKeySpec.getX(), dsaPrivKeySpec.getP(), dsaPrivKeySpec.getQ(), dsaPrivKeySpec.getG()); } else if (keySpec instanceof PKCS8EncodedKeySpec) { return new DSAPrivateKey (((PKCS8EncodedKeySpec)keySpec).getEncoded()); } else { throw new InvalidKeySpecException ("Inappropriate key specification"); } } catch (InvalidKeyException e) { throw new InvalidKeySpecException ("Inappropriate key specification: " + e.getMessage()); } }
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(); } }
private DsaPrivateKey initFromJson() throws KeyczarException { this.publicKey.initFromJson(); BigInteger localBigInteger1 = new BigInteger(Base64Coder.decodeWebSafe(this.x)); BigInteger localBigInteger2 = new BigInteger(Base64Coder.decodeWebSafe(this.publicKey.p)); BigInteger localBigInteger3 = new BigInteger(Base64Coder.decodeWebSafe(this.publicKey.q)); BigInteger localBigInteger4 = new BigInteger(Base64Coder.decodeWebSafe(this.publicKey.g)); try { this.jcePrivateKey = ((DSAPrivateKey)KeyFactory.getInstance("DSA").generatePrivate(new DSAPrivateKeySpec(localBigInteger1, localBigInteger2, localBigInteger3, localBigInteger4))); return this; } catch (GeneralSecurityException localGeneralSecurityException) { throw new KeyczarException(localGeneralSecurityException); } }
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); }
/** * This method generates a DSAPrivateKey object from the provided key specification. * * @param * keySpec - the specification (key material) for the DSAPrivateKey. * * @return * a DSAPrivateKey object * * @throws InvalidKeySpecException * if "keySpec" is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec */ protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec != null) { if (keySpec instanceof DSAPrivateKeySpec) { return new DSAPrivateKeyImpl((DSAPrivateKeySpec) keySpec); } if (keySpec instanceof PKCS8EncodedKeySpec) { return new DSAPrivateKeyImpl((PKCS8EncodedKeySpec) keySpec); } } throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec"); }
/** * Test for constructor */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "DSAPrivateKeySpec", args = {java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class, java.math.BigInteger.class} ) public final void testDSAPrivateKeySpec() { KeySpec ks = new DSAPrivateKeySpec( new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4")); assertTrue(ks instanceof DSAPrivateKeySpec); }
/** * getG() test */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getG", args = {} ) public final void testGetG() { DSAPrivateKeySpec dpks = new DSAPrivateKeySpec( new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4")); assertEquals(4, dpks.getG().intValue()); }
/** * getP() test */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getP", args = {} ) public final void testGetP() { DSAPrivateKeySpec dpks = new DSAPrivateKeySpec( new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4")); assertEquals(2, dpks.getP().intValue()); }
/** * getQ() test */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getQ", args = {} ) public final void testGetQ() { DSAPrivateKeySpec dpks = new DSAPrivateKeySpec( new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4")); assertEquals(3, dpks.getQ().intValue()); }
/** * getX() test */ @TestTargetNew( level = TestLevel.COMPLETE, notes = "", method = "getX", args = {} ) public final void testGetX() { DSAPrivateKeySpec dpks = new DSAPrivateKeySpec( new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4")); assertEquals(1, dpks.getX().intValue()); }
public Ssh2DsaPrivateKey(BigInteger p, BigInteger q, BigInteger g, BigInteger x, BigInteger y) throws SshException { try { KeyFactory kf = JCEProvider .getProviderForAlgorithm(JCEAlgorithms.JCE_DSA) == null ? KeyFactory .getInstance(JCEAlgorithms.JCE_DSA) : KeyFactory .getInstance(JCEAlgorithms.JCE_DSA, JCEProvider .getProviderForAlgorithm(JCEAlgorithms.JCE_DSA)); DSAPrivateKeySpec spec = new DSAPrivateKeySpec(x, p, q, g); prv = (DSAPrivateKey) kf.generatePrivate(spec); pub = new Ssh2DsaPublicKey(p, q, g, y); } catch (Throwable e) { throw new SshException(e); } }
/** * This method generates a DSAPrivateKey object from the provided key specification. * * @param * keySpec - the specification (key material) for the DSAPrivateKey. * * @return * a DSAPrivateKey object * * @throws InvalidKeySpecException * if "keySpec" is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec */ protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec != null) { if (keySpec instanceof DSAPrivateKeySpec) { return new DSAPrivateKeyImpl((DSAPrivateKeySpec) keySpec); } if (keySpec instanceof PKCS8EncodedKeySpec) { return new DSAPrivateKeyImpl((PKCS8EncodedKeySpec) keySpec); } } throw new InvalidKeySpecException(Messages.getString("security.19C")); //$NON-NLS-1$ }
private void testHMacDeterministic() throws Exception { DSAPrivateKeySpec privKeySpec = new DSAPrivateKeySpec(new BigInteger("411602CB19A6CCC34494D79D98EF1E7ED5AF25F7", 16), new BigInteger("86F5CA03DCFEB225063FF830A0C769B9DD9D6153AD91D7CE27F787C43278B447" + "E6533B86B18BED6E8A48B784A14C252C5BE0DBF60B86D6385BD2F12FB763ED88" + "73ABFD3F5BA2E0A8C0A59082EAC056935E529DAF7C610467899C77ADEDFC846C" + "881870B7B19B2B58F9BE0521A17002E3BDD6B86685EE90B3D9A1B02B782B1779", 16), new BigInteger("996F967F6C8E388D9E28D01E205FBA957A5698B1", 16), new BigInteger("07B0F92546150B62514BB771E2A0C0CE387F03BDA6C56B505209FF25FD3C133D" + "89BBCD97E904E09114D9A7DEFDEADFC9078EA544D2E401AEECC40BB9FBBF78FD" + "87995A10A1C27CB7789B594BA7EFB5C4326A9FE59A070E136DB77175464ADCA4" + "17BE5DCE2F40D10A46A3A3943F26AB7FD9C0398FF8C76EE0A56826A8A88F1DBD", 16)); KeyFactory keyFact = KeyFactory.getInstance("DSA", "BC"); PrivateKey privKey = keyFact.generatePrivate(privKeySpec); doTestHMACDetDSASample("SHA1withDETDSA", privKey, new BigInteger("2E1A0C2562B2912CAAF89186FB0F42001585DA55", 16), new BigInteger("29EFB6B0AFF2D7A68EB70CA313022253B9A88DF5", 16)); doTestHMACDetDSASample("SHA224withDETDSA", privKey, new BigInteger("4BC3B686AEA70145856814A6F1BB53346F02101E", 16), new BigInteger("410697B92295D994D21EDD2F4ADA85566F6F94C1", 16)); doTestHMACDetDSASample("SHA256withDETDSA", privKey, new BigInteger("81F2F5850BE5BC123C43F71A3033E9384611C545", 16), new BigInteger("4CDD914B65EB6C66A8AAAD27299BEE6B035F5E89", 16)); doTestHMACDetDSASample("SHA384withDETDSA", privKey, new BigInteger("07F2108557EE0E3921BC1774F1CA9B410B4CE65A", 16), new BigInteger("54DF70456C86FAC10FAB47C1949AB83F2C6F7595", 16)); doTestHMACDetDSASample("SHA512withDETDSA", privKey, new BigInteger("16C3491F9B8C3FBBDD5E7A7B667057F0D8EE8E1B", 16), new BigInteger("02C36A127A7B89EDBB72E4FFBC71DABC7D4FC69C", 16)); }
/** * The method generates a DSAPrivateKey object from the provided key specification. * * @param * keySpec - the specification (key material) for the DSAPrivateKey. * * @return * a DSAPrivateKey object * * @throws InvalidKeySpecException * if "keySpec" is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec */ protected PrivateKey engineGeneratePrivate(KeySpec keySpec) throws InvalidKeySpecException { if (keySpec != null) { if (keySpec instanceof DSAPrivateKeySpec) { return new DSAPrivateKeyImpl((DSAPrivateKeySpec) keySpec); } if (keySpec instanceof PKCS8EncodedKeySpec) { return new DSAPrivateKeyImpl((PKCS8EncodedKeySpec) keySpec); } } throw new InvalidKeySpecException(Messages.getString("security.19C")); //$NON-NLS-1$ }
protected PrivateKey engineGeneratePrivate( KeySpec keySpec) throws InvalidKeySpecException { if (keySpec instanceof DSAPrivateKeySpec) { return new BCDSAPrivateKey((DSAPrivateKeySpec)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 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()); } }
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); }