List of usage examples for java.security KeyFactory getInstance
public static KeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:org.nick.ghettounlock.GhettoTrustAgent.java
public static RSAPublicKey getPublicKey(Context ctx) { String pubKeyStr = PreferenceManager.getDefaultSharedPreferences(ctx).getString(PREF_PUB_KEY, null); if (pubKeyStr == null) { return null; }/*from w ww .ja va2s. com*/ try { X509EncodedKeySpec keySpec = new X509EncodedKeySpec( Base64.decode(pubKeyStr.getBytes("UTF-8"), Base64.DEFAULT)); KeyFactory kf = KeyFactory.getInstance("RSA"); RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(keySpec); return pubKey; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.cloudfoundry.identity.uaa.oauth.SignerProvider.java
static KeyPair parseKeyPair(String pemData) { Matcher m = PEM_DATA.matcher(pemData.trim()); if (!m.matches()) { throw new IllegalArgumentException("String is not PEM encoded data"); }/*from ww w.j av a2 s. co m*/ String type = m.group(1); final byte[] content = b64Decode(utf8Encode(m.group(2))); PublicKey publicKey; PrivateKey privateKey = null; try { KeyFactory fact = KeyFactory.getInstance("RSA"); if (type.equals("RSA PRIVATE KEY")) { ASN1Sequence seq = ASN1Sequence.getInstance(content); if (seq.size() != 9) { throw new IllegalArgumentException("Invalid RSA Private Key ASN1 sequence."); } org.bouncycastle.asn1.pkcs.RSAPrivateKey key = org.bouncycastle.asn1.pkcs.RSAPrivateKey .getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); RSAPrivateCrtKeySpec privSpec = new RSAPrivateCrtKeySpec(key.getModulus(), key.getPublicExponent(), key.getPrivateExponent(), key.getPrime1(), key.getPrime2(), key.getExponent1(), key.getExponent2(), key.getCoefficient()); publicKey = fact.generatePublic(pubSpec); privateKey = fact.generatePrivate(privSpec); } else if (type.equals("PUBLIC KEY")) { KeySpec keySpec = new X509EncodedKeySpec(content); publicKey = fact.generatePublic(keySpec); } else if (type.equals("RSA PUBLIC KEY")) { ASN1Sequence seq = ASN1Sequence.getInstance(content); org.bouncycastle.asn1.pkcs.RSAPublicKey key = org.bouncycastle.asn1.pkcs.RSAPublicKey .getInstance(seq); RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent()); publicKey = fact.generatePublic(pubSpec); } else { throw new IllegalArgumentException(type + " is not a supported format"); } return new KeyPair(publicKey, privateKey); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e); } }
From source file:com.ibm.dbwkl.helper.CryptionModule.java
/** * @return PrivateKey Instance//from ww w.j a v a2 s . co m * @throws IOException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ private RSAPrivateKey getPrivateKey() throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { byte[] privateKeyBytes = null; RSAPrivateKey privateKey = null; privateKeyBytes = getPrivateKeyBytes(); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); KeySpec keySpec = new PKCS8EncodedKeySpec(privateKeyBytes); privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec); return privateKey; }
From source file:net.adamcin.httpsig.testutil.KeyTestUtil.java
public static KeyPair getKeyPairFromProperties(String parentName, String keyName) { InputStream is = null;// w w w.j a v a 2 s .c o m try { is = KeyTestUtil.class.getResourceAsStream("/" + parentName + "/" + keyName + ".properties"); Properties props = new Properties(); props.load(is); if (TYPE_RSA.equals(props.getProperty(P_TYPE))) { RSAPrivateKeySpec privSpec = null; if (props.getProperty(RSA_P) != null && props.getProperty(RSA_Q) != null && props.getProperty(RSA_U) != null) { privSpec = new RSAPrivateCrtKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E)), new BigInteger(props.getProperty(RSA_D)), new BigInteger(props.getProperty(RSA_P)), new BigInteger(props.getProperty(RSA_Q)), new BigInteger(props.getProperty(RSA_PE)), new BigInteger(props.getProperty(RSA_QE)), new BigInteger(props.getProperty(RSA_U))); } else { privSpec = new RSAPrivateKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_D))); } RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(new BigInteger(props.getProperty(RSA_N)), new BigInteger(props.getProperty(RSA_E))); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } else if (TYPE_DSA.equals(props.getProperty(P_TYPE))) { DSAPrivateKeySpec privSpec = new DSAPrivateKeySpec(new BigInteger(props.getProperty(DSA_X)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); DSAPublicKeySpec pubSpec = new DSAPublicKeySpec(new BigInteger(props.getProperty(DSA_Y)), new BigInteger(props.getProperty(DSA_P)), new BigInteger(props.getProperty(DSA_Q)), new BigInteger(props.getProperty(DSA_G))); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); return new KeyPair(keyFactory.generatePublic(pubSpec), keyFactory.generatePrivate(privSpec)); } } catch (Exception e) { LOGGER.error("Failed to read properties", e); } finally { IOUtils.closeQuietly(is); } return null; }
From source file:com.torresbueno.RSAEncryptionDecryptionUtil.java
/** * Read public key from file./*from ww w . j ava 2 s . c o m*/ * @param filePath * @return * @throws IOException */ public PrivateKey readPrivateKeyFromFile(String filePath) throws Exception { // Read file to a byte array. String privateKeyFileName = filePath; Path path = Paths.get(privateKeyFileName); byte[] privKeyByteArray = Files.readAllBytes(path); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privKeyByteArray); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey myPrivKey = keyFactory.generatePrivate(keySpec); return myPrivKey; }
From source file:com.cablevision.util.sso.UtilSSO.java
/** * Creates a PrivateKey from the specified public key file and algorithm. * Returns null if failure to generate PrivateKey. * /*from w ww . ja v a 2s . c om*/ * @param PrivateKeyFilepath location of public key file * @param algorithm algorithm of specified key file * @return PrivateKey object representing contents of specified private key * file, null if error in generating key or invalid file specified */ public static PrivateKey getPrivateKey(String privateKeyFilepath, String algorithm) throws SamlException { try { InputStream privKey = null; ClassLoader cl = Thread.currentThread().getContextClassLoader(); privKey = cl.getResourceAsStream(privateKeyFilepath); byte[] bytes = IOUtils.toByteArray(privKey); privKey.close(); System.out.println("Private bytes: " + Arrays.toString(bytes)); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(bytes); KeyFactory factory = KeyFactory.getInstance(algorithm); return factory.generatePrivate(privSpec); } catch (FileNotFoundException e) { throw new SamlException("ERROR: Private key file not found - " + privateKeyFilepath); } catch (IOException e) { throw new SamlException("ERROR: Invalid private key file - " + e.getMessage()); } catch (NoSuchAlgorithmException e) { throw new SamlException("ERROR: Invalid private key algorithm - " + e.getMessage()); } catch (InvalidKeySpecException e) { throw new SamlException("ERROR: Invalid private key spec - " + e.getMessage()); } }
From source file:com.clustercontrol.util.KeyCheck.java
/** * ?//from w ww . j a va 2 s. co m * com.clustercontrol.key.KeyGenerator????????public?? * @param str * @return * @throws HinemosUnknown */ public static PrivateKey getPrivateKey(String str) throws HinemosUnknown { try { KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(string2Byte(str)); return keyFactory.generatePrivate(privateKeySpec); } catch (InvalidKeySpecException e) { throw new HinemosUnknown("getPrivateKey fail " + e.getMessage(), e); } catch (NoSuchAlgorithmException e) { throw new HinemosUnknown("getPrivateKey fail " + e.getMessage(), e); } }
From source file:de.tum.in.tumcampus.services.GcmIntentService.java
/** * Loads the private key from preferences * * @return The private key object//from ww w .ja v a2 s . com */ private PrivateKey getPrivateKeyFromSharedPrefs() { String privateKeyString = Utils.getInternalSettingString(this, Const.PRIVATE_KEY, ""); byte[] privateKeyBytes = Base64.decode(privateKeyString, Base64.DEFAULT); KeyFactory keyFactory; try { keyFactory = KeyFactory.getInstance("RSA"); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes); return keyFactory.generatePrivate(privateKeySpec); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { Utils.log(e); } return null; }
From source file:gemlite.core.util.RSAUtils.java
/** * <p>/* w w w .j a v a 2 s. c om*/ * * </p> * * @param data * ?? * @param publicKey * (BASE64?) * @return * @throws Exception */ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception { byte[] keyBytes = Base64Utils.decode(publicKey); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); Key publicK = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicK); int inputLen = data.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // ? while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_ENCRYPT_BLOCK) { cache = cipher.doFinal(data, offSet, MAX_ENCRYPT_BLOCK); } else { cache = cipher.doFinal(data, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = out.toByteArray(); out.close(); return encryptedData; }
From source file:net.sourceforge.msscodefactory.cflib.v2_1.CFLib.Tip.CFTipEnvelopeHandler.java
public void setEncodedClientPublicKey(byte encoded[]) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidKeySpecException { X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(encoded); KeyFactory kf = KeyFactory.getInstance("RSA"); clientPublicKey = kf.generatePublic(x509KeySpec); }