List of usage examples for java.security KeyFactory generatePublic
public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException
From source file:Main.java
public static byte[] encryptMsg(String msg, RSAPublicKeySpec pubKeySpec) { if (msg != null && pubKeySpec != null && !msg.isEmpty()) { try {/*ww w.j av a 2s.c o m*/ Log.w(TAG, "msg is: " + msg + " with length " + msg.length()); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey pubKey = fact.generatePublic(pubKeySpec); // TODO encrypt the message and send it // Cipher cipher = Cipher.getInstance("RSA/None/NoPadding"); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // Cipher cipher = // Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", // "BC"); cipher.init(Cipher.ENCRYPT_MODE, pubKey); Log.d(TAG, "cipher block size is " + cipher.getBlockSize()); byte[] msgByteArray = msg.getBytes(); byte[] cipherData = new byte[cipher.getOutputSize(msgByteArray.length)]; cipherData = cipher.doFinal(msgByteArray); Log.d(TAG, "output size is " + cipher.getOutputSize(msgByteArray.length)); Log.d(TAG, "is the measurement already broken into chunks here? " + (new String(cipherData))); return cipherData; } catch (NoSuchAlgorithmException e) { Log.e(TAG, "RSA algorithm not available", e); } catch (InvalidKeySpecException e) { Log.e(TAG, "", e); } catch (NoSuchPaddingException e) { Log.e(TAG, "", e); } catch (InvalidKeyException e) { Log.e(TAG, "", e); } catch (BadPaddingException e) { Log.e(TAG, "", e); } catch (IllegalBlockSizeException e) { Log.e(TAG, "", e); } catch (Exception e) { Log.e(TAG, "", e); } /* * catch (NoSuchProviderException e) { Log.e(TAG, "", e); } */ } return null; }
From source file:license.rsa.WakeRSA.java
/** * ?//from ww w . jav a 2 s.c o m * * @param keyFileName * @return * @throws Exception */ static PublicKey readPublicKeyFromFile() throws Exception { ObjectInputStream oin = new ObjectInputStream(new ByteArrayInputStream(KeyData.publicKey)); try { BigInteger m = (BigInteger) oin.readObject(); BigInteger e = (BigInteger) oin.readObject(); System.out.println(); System.out.println(); System.out.println("=======mmm=====" + m); System.out.println("=======eeee=====" + e); System.out.println(); System.out.println(); RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e); KeyFactory fact = KeyFactory.getInstance("RSA"); return fact.generatePublic(keySpec); } finally { oin.close(); } }
From source file:com.spotify.sshagentproxy.RSA.java
/** * Create an {@link RSAPublicKey} from bytes. * @param key Array of bytes representing RSA public key. * @return {@link RSAPublicKey}// w w w . j a v a2s . c o m * @throws InvalidKeyException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ static RSAPublicKey from(final byte[] key) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException { final String s = new String(key); final byte[] encoded; final String decoded; if (s.startsWith(RSA_LABEL)) { decoded = s.split(" ")[1]; encoded = Base64.decodeBase64(decoded); } else { encoded = key; decoded = Base64.encodeBase64String(key); } final Iterator<byte[]> fields = new ByteIterator(encoded); final String sigType = new String(fields.next()); if (!sigType.equals(RSA_LABEL)) { throw new RuntimeException(String.format("Unknown key type %s. This code currently only supports %s.", sigType, RSA_LABEL)); } final RSAPublicKeySpec keySpec = TraditionalKeyParser.parsePemPublicKey(RSA_LABEL + " " + decoded + " "); final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return (RSAPublicKey) keyFactory.generatePublic(keySpec); }
From source file:com.alliander.osgp.shared.security.CertificateHelper.java
/** * Create public key from public key file on disk * * @param keyPath// w w w.j a v a2 s. c o m * path to key * @param keyType * type of key * @return instance of public key * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws IOException * @throws NoSuchProviderException */ public static PublicKey createPublicKey(final String keyPath, final String keyType, final String provider) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchProviderException { final byte[] key = readKeyFromDisk(keyPath); final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(key); final KeyFactory publicKeyFactory = KeyFactory.getInstance(keyType, provider); return publicKeyFactory.generatePublic(publicKeySpec); }
From source file:com.alliander.osgp.shared.security.CertificateHelper.java
public static PublicKey createPublicKeyFromBase64(final String keyBase64, final String keyType, final String provider) throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchProviderException { final byte[] key = Base64.decodeBase64(keyBase64); final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(key); final KeyFactory publicKeyFactory = KeyFactory.getInstance(keyType, provider); return publicKeyFactory.generatePublic(publicKeySpec); }
From source file:org.apache.abdera.security.util.KeyHelper.java
public static PublicKey generatePublicKey(String hex) { try {/*from w w w. j a v a 2 s . co m*/ if (hex == null || hex.trim().length() == 0) return null; byte[] data = Hex.decodeHex(hex.toCharArray()); X509EncodedKeySpec keyspec = new X509EncodedKeySpec(data); KeyFactory keyfactory = KeyFactory.getInstance("RSA"); return keyfactory.generatePublic(keyspec); } catch (Exception e) { return null; } }
From source file:cl.nic.dte.extension.AutorizacionTypeExtensionHandler.java
@SuppressWarnings("unchecked") public static PublicKey getPublicKey(AutorizacionType auth) throws InvalidKeySpecException, NoSuchAlgorithmException { List<PEMItem> items = PEMUtil.decode(auth.getRSAPUBK().getBytes()); for (PEMItem item : items) { if ("PUBLIC KEY".equals(item.pemType)) { X509EncodedKeySpec enc; try { enc = new X509EncodedKeySpec(item.getDerBytes()); KeyFactory rsaKeyFac; rsaKeyFac = KeyFactory.getInstance("RSA"); return (PublicKey) rsaKeyFac.generatePublic((enc)); } catch (GeneralSecurityException e) { throw new InvalidKeySpecException(e); }//from ww w .j a v a 2 s .com } } return null; }
From source file:com.dev.cty.utils.googleplay.Security.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key./*from ww w . j av a2 s. c o m*/ * * @param encodedPublicKey Base64-encoded public key * @throws IllegalArgumentException if encodedPublicKey is invalid */ public static PublicKey generatePublicKey(String encodedPublicKey) { try { byte[] decodedKey = Base64.decode(encodedPublicKey); KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM); return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { logger.info("Invalid key specification."); throw new IllegalArgumentException(e); } catch (Base64DecoderException e) { logger.info("Base64 decoding failed."); throw new IllegalArgumentException(e); } }
From source file:com.zf.decipher.DataEn.java
private static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey); PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey); PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec); KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm()); keyAgree.init(priKey);/*from w w w . ja va2 s . co m*/ keyAgree.doPhase(pubKey, true); return keyAgree.generateSecret(AES).getEncoded(); }
From source file:acceptable_risk.nik.uniobuda.hu.andrawid.util.Security.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key.//ww w . ja va 2s . c o m * * @param encodedPublicKey Base64-encoded public key * @throws IllegalArgumentException if encodedPublicKey is invalid */ public static PublicKey generatePublicKey(String encodedPublicKey) { try { byte[] decodedKey = Base64.decode(encodedPublicKey); KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM); return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { Log.e(TAG, "Invalid key specification."); throw new IllegalArgumentException(e); } catch (Base64DecoderException e) { Log.e(TAG, "Base64 decoding failed."); throw new IllegalArgumentException(e); } }