List of usage examples for java.security PublicKey getEncoded
public byte[] getEncoded();
From source file:com.sixsq.slipstream.cookie.CryptoUtils.java
static private String savePublicKey(PublicKey publicKey) throws GeneralSecurityException { byte[] publicKeyBytes = publicKey.getEncoded(); String publicKeyStr = new Base64().encodeToString(publicKeyBytes); return publicKeyStr; }
From source file:org.cloudfoundry.identity.uaa.oauth.jwk.JsonWebKey.java
public static String pemEncodePublicKey(PublicKey publicKey) { String begin = "-----BEGIN PUBLIC KEY-----\n"; String end = "\n-----END PUBLIC KEY-----"; byte[] data = publicKey.getEncoded(); String base64encoded = new String(new Base64(false).encode(data)); return begin + base64encoded + end; }
From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Saves the key pair to the disk.//from w w w.j a va 2 s . co m * * @param directory * The directory to save to * @param keyPair * The key pair to save * @throws Exception * If an error occurs */ public static void save(File directory, KeyPair keyPair) throws Exception { PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // Store the public key. X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(publicKey.getEncoded()); FileOutputStream out = null; try { out = new FileOutputStream(directory + "/public.key"); out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } // Store the private key. PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); try { out = new FileOutputStream(directory + "/private.key"); out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes()); } finally { try { out.close(); } catch (Exception exception) { // ignore } } }
From source file:net.arccotangent.pacchat.filesystem.KeyManager.java
private static void saveKeys(PrivateKey privkey, PublicKey pubkey) { km_log.i("Saving keys to disk."); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubkey.getEncoded()); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privkey.getEncoded()); try {/*from w w w. ja va 2s . com*/ km_log.i(pubkeyFile.createNewFile() ? "Creation of public key file successful." : "Creation of public key file failed!"); FileOutputStream pubOut = new FileOutputStream(pubkeyFile); pubOut.write(Base64.encodeBase64(pubSpec.getEncoded())); pubOut.flush(); pubOut.close(); } catch (IOException e) { km_log.e("Error while saving public key!"); e.printStackTrace(); } try { km_log.i(privkeyFile.createNewFile() ? "Creation of private key file successful." : "Creation of private key file failed!"); FileOutputStream privOut = new FileOutputStream(privkeyFile); privOut.write(Base64.encodeBase64(privSpec.getEncoded())); privOut.flush(); privOut.close(); } catch (IOException e) { km_log.e("Error while saving private key!"); e.printStackTrace(); } km_log.i("Finished saving keys to disk. Operation appears successful."); }
From source file:net.arccotangent.pacchat.filesystem.KeyManager.java
@SuppressWarnings("ResultOfMethodCallIgnored") public static void saveKeyByIP(String ip_address, PublicKey publicKey) { km_log.i("Saving public key for " + ip_address); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(publicKey.getEncoded()); File pubFile = new File(installationPath + File.separator + ip_address + ".pub"); km_log.i("Deleting old key if it exists."); if (pubFile.exists()) pubFile.delete();/*from w w w . java 2 s .co m*/ try { km_log.i(pubFile.createNewFile() ? "Creation of public key file successful." : "Creation of public key file failed!"); FileOutputStream pubOut = new FileOutputStream(pubFile); pubOut.write(Base64.encodeBase64(pubSpec.getEncoded())); pubOut.flush(); pubOut.close(); } catch (IOException e) { km_log.e("Error while saving public key for " + ip_address + "!"); e.printStackTrace(); } }
From source file:net.link.util.test.pkix.PkiTestUtils.java
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed") private static SubjectKeyIdentifier createSubjectKeyId(PublicKey publicKey) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(publicKey.getEncoded()); SubjectPublicKeyInfo info = new SubjectPublicKeyInfo((ASN1Sequence) new ASN1InputStream(bais).readObject()); return new SubjectKeyIdentifier(info); }
From source file:net.link.util.test.pkix.PkiTestUtils.java
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed") private static AuthorityKeyIdentifier createAuthorityKeyId(PublicKey publicKey) throws IOException { ByteArrayInputStream bais = new ByteArrayInputStream(publicKey.getEncoded()); SubjectPublicKeyInfo info = new SubjectPublicKeyInfo((ASN1Sequence) new ASN1InputStream(bais).readObject()); return new AuthorityKeyIdentifier(info); }
From source file:org.apache.sshd.server.PublickeyAuthenticatorUtils.java
/** * @param entries A {@link Collection} of {@link CryptoKeyEntry}-ies * @return A {@link PublickeyAuthenticator} that matches the received encoded * public key bytes to one of the authorized keys published by the user * @see CryptoKeyEntry#readAuthorizedKeys(File) *//*from ww w .j av a 2 s .c om*/ public static final PublickeyAuthenticator authorizedKeysAuthenticator( Collection<? extends CryptoKeyEntry> entries) { final Map<String, ? extends Collection<CryptoKeyEntry>> keysMap = ExtendedMapUtils.mapCollectionMultiValues( CryptoKeyEntry.USERNAME_EXTRACTOR, ExtendedCollectionUtils.<CryptoKeyEntry>linkedListFactory(), entries); return new PublickeyAuthenticator() { @Override public boolean authenticate(String username, PublicKey key, ServerSession session) { Collection<CryptoKeyEntry> keySet = keysMap.get(username); if (ExtendedCollectionUtils.isEmpty(keySet)) { return false; } final byte[] keyBytes = key.getEncoded(); if (ArrayUtils.isEmpty(keyBytes)) { return false; // TODO consider throwing an exception } CryptoKeyEntry entry = CollectionUtils.find(keySet, new AbstractExtendedPredicate<CryptoKeyEntry>(CryptoKeyEntry.class) { @Override public boolean evaluate(CryptoKeyEntry e) { byte[] entryBytes = e.getKeyData(); if (Arrays.equals(keyBytes, entryBytes)) { return true; } else { return false; // debug breakpoint; } } }); if (entry == null) { return false; } else { return true; } } }; }
From source file:com.znsx.util.licence.LicenceUtil.java
/** * ??// w w w . j a v a 2s . c om * * @param seed * ?? * @return * @throws Exception */ public static Map<String, String> generateKey(String seed) throws Exception { Map<String, String> map = new HashMap<String, String>(2); KeyPairGenerator keygen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = new SecureRandom(); random.setSeed(seed.getBytes("utf8")); keygen.initialize(1024, random); KeyPair keyPair = keygen.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); Base64 base64 = new Base64(); String publicKeyString = new String(base64.encode(publicKey.getEncoded()), "utf8"); String privateKeyString = new String(base64.encode(privateKey.getEncoded()), "utf8"); // BASE64Encoder encoder = new BASE64Encoder(); // map.put("public", encoder.encode(publicKey.getEncoded())); // map.put("private", encoder.encode(privateKey.getEncoded())); map.put("public", publicKeyString); map.put("private", privateKeyString); System.out.println("publicKey: " + map.get("public")); System.out.println("privateKey: " + map.get("private")); return map; }
From source file:org.kaaproject.kaa.common.endpoint.security.KeyUtil.java
/** * Saves public and private keys to specified streams. * * @param keyPair the key pair//from w w w .j a v a 2s . c om * @param privateKeyOutput the private key output stream * @param publicKeyOutput the public key output stream * @throws IOException Signals that an I/O exception has occurred. */ public static void saveKeyPair(KeyPair keyPair, OutputStream privateKeyOutput, OutputStream publicKeyOutput) throws IOException { PrivateKey privateKey = keyPair.getPrivate(); PublicKey publicKey = keyPair.getPublic(); // Store Public Key. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); publicKeyOutput.write(x509EncodedKeySpec.getEncoded()); // Store Private Key. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); privateKeyOutput.write(pkcs8EncodedKeySpec.getEncoded()); }