List of usage examples for java.security PublicKey getEncoded
public byte[] getEncoded();
From source file:Main.java
public static byte[] getEncodedPublic(PublicKey pk) { return new X509EncodedKeySpec(pk.getEncoded()).getEncoded(); }
From source file:de.alpharogroup.crypto.key.PublicKeyExtensions.java
/** * Transform the given {@link PublicKey} to a base64 encoded {@link String} value. * * @param publicKey//w ww. j a v a 2 s . c o m * the public key * @return the base64 encoded {@link String} value. */ public static String toBase64(final PublicKey publicKey) { final byte[] encoded = publicKey.getEncoded(); final String publicKeyAsBase64String = Base64.encodeBase64String(encoded); return publicKeyAsBase64String; }
From source file:com.vimukti.accounter.developer.api.PublicKeyGenerator.java
private static void generate() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, KeyStoreException, CertificateException, IOException, URISyntaxException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed("VimTech".getBytes("UTF-8")); keyGen.initialize(1024, random);//from w ww.j ava 2 s.c om KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); System.out.println(priv); System.out.println(pub); byte[] encoded = pub.getEncoded(); byte[] encodeBase64 = Base64.encodeBase64(encoded); System.out.println("Public Key:" + new String(encodeBase64)); byte[] encodedPrv = priv.getEncoded(); byte[] encodeBase64Prv = Base64.encodeBase64(encodedPrv); System.out.println("Private Key:" + new String(encodeBase64Prv)); byte[] decodeBase64 = Base64.decodeBase64(encodeBase64); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decodeBase64); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); System.out.println(keyFactory.generatePublic(pubKeySpec).equals(pub)); }
From source file:net.nicholaswilliams.java.licensing.encryption.KeyFileUtilities.java
protected static byte[] writeEncryptedPublicKey(PublicKey publicKey, char[] passphrase) { X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); return Encryptor.encryptRaw(x509EncodedKeySpec.getEncoded(), passphrase); }
From source file:de.alpharogroup.crypto.key.PublicKeyExtensions.java
/** * Transform the given {@link PublicKey} to a hexadecimal {@link String} value. * * @param publicKey// w ww . j ava2 s. c om * the public key * @param lowerCase * the flag if the result shell be transform in lower case. If true the result is * @return the new hexadecimal {@link String} value. */ public static String toHexString(final PublicKey publicKey, final boolean lowerCase) { final String hexString = HexExtensions.toHexString(publicKey.getEncoded(), lowerCase); return hexString; }
From source file:Main.java
public static String executeHttpsPost(String url, String data, InputStream key) { HttpsURLConnection localHttpsURLConnection = null; try {/*www . j av a 2 s . c o m*/ URL localURL = new URL(url); localHttpsURLConnection = (HttpsURLConnection) localURL.openConnection(); localHttpsURLConnection.setRequestMethod("POST"); localHttpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); localHttpsURLConnection.setRequestProperty("Content-Length", "" + Integer.toString(data.getBytes().length)); localHttpsURLConnection.setRequestProperty("Content-Language", "en-US"); localHttpsURLConnection.setUseCaches(false); localHttpsURLConnection.setDoInput(true); localHttpsURLConnection.setDoOutput(true); localHttpsURLConnection.connect(); Certificate[] arrayOfCertificate = localHttpsURLConnection.getServerCertificates(); byte[] arrayOfByte1 = new byte[294]; DataInputStream localDataInputStream = new DataInputStream(key); localDataInputStream.readFully(arrayOfByte1); localDataInputStream.close(); Certificate localCertificate = arrayOfCertificate[0]; PublicKey localPublicKey = localCertificate.getPublicKey(); byte[] arrayOfByte2 = localPublicKey.getEncoded(); for (int i = 0; i < arrayOfByte2.length; i++) { if (arrayOfByte2[i] != arrayOfByte1[i]) throw new RuntimeException("Public key mismatch"); } DataOutputStream localDataOutputStream = new DataOutputStream( localHttpsURLConnection.getOutputStream()); localDataOutputStream.writeBytes(data); localDataOutputStream.flush(); localDataOutputStream.close(); InputStream localInputStream = localHttpsURLConnection.getInputStream(); BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream)); StringBuffer localStringBuffer = new StringBuffer(); String str1; while ((str1 = localBufferedReader.readLine()) != null) { localStringBuffer.append(str1); localStringBuffer.append('\r'); } localBufferedReader.close(); return localStringBuffer.toString(); } catch (Exception localException) { byte[] arrayOfByte1; localException.printStackTrace(); return null; } finally { if (localHttpsURLConnection != null) localHttpsURLConnection.disconnect(); } }
From source file:com.aqnote.shared.cryptology.asymmetric.dsa.DSAKeyPairGenTest.java
public static void generator() throws NoSuchAlgorithmException, FileNotFoundException, IOException { KeyPairGenerator keygen = KeyPairGenerator.getInstance(ALGORITHM); // ???/* ww w . ja v a2 s. c om*/ SecureRandom secrand = new SecureRandom(); secrand.setSeed(seed); // ?? // ??, ??keysize ?. 512 1024 64 ? keygen.initialize(512, secrand); // ?pubkey?prikey KeyPair keys = keygen.generateKeyPair(); // ? PublicKey pubkey = keys.getPublic(); PrivateKey prikey = keys.getPrivate(); byte[] pubkeyByteArray = Base64.encodeBase64(pubkey.getEncoded()); OutputStream os = new FileOutputStream(new File(PUBKEY_FILE_NAME)); ByteArrayInputStream bais = new ByteArrayInputStream(pubkeyByteArray); StreamUtil.io(bais, os); bais.close(); os.close(); byte[] prikeyByteArray = Base64.encodeBase64(prikey.getEncoded()); os = new FileOutputStream(new File(PRIKEY_FILE_NAME)); bais = new ByteArrayInputStream(prikeyByteArray); StreamUtil.io(bais, os); bais.close(); os.close(); }
From source file:com.vexsoftware.votifier.crypto.RSAIO.java
/** * Saves the key pair to the disk.// w w w. j av a 2 s. c om * * @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:edu.stanford.mobisocial.dungbeetle.DBIdentityProvider.java
public static String publicKeyToString(PublicKey pubkey) { return FastBase64.encodeToString(pubkey.getEncoded()); }
From source file:com.boubei.tss.modules.license.LicenseFactory.java
/** * ???// www .j a va2 s. c o m * ????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); }