List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec
public X509EncodedKeySpec(byte[] encodedKey)
From source file:org.cprados.wificellmanager.billing.Security.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key.//from ww w. j a va 2 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); byte[] decodedKey = Base64.decode(encodedPublicKey, Base64.DEFAULT); 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); //} }
From source file:com.vmware.identity.openidconnect.sample.RelyingPartyInstaller.java
private void savePublicKey(String file, PublicKey publicKey) throws IOException { // Store Public Key. X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); FileOutputStream fos = new FileOutputStream(file); fos.write(x509EncodedKeySpec.getEncoded()); fos.close();//from w ww . ja v a2 s . c om }
From source file:com.clustercontrol.util.KeyCheck.java
/** * ?/*from w ww .j ava 2 s .c om*/ * com.clustercontrol.key.KeyGenerator????????public?? * @param str * @return * @throws HinemosUnknown */ public static PublicKey getPublicKey(String str) throws HinemosUnknown { try { X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(string2Byte(str)); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); return keyFactory.generatePublic(publicKeySpec); } catch (InvalidKeySpecException e) { throw new HinemosUnknown("getPublicKey fail " + e.getMessage(), e); } catch (NoSuchAlgorithmException e) { throw new HinemosUnknown("getPublicKey fail " + e.getMessage(), 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 w ww . ja v a 2s .c o 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:oscar.oscarLab.ca.all.pageUtil.LabUploadAction.java
public static ArrayList<Object> getClientInfo(String service) { PublicKey Key = null;// w w w . j av a 2s. c om Base64 base64 = new Base64(0); String keyString = ""; String type = ""; byte[] publicKey; ArrayList<Object> info = new ArrayList<Object>(); try { PublicKeyDao publicKeyDao = (PublicKeyDao) SpringUtils.getBean("publicKeyDao"); org.oscarehr.common.model.PublicKey publicKeyObject = publicKeyDao.find(service); if (publicKeyObject != null) { keyString = publicKeyObject.getBase64EncodedPublicKey(); type = publicKeyObject.getType(); } publicKey = base64.decode(keyString.getBytes(MiscUtils.ENCODING)); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(publicKey); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key = keyFactory.generatePublic(pubKeySpec); info.add(Key); info.add(type); } catch (Exception e) { logger.error("Could not retrieve private key: ", e); } return (info); }
From source file:org.ejbca.core.protocol.cmp.CrmfRequestMessage.java
private PublicKey getPublicKey(final SubjectPublicKeyInfo subjectPKInfo, final String provider) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException { try {/*from w w w. j av a 2s.c om*/ final X509EncodedKeySpec xspec = new X509EncodedKeySpec(new DERBitString(subjectPKInfo).getBytes()); final AlgorithmIdentifier keyAlg = subjectPKInfo.getAlgorithm(); return KeyFactory.getInstance(keyAlg.getAlgorithm().getId(), provider).generatePublic(xspec); } catch (java.security.spec.InvalidKeySpecException e) { final InvalidKeyException newe = new InvalidKeyException("Error decoding public key."); newe.initCause(e); throw newe; } catch (IOException e) { final InvalidKeyException newe = new InvalidKeyException("Error decoding public key."); newe.initCause(e); throw newe; } }
From source file:net.jmhertlein.core.crypto.Keys.java
/** * Given a Base64-encoded, X509-formatted RSA public key, returns a PublicKey object representing it * * @param encodedKey/*from ww w . j a v a2 s .c o m*/ * * @return the RSA public key, or null if the RSA algorithm is not available on the system */ public static PublicKey getPublicKeyFromBASE64X509Encoded(String encodedKey) { byte[] decoded = Base64.decodeBase64(encodedKey); try { return KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded)); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { Logger.getLogger(Keys.class.getName()).log(Level.SEVERE, null, ex); return null; } }
From source file:jfabrix101.billing.BillingSecurity.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key.//from ww w. jav a 2 s . co 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 (Exception e) { Log.e(TAG, "Base64 decoding failed."); throw new IllegalArgumentException(e); } }
From source file:in.neoandroid.neoupdate.neoUpdate.java
private boolean checkSignature(String jsonContent, String sign) { Log.d(TAG, "JSON: " + jsonContent); if (sign == null) return false; final String publicKeyStr = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAq+6EG/fAE+zIdh5Wzqnf" + "Fo4nCf7t7eJcKyvk1lqX1MdkIi/fUs8HQ4aQ4jWLCO4M1Gkz1FQiXOnheGLV5MXY" + "c9GyaglsofvpA/pU5d16FybX2pCevbTzcm39eU+XlwQWOr8gh23tYD8G6uMX6sIJ" + "W+1k1FWdud9errMVm0YUScI+J4AV5xzN0IQ29h9IeNp6oFqZ2ByWog6OBMTUDFIW" + "q8oRvH0OuPv3zFR5rKwsbTYb5Da8lhUht04dLBA860Y4zeUu98huvS9jQPu2N4ns" + "Hf425FfDJ/wae+7eLdQo7uFb+Wvc+PO9U39e6vXQfa8ZkUoXHD0XZN4jsFcKYuJw" + "OwIDAQAB"; try {//from w w w . j ava 2 s .c o m byte keyBytes[] = Base64.decode(publicKeyStr.getBytes(), Base64.NO_WRAP); X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance("RSA"); PublicKey publicKey = kf.generatePublic(publicSpec); Signature signer = Signature.getInstance("SHA1withRSA"); signer.initVerify(publicKey); signer.update(jsonContent.getBytes(), 0, jsonContent.length()); return signer.verify(Base64.decode(sign, Base64.NO_WRAP)); } catch (Exception e) { } return false; }