List of usage examples for java.security.spec DSAPublicKeySpec DSAPublicKeySpec
public DSAPublicKeySpec(BigInteger y, BigInteger p, BigInteger q, BigInteger g)
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);//from www . java 2 s. com KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g); PublicKey publicKey1 = keyFactory.generatePublic(publicKeySpec); }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); keyGen.initialize(1024);//ww w. ja va2 s . c om KeyPair keypair = keyGen.genKeyPair(); DSAPrivateKey privateKey = (DSAPrivateKey) keypair.getPrivate(); DSAPublicKey publicKey = (DSAPublicKey) keypair.getPublic(); DSAParams dsaParams = privateKey.getParams(); BigInteger p = dsaParams.getP(); BigInteger q = dsaParams.getQ(); BigInteger g = dsaParams.getG(); BigInteger x = privateKey.getX(); BigInteger y = publicKey.getY(); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g); PublicKey publicKey1 = keyFactory.generatePublic(publicKeySpec); KeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g); PrivateKey privateKey1 = keyFactory.generatePrivate(privateKeySpec); byte[] buffer = new byte[1024]; Signature sig = Signature.getInstance(privateKey1.getAlgorithm()); sig.initSign(privateKey1); sig.update(buffer, 0, buffer.length); byte[] signature = sig.sign(); sig = Signature.getInstance(publicKey1.getAlgorithm()); sig.initVerify(publicKey1); sig.update(buffer, 0, buffer.length); sig.verify(signature); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); random.setSeed(101L);//from w w w . java 2 s . c o m keyGen.initialize(1024, random); KeyPair keypair = keyGen.generateKeyPair(); KeyFactory kfactory = KeyFactory.getInstance("DSA"); DSAPublicKeySpec kspec = (DSAPublicKeySpec) kfactory.getKeySpec(keypair.getPublic(), DSAPublicKeySpec.class); System.out.println(keypair.getPublic()); FileOutputStream fos = new FileOutputStream("publicKeys"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(kspec.getY()); oos.writeObject(kspec.getP()); oos.writeObject(kspec.getQ()); oos.writeObject(kspec.getG()); FileInputStream fin = new FileInputStream("publicKeys"); ObjectInputStream ois = new ObjectInputStream(fin); BigInteger Y = (BigInteger) ois.readObject(); BigInteger P = (BigInteger) ois.readObject(); BigInteger Q = (BigInteger) ois.readObject(); BigInteger G = (BigInteger) ois.readObject(); DSAPublicKeySpec keyspec = new DSAPublicKeySpec(Y, P, Q, G); PublicKey pkey = kfactory.generatePublic(keyspec); System.out.println(pkey); }
From source file:me.brjannc.plugins.sushi.AuthorizedKeysDecoder.java
public PublicKey decodePublicKey(String keyLine) throws Exception { bytes = null;/*from ww w.j a va 2 s . co m*/ pos = 0; // look for the Base64 encoded part of the line to decode // both ssh-rsa and ssh-dss begin with "AAAA" due to the length bytes for (String part : keyLine.split(" ")) { if (part.startsWith("AAAA")) { bytes = Base64.decodeBase64(part); break; } } if (bytes == null) { throw new IllegalArgumentException("no Base64 part to decode"); } String type = decodeType(); if (type.equals("ssh-rsa")) { BigInteger e = decodeBigInt(); BigInteger m = decodeBigInt(); RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e); return KeyFactory.getInstance("RSA").generatePublic(spec); } else if (type.equals("ssh-dss")) { BigInteger p = decodeBigInt(); BigInteger q = decodeBigInt(); BigInteger g = decodeBigInt(); BigInteger y = decodeBigInt(); DSAPublicKeySpec spec = new DSAPublicKeySpec(y, p, q, g); return KeyFactory.getInstance("DSA").generatePublic(spec); } else { throw new IllegalArgumentException("unknown type " + type); } }
From source file:com.adito.security.pki.dsa.SshDssPublicKey.java
/** * Creates a new SshDssPublicKey object. * * @param key/* w ww.jav a 2s. c om*/ * * @throws InvalidKeyException */ public SshDssPublicKey(byte[] key) throws InvalidKeyException { try { DSAPublicKeySpec dsaKey; // Extract the key information ByteArrayReader bar = new ByteArrayReader(key); String header = bar.readString(); if (!header.equals(getAlgorithmName())) { throw new InvalidKeyException(); } BigInteger p = bar.readBigInteger(); BigInteger q = bar.readBigInteger(); BigInteger g = bar.readBigInteger(); BigInteger y = bar.readBigInteger(); dsaKey = new DSAPublicKeySpec(y, p, q, g); KeyFactory kf = KeyFactory.getInstance("DSA"); pubkey = (DSAPublicKey) kf.generatePublic(dsaKey); } catch (Exception e) { throw new InvalidKeyException(); } }
From source file:com.sshtools.j2ssh.transport.publickey.dsa.SshDssPublicKey.java
/** * Creates a new SshDssPublicKey object. * * @param key//from w w w . j av a 2 s . c om * * @throws InvalidSshKeyException */ public SshDssPublicKey(byte[] key) throws InvalidSshKeyException { try { DSAPublicKeySpec dsaKey; // Extract the key information ByteArrayReader bar = new ByteArrayReader(key); String header = bar.readString(); if (!header.equals(getAlgorithmName())) { throw new InvalidSshKeyException(); } BigInteger p = bar.readBigInteger(); BigInteger q = bar.readBigInteger(); BigInteger g = bar.readBigInteger(); BigInteger y = bar.readBigInteger(); dsaKey = new DSAPublicKeySpec(y, p, q, g); KeyFactory kf = KeyFactory.getInstance("DSA"); pubkey = (DSAPublicKey) kf.generatePublic(dsaKey); } catch (Exception e) { throw new InvalidSshKeyException(); } }
From source file:com.adito.security.pki.dsa.SshDssPrivateKey.java
/** * * * @return/* w w w . ja v a 2 s . co m*/ */ public SshPublicKey getPublicKey() { try { DSAPublicKeySpec spec = new DSAPublicKeySpec(getY(), prvkey.getParams().getP(), prvkey.getParams().getQ(), prvkey.getParams().getG()); KeyFactory kf = KeyFactory.getInstance("DSA"); return new SshDssPublicKey((DSAPublicKey) kf.generatePublic(spec)); } catch (Exception e) { return null; } }
From source file:com.sshtools.j2ssh.transport.publickey.dsa.SshDssPrivateKey.java
/** * * * @return// w ww . j ava 2 s . c o m */ public SshPublicKey getPublicKey() { try { DSAPublicKeySpec spec = new DSAPublicKeySpec(getY(), prvkey.getParams().getP(), prvkey.getParams().getQ(), prvkey.getParams().getG()); KeyFactory kf = KeyFactory.getInstance("DSA"); return new SshDssPublicKey((DSAPublicKey) kf.generatePublic(spec)); } catch (Exception e) { return null; } }
From source file:net.adamcin.httpsig.testutil.KeyTestUtil.java
public static KeyPair getKeyPairFromProperties(String parentName, String keyName) { InputStream is = null;/*from w w w .j av a 2 s. com*/ try { is = KeyTestUtil.class.getResourceAsStream("/" + parentName + "/" + keyName + ".properties"); Properties props = new Properties(); props.load(is); if (TYPE_RSA.equals(props.getProperty(P_TYPE))) { RSAPrivateKeySpec privSpec = null; if (props.getProperty(RSA_P) != null && props.getProperty(RSA_Q) != null && props.getProperty(RSA_U) != null) { privSpec = new RSAPrivateCrtKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E)), new BigInteger(props.getProperty(RSA_D)), new BigInteger(props.getProperty(RSA_P)), new BigInteger(props.getProperty(RSA_Q)), new BigInteger(props.getProperty(RSA_PE)), new BigInteger(props.getProperty(RSA_QE)), new BigInteger(props.getProperty(RSA_U))); } else { privSpec = new RSAPrivateKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_D))); } RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E))); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } else if (TYPE_DSA.equals(props.getProperty(P_TYPE))) { DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(new BigInteger(props.getProperty(DSA_X)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(new BigInteger(props.getProperty(DSA_Y)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } } catch (Exception e) { LOGGER.error("Failed to read properties", e); } finally { IOUtils.closeQuietly(is); } return null; }
From source file:com.bitdubai.fermat_api.layer.all_definition.crypto.asymmetric.util.PublicKeyReaderUtil.java
/** * <p>Decodes a DSA public key according to the SSH standard from the * data <code>_buffer</code> based on <b>NIST's FIPS-186</b>. The values of * the DSA public key specification are read in the order * <ul>// w w w. j a v a 2 s. co m * <li>prime p</li> * <li>sub-prime q</li> * <li>base g</li> * <li>public key y</li> * </ul> * With the specification the related DSA public key is generated.</p> * * @param _buffer SSH2 data buffer where the type of the key is already * read * @return DSA public key instance * @throws PublicKeyParseException if the SSH2 public key blob could not be * decoded * @see DSAPublicKeySpec * @see <a href="http://en.wikipedia.org/wiki/Digital_Signature_Algorithm">Digital Signature Algorithm on Wikipedia</a> * @see <a href="http://tools.ietf.org/html/rfc4253#section-6.6">RFC 4253 Section 6.6</a> */ private static PublicKey decodeDSAPublicKey(final SSH2DataBuffer _buffer) throws PublicKeyParseException { final BigInteger p = _buffer.readMPint(); final BigInteger q = _buffer.readMPint(); final BigInteger g = _buffer.readMPint(); final BigInteger y = _buffer.readMPint(); try { final KeyFactory dsaKeyFact = KeyFactory.getInstance("DSA"); final DSAPublicKeySpec dsaPubSpec = new DSAPublicKeySpec(y, p, q, g); return dsaKeyFact.generatePublic(dsaPubSpec); } catch (final Exception e) { throw new PublicKeyParseException( PublicKeyParseException.ErrorCode.SSH2DSA_ERROR_DECODING_PUBLIC_KEY_BLOB, e); } }