List of usage examples for java.security KeyPair getPublic
public PublicKey getPublic()
From source file:com.boubei.tss.modules.license.LicenseFactory.java
/** * ???/* w w w . j a v a 2 s. c om*/ * ????hacker??license * @throws Exception */ public static void generateKey() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance(KEY_ALGORITHM); keyGen.initialize(1024, new SecureRandom()); KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); log.info("?"); DataOutputStream out = new DataOutputStream(new FileOutputStream(PUBLIC_KEY_FILE)); out.writeBytes(EasyUtils.encodeHex(pub.getEncoded())); out.close(); log.info("??" + PUBLIC_KEY_FILE); out = new DataOutputStream(new FileOutputStream(PRIVATE_KEY_FILE)); out.writeBytes(EasyUtils.encodeHex(priv.getEncoded())); out.close(); log.info("??" + PRIVATE_KEY_FILE); }
From source file:Main.java
/** * save private key and public key of a keypair in the directory * * @param dir/*from w w w .java2 s . com*/ * @param keyPair * @param name keys will be stored as name_private.key and name_public.key * @throws IOException */ public static void saveKeyPair(File dir, KeyPair keyPair, String name) throws IOException { // Store Public Key. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded()); FileOutputStream fos = new FileOutputStream(new File(dir, name + "_public.key")); fos.write(x509EncodedKeySpec.getEncoded()); fos.close(); // Store Private Key. PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(keyPair.getPrivate().getEncoded()); fos = new FileOutputStream(new File(dir, name + "_private.key")); fos.write(pkcs8EncodedKeySpec.getEncoded()); fos.close(); }
From source file:com.vmware.identity.rest.core.test.RequestSignerTest.java
@BeforeClass public static void setup() throws NoSuchAlgorithmException { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); generator.initialize(2048);//from ww w . j a v a2s .c o m KeyPair pair = generator.generateKeyPair(); publicKey = pair.getPublic(); privateKey = pair.getPrivate(); }
From source file:com.vexsoftware.votifier.crypto.RSAIO.java
/** * Saves the key pair to the disk./*from w w w . j a v a2s. c o 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 = new FileOutputStream(directory + "/public.key"); out.write(DatatypeConverter.printBase64Binary(publicSpec.getEncoded()).getBytes()); out.close(); // Store the private key. PKCS8EncodedKeySpec privateSpec = new PKCS8EncodedKeySpec(privateKey.getEncoded()); out = new FileOutputStream(directory + "/private.key"); out.write(DatatypeConverter.printBase64Binary(privateSpec.getEncoded()).getBytes()); out.close(); }
From source file:com.example.license.RSAUtil.java
/** * ?// ww w . j av a 2s. c om * * @param data * ? * @param key * * @return ?? */ public static String decrypt(String data, String seed) throws Exception { KeyPair keyPair = generatorKeyPair(seed); Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); // ?Cipher? cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic()); // ? return new String(cipher.doFinal(Base64.decodeBase64(data))); }
From source file:nu.yona.server.messaging.entities.MessageSource.java
public static MessageSource createInstance() { KeyPair pair = PublicKeyUtil.generateKeyPair(); MessageDestination messageDestination = MessageDestination.createInstance(pair.getPublic()); return new MessageSource(UUID.randomUUID(), pair.getPrivate(), messageDestination); }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ?/*from www. j a va 2 s .c o m*/ * * @throws Exception */ static void generatorKeyPair() throws Exception { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(ALGORITHM_RSA); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.generateKeyPair(); RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic(); byte[] keyBs = rsaPublicKey.getEncoded(); publicKey = encodeBase64(keyBs); System.out.println("?\r\n" + publicKey); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); keyBs = rsaPrivateKey.getEncoded(); privateKey = encodeBase64(keyBs); System.out.println("??\r\n" + privateKey); }
From source file:org.opentravel.schemacompiler.security.GenerateEncryptionKeys.java
/** * Generates the public and private key files used for encrypting and decrypting passwords. *//*from w w w. ja va2 s.c o m*/ public static void generateKeyFiles() throws Exception { File publicKeyFile = new File(System.getProperty("user.dir"), "/src/main/resources" + PasswordHelper.PUBLIC_KEYFILE); File privateKeyFile = new File(System.getProperty("user.dir"), "/src/main/resources" + PasswordHelper.PRIVATE_KEYFILE); KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance(PasswordHelper.ENCRYPTION_ALGORITHM); keyGenerator.initialize(ENCRYPTION_KEY_SIZE); KeyPair keyPair = keyGenerator.generateKeyPair(); KeyFactory fact = KeyFactory.getInstance(PasswordHelper.ENCRYPTION_ALGORITHM); RSAPublicKeySpec publicKeySpec = fact.getKeySpec(keyPair.getPublic(), RSAPublicKeySpec.class); RSAPrivateKeySpec privateKeySpec = fact.getKeySpec(keyPair.getPrivate(), RSAPrivateKeySpec.class); System.out .println("Public Key : " + publicKeySpec.getModulus() + " / " + publicKeySpec.getPublicExponent()); System.out.println( "Private Key: " + privateKeySpec.getModulus() + " / " + privateKeySpec.getPrivateExponent()); writeKeyFile(publicKeyFile, publicKeySpec.getModulus(), publicKeySpec.getPublicExponent()); writeKeyFile(privateKeyFile, privateKeySpec.getModulus(), privateKeySpec.getPrivateExponent()); }
From source file:com.google.gerrit.sshd.DatabasePubKeyAuth.java
private static void addPublicKey(final Collection<PublicKey> out, final KeyPairProvider p, final String type) { final KeyPair pair = p.loadKey(type); if (pair != null && pair.getPublic() != null) { out.add(pair.getPublic());/*from w w w . j a v a2 s .co m*/ } }
From source file:com.vexsoftware.votifier.util.rsa.RSAIO.java
/** * Saves the key pair to the disk./*from www.ja v a 2s . 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 } } }