List of usage examples for java.security KeyFactory generatePublic
public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException
From source file:SecureConnection.java
public byte[] getPublicKey(byte[] otherPubKeyBytes) throws Exception { KeyFactory keyFac = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(otherPubKeyBytes); PublicKey otherPubKey = keyFac.generatePublic(x509KeySpec); DHParameterSpec dhParamSpec = ((DHPublicKey) otherPubKey).getParams(); return this.getPublicKeyStep2(dhParamSpec); }
From source file:SecureConnection.java
private byte[] generateSecret(byte[] otherPubKeyBytes) throws Exception { KeyFactory keyFac = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(otherPubKeyBytes); PublicKey otherPubKey = keyFac.generatePublic(x509KeySpec); this.keyAgree.doPhase(otherPubKey, true); return keyAgree.generateSecret(); }
From source file:bftsmart.reconfiguration.util.RSAKeyLoaderO.java
private PublicKey getPublicKeyFromString(String key) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(key)); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); return publicKey; }
From source file:org.picketbox.json.key.RSAKey.java
/** * Convert to the JDK representation of a RSA Public Key * * @return/*from www . jav a 2 s.c o m*/ * @throws ProcessingException */ public RSAPublicKey convertToPublicKey() throws ProcessingException { BigInteger bigModulus = new BigInteger(1, massage(Base64.decode(mod))); BigInteger bigEx = new BigInteger(1, massage(Base64.decode(exp))); try { KeyFactory rsaKeyFactory = KeyFactory.getInstance("rsa"); RSAPublicKeySpec kspec = new RSAPublicKeySpec(bigModulus, bigEx); return (RSAPublicKey) rsaKeyFactory.generatePublic(kspec); } catch (Exception e) { throw PicketBoxJSONMessages.MESSAGES.processingException(e); } }
From source file:com.adito.security.pki.dsa.SshDssPrivateKey.java
/** * * * @return//w w w . j a va 2s . 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:com.adito.security.pki.dsa.SshDssPublicKey.java
/** * Creates a new SshDssPublicKey object. * * @param key/*from w w w . ja v a 2 s . c o m*/ * * @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:ie.peternagy.jcrypto.algo.EllipticCurveWrapper.java
/** * Try load the keys from disk/*from w w w . j a v a 2 s . c o m*/ */ public void tryLoadKeys() { try { byte[] publicBytes = Hex .decodeHex(new String(FileAccessUtil.readFromDisk(getKeyFilePath(false))).toCharArray()); byte[] privateBytes = Hex .decodeHex(new String(FileAccessUtil.readFromDisk(getKeyFilePath(true))).toCharArray()); KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC"); publicKey = fact.generatePublic(new X509EncodedKeySpec(publicBytes)); privateKey = fact.generatePrivate(new PKCS8EncodedKeySpec(privateBytes)); } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | DecoderException ex) { Logger.getLogger(EllipticCurveWrapper.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.buzzcoders.security.cryptoutils.asymmetric.AbstractAsymmetricEncryptionModule.java
public PublicKey loadPublicKey(String path) { FileInputStream fis = null;/*from ww w .j a va 2 s . c o m*/ try { File filePublicKey = new File(path); fis = new FileInputStream(path); byte[] pubKey = new byte[(int) filePublicKey.length()]; fis.read(pubKey); KeyFactory keyFactory = KeyFactory.getInstance(getAlgorithm()); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKey); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); return publicKey; } catch (Exception e) { LOG.error("An error occurred while loading the public key from disk.", e); } finally { IOUtils.closeQuietly(fis); } return null; }
From source file:com.sshtools.j2ssh.transport.publickey.dsa.SshDssPublicKey.java
/** * Creates a new SshDssPublicKey object. * * @param key/*from w w w . ja v 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:sec_algo.commonenc.java
/** * Encrypts the AES key to a file using an RSA public key *///from ww w.jav a 2 s .c o m public void saveKey(File out, File publicKeyFile) { try { // read public key to be used to encrypt the AES key byte[] encodedKey = new byte[(int) publicKeyFile.length()]; new FileInputStream(publicKeyFile).read(encodedKey); // create public key X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedKey); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey pk = kf.generatePublic(publicKeySpec); // write AES key pkCipher.init(Cipher.ENCRYPT_MODE, pk); CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), pkCipher); os.write(key); os.close(); } catch (Exception e) { e.printStackTrace(); } }