List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec
public X509EncodedKeySpec(byte[] encodedKey)
From source file:SecureConnection.java
public byte[] getPublicKey(byte[] otherPubKeyBytes) throws Exception { KeyFactory keyFac = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(otherPubKeyBytes); PublicKey otherPubKey = keyFac.generatePublic(x509KeySpec); DHParameterSpec dhParamSpec = ((DHPublicKey) otherPubKey).getParams(); return this.getPublicKeyStep2(dhParamSpec); }
From source file:org.kaaproject.kaa.common.endpoint.security.KeyUtil.java
/** * Gets the public key from bytes.//from w ww . j ava 2 s . c om * * @param keyBytes the key bytes * @return the public * @throws InvalidKeyException invalid key exception */ public static PublicKey getPublic(byte[] keyBytes) throws InvalidKeyException { try { X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory kf = KeyFactory.getInstance(RSA); return kf.generatePublic(spec); } catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { throw new InvalidKeyException(ex); } }
From source file:org.aon.esolutions.appconfig.client.util.RSAEncryptUtil.java
/** * Generates Public Key from BASE64 encoded string * @param key BASE64 encoded string which represents the key * @return The PublicKey//from w w w . java2 s . co m * @throws java.lang.Exception */ public static PublicKey getPublicKeyFromString(String key) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(decodeBASE64(key)); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); return publicKey; }
From source file:gemlite.core.util.RSAUtils.java
/** * <p>/* w w w .ja va2 s . c om*/ * * </p> * * @param encryptedData * ? * @param publicKey * (BASE64?) * @return * @throws Exception */ public static byte[] decryptByPublicKey(byte[] encryptedData, 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.DECRYPT_MODE, publicK); int inputLen = encryptedData.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offSet = 0; byte[] cache; int i = 0; // ? while (inputLen - offSet > 0) { if (inputLen - offSet > MAX_DECRYPT_BLOCK) { cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK); } else { cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet); } out.write(cache, 0, cache.length); i++; offSet = i * MAX_DECRYPT_BLOCK; } byte[] decryptedData = out.toByteArray(); out.close(); return decryptedData; }
From source file:org.openengsb.core.services.internal.security.FileKeySource.java
protected PublicKey readPublicKeyFromFile(File keyDirectory) { byte[] keyData; try {/*w w w. ja v a 2s .co m*/ File file = new File(keyDirectory, DEFAULT_PUBLIC_KEY_FILENAME); LOGGER.trace("reading private key form file: {}", file.getAbsolutePath()); keyData = FileUtils.readFileToByteArray(file); } catch (IOException e) { throw new IllegalStateException(e); } KeySpec keySpec = new X509EncodedKeySpec(keyData); try { return getKeyFactory().generatePublic(keySpec); } catch (InvalidKeySpecException e) { throw new IllegalStateException(e); } }
From source file:net.firejack.platform.web.security.x509.KeyUtils.java
public static void verify(String cert64, String public64) throws CertificateException, NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, NoSuchProviderException, InvalidKeyException { if (StringUtils.isBlank(cert64)) { throw new CertificateException("Certificate is empty"); } else if (StringUtils.isBlank(public64)) { throw new InvalidKeyException("Public key is empty"); }/*from w w w.j a v a2 s .c o m*/ byte[] cert = Base64.decode(cert64); byte[] key = Base64.decode(public64); X509CertImpl x509 = new X509CertImpl(cert); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(key); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC")); x509.verify(publicKey); x509.checkValidity(utc.getTime()); }
From source file:org.apache.james.jdkim.tagvalue.PublicKeyRecordImpl.java
/** * @see org.apache.james.jdkim.api.PublicKeyRecord#getPublicKey() *//* w w w .j av a 2 s . c o m*/ public PublicKey getPublicKey() { try { String p = getValue("p").toString(); byte[] key = Base64.decodeBase64(p.getBytes()); KeyFactory keyFactory; keyFactory = KeyFactory.getInstance(getValue("k").toString()); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(key); RSAPublicKey rsaKey; rsaKey = (RSAPublicKey) keyFactory.generatePublic(pubSpec); return rsaKey; } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("Unknown algorithm: " + e.getMessage()); } catch (InvalidKeySpecException e) { throw new IllegalStateException("Invalid key spec: " + e.getMessage()); } }
From source file:com.badlogic.gdx.pay.android.ouya.PurchaseManagerAndroidOUYA.java
@Override public void install(final PurchaseObserver observer, PurchaseManagerConfig config) { this.observer = observer; this.config = config; // Obtain applicationKey and developer ID. Pass in as follows: // ------------------------------------------------------------------------- // config.addStoreParam( // PurchaseManagerConfig.STORE_NAME_ANDROID_OUYA, // new Object[] { OUYA_DEVELOPERID_STRING, applicationKeyPathSTRING }); // ------------------------------------------------------------------------- Object[] configuration = (Object[]) config.getStoreParam(PurchaseManagerConfig.STORE_NAME_ANDROID_OUYA); String developerID = (String) configuration[0]; applicationKeyPath = (String) configuration[1]; // store our OUYA applicationKey-Path! ouyaFacade = OuyaFacade.getInstance(); ouyaFacade.init((Context) activity, developerID); // --- copy all available products to the list of purchasables productIDList = new ArrayList<Purchasable>(config.getOfferCount()); for (int i = 0; i < config.getOfferCount(); i++) { productIDList.add(new Purchasable(config.getOffer(i).getIdentifier())); }/*from w ww . ja va 2 s .c om*/ // Create a PublicKey object from the key data downloaded from the developer portal. try { // Read in the key.der file (downloaded from the developer portal) FileHandle fHandle = Gdx.files.internal(applicationKeyPath); byte[] applicationKey = fHandle.readBytes(); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(applicationKey); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); ouyaPublicKey = keyFactory.generatePublic(keySpec); showMessage(LOGTYPELOG, "succesfully created publicKey"); // ---- request the productlist --------- requestProductList(); // notify of successful initialization observer.handleInstall(); } catch (Exception e) { // notify about the problem showMessage(LOGTYPEERROR, "Problem setting up in-app billing: Unable to create encryption key"); observer.handleInstallError(new RuntimeException( "Problem setting up in-app billing: Unable to create encryption key: " + e)); } }
From source file:mitm.common.security.keystore.hibernate.SerializableKeyEntry.java
/** * Creates the key from the given byte array using the stored format and type. * /*from w ww. ja va2s. com*/ * @param keyBytes * @return the key * @throws KeyStoreException * @throws InvalidKeySpecException * @throws NoSuchAlgorithmException * @throws NoSuchProviderException */ private Key getKey(byte[] rawKey) throws KeyStoreException, InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { KeySpec keySpec; Format keyFormat = toFormat(format); switch (keyFormat) { case PKCS8: keySpec = new PKCS8EncodedKeySpec(rawKey); break; case X509: keySpec = new X509EncodedKeySpec(rawKey); break; case RAW: return new SecretKeySpec(rawKey, algorithm); default: throw new KeyStoreException("Unknown key format " + keyFormat); } SecurityFactory securityFactory = getSecurityFactory(); switch (keyType) { case PRIVATE: return securityFactory.createKeyFactory(algorithm).generatePrivate(keySpec); case PUBLIC: return securityFactory.createKeyFactory(algorithm).generatePublic(keySpec); case SECRET: return securityFactory.createSecretKeyFactory(algorithm).generateSecret(keySpec); default: throw new KeyStoreException("Unknown key type " + keyType); } }
From source file:eu.dety.burp.joseph.utilities.Converter.java
/** * Build {@link RSAPublicKey} from PublicKey PEM string * //w w w .j av a 2s . co m * @param pemInput * PublicKey PEM string * @return {@link RSAPublicKey} or null */ public static RSAPublicKey getRsaPublicKeyByPemString(String pemInput) { RSAPublicKey publicKey = null; String pubKey = pemInput.replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", ""); // PKCS8 try { byte[] keyBytes = Base64.decodeBase64(pubKey); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); publicKey = (RSAPublicKey) keyFactory.generatePublic(spec); } catch (Exception e) { } // PKCS1 try { byte[] keyBytes = Base64.decodeBase64(pubKey); keyBytes = Arrays.copyOfRange(keyBytes, 24, keyBytes.length); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); publicKey = (RSAPublicKey) keyFactory.generatePublic(spec); } catch (Exception e) { } return publicKey; }