List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec
public X509EncodedKeySpec(byte[] encodedKey)
From source file:com.soomla.billing.Security.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key./* w w w. jav a 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); KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM); return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { StoreUtils.LogError(TAG, "Invalid key specification."); throw new IllegalArgumentException(e); } catch (Base64DecoderException e) { StoreUtils.LogError(TAG, "Base64 decoding failed."); throw new IllegalArgumentException(e); } }
From source file:io.gomint.server.network.packet.PacketLogin.java
private ECPublicKey getPublicKey(byte[] publicKeyBlob) { X509EncodedKeySpec ks = new X509EncodedKeySpec(publicKeyBlob); try {//from www . j a v a2 s. com return (ECPublicKey) KEY_FACTORY.generatePublic(ks); } catch (InvalidKeySpecException e) { System.out.println("Received invalid key specification from client"); this.valid = false; return null; } catch (ClassCastException e) { System.out.println("Received valid X.509 key from client but it was not EC Public Key material"); this.valid = false; return null; } }
From source file:org.psl.fidouaf.core.crypto.KeyCodec.java
/** * Added code for IOS (RSA keys) compatibility. * /* w w w. j a va 2 s . c o m*/ * @param encodedPubKey * @return * @throws IOException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException */ public static PublicKey getPublicKey(byte[] encodedPubKey) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { X509EncodedKeySpec spec = new X509EncodedKeySpec(encodedPubKey); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(spec); }
From source file:gemlite.core.util.RSAUtils.java
/** * <p>/*from w w w . j a va 2s . 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); }
From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java
public static PublicKey getPublicOscarKey(String publicOscarKeyString) throws NoSuchAlgorithmException, InvalidKeySpecException { X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicOscarKeyString)); KeyFactory pubKeyFactory = KeyFactory.getInstance("RSA"); PublicKey publicOscarKey = pubKeyFactory.generatePublic(pubKeySpec); return publicOscarKey; }
From source file:com.vmware.identity.openidconnect.sample.RelyingPartyInstaller.java
static PublicKey loadPublicKey(String file, String algorithm) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { // Read Public Key. File filePublicKey = new File(file); FileInputStream fis = new FileInputStream(file); byte[] encodedPublicKey = new byte[(int) filePublicKey.length()]; fis.read(encodedPublicKey);/* w ww .j a va 2s. c o m*/ fis.close(); // Generate Public Key. KeyFactory keyFactory = KeyFactory.getInstance(algorithm); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); return publicKey; }
From source file:ie.peternagy.jcrypto.algo.EllipticCurveWrapper.java
/** * Try load the keys from disk//from w w w. j av a 2 s .co m */ public void tryLoadKeys() { try { byte[] publicBytes = Hex .decodeHex(new String(FileAccessUtil.readFromDisk(getKeyFilePath(false))).toCharArray()); byte[] privateBytes = Hex .decodeHex(new String(FileAccessUtil.readFromDisk(getKeyFilePath(true))).toCharArray()); KeyFactory fact = KeyFactory.getInstance("ECDSA", "BC"); publicKey = fact.generatePublic(new X509EncodedKeySpec(publicBytes)); privateKey = fact.generatePrivate(new PKCS8EncodedKeySpec(privateBytes)); } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException | DecoderException ex) { Logger.getLogger(EllipticCurveWrapper.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:info.magnolia.cms.security.SecurityUtil.java
public static String encrypt(String message, String encodedKey) { try {/*from ww w . ja v a2s . c o m*/ // read private key if (StringUtils.isBlank(encodedKey)) { throw new SecurityException( "Activation key was not found. Please make sure your instance is correctly configured."); } byte[] binaryKey = hexToByteArray(encodedKey); // create RSA public key cipher Cipher pkCipher = Cipher.getInstance(ALGORITHM, "BC"); try { // create private key PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(binaryKey); KeyFactory kf = KeyFactory.getInstance(ALGORITHM, "BC"); PrivateKey pk = kf.generatePrivate(privateKeySpec); pkCipher.init(Cipher.ENCRYPT_MODE, pk); } catch (InvalidKeySpecException e) { // encrypting with public key? X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(binaryKey); KeyFactory kf = KeyFactory.getInstance(ALGORITHM, "BC"); PublicKey pk = kf.generatePublic(publicKeySpec); pkCipher.init(Cipher.ENCRYPT_MODE, pk); } // encrypt byte[] bytes = message.getBytes("UTF-8"); // split bit message in chunks int start = 0; StringBuilder chaos = new StringBuilder(); while (start < bytes.length) { byte[] tmp = new byte[Math.min(bytes.length - start, binaryKey.length / 8)]; System.arraycopy(bytes, start, tmp, 0, tmp.length); start += tmp.length; byte[] encrypted = pkCipher.doFinal(tmp); chaos.append(byteArrayToHex(encrypted)); chaos.append(";"); } chaos.setLength(chaos.length() - 1); return chaos.toString(); } catch (IOException e) { throw new SecurityException( "Failed to create authentication string. Please use Java version with cryptography support.", e); } catch (NoSuchAlgorithmException e) { throw new SecurityException( "Failed to create authentication string. Please use Java version with cryptography support.", e); } catch (NoSuchPaddingException e) { throw new SecurityException( "Failed to create authentication string. Please use Java version with cryptography support.", e); } catch (InvalidKeySpecException e) { throw new SecurityException( "Failed to create authentication string. Please use Java version with cryptography support.", e); } catch (InvalidKeyException e) { throw new SecurityException( "Failed to create authentication string. Please use Java version with cryptography support.", e); } catch (NoSuchProviderException e) { throw new SecurityException( "Failed to find encryption provider. Please use Java version with cryptography support.", e); } catch (IllegalBlockSizeException e) { throw new SecurityException( "Failed to encrypt string. Please use Java version with cryptography support.", e); } catch (BadPaddingException e) { throw new SecurityException( "Failed to encrypt string. Please use Java version with cryptography support.", e); } }
From source file:com.guillaumesoft.escapehellprison.PurchaseActivity.java
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mOuyaFacade = OuyaFacade.getInstance(); Bundle developerInfo = new Bundle(); developerInfo.putString(OuyaFacade.OUYA_DEVELOPER_ID, DEVELOPER_ID); developerInfo.putByteArray(OuyaFacade.OUYA_DEVELOPER_PUBLIC_KEY, loadApplicationKey()); mOuyaFacade = OuyaFacade.getInstance(); mOuyaFacade.init(this, developerInfo); // Uncomment this line to test against the server using "fake" credits. // This will also switch over to a separate "test" purchase history. //ouyaFacade.setTestMode(); setContentView(R.layout.sample_app); receiptListView = (ListView) findViewById(R.id.receipts); receiptListView.setFocusable(false); /*/*from w w w . j av a 2 s .com*/ * In order to avoid "application not responding" popups, Android demands that long-running operations * happen on a background thread. Listener objects provide a way for you to specify what ought to happen * at the end of the long-running operation. Examples of this pattern in Android include * android.os.AsyncTask. */ findViewById(R.id.gamer_uuid_button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { fetchGamerInfo(); } }); // Attempt to restore the product and receipt list from the savedInstanceState Bundle if (savedInstanceState != null) { if (savedInstanceState.containsKey(PRODUCTS_INSTANCE_STATE_KEY)) { Parcelable[] products = savedInstanceState.getParcelableArray(PRODUCTS_INSTANCE_STATE_KEY); mProductList = new ArrayList<Product>(products.length); for (Parcelable product : products) { mProductList.add((Product) product); } addProducts(); } if (savedInstanceState.containsKey(RECEIPTS_INSTANCE_STATE_KEY)) { Parcelable[] receipts = savedInstanceState.getParcelableArray(RECEIPTS_INSTANCE_STATE_KEY); mReceiptList = new ArrayList<Receipt>(receipts.length); for (Parcelable receipt : receipts) { mReceiptList.add((Receipt) receipt); } addReceipts(); } } // Request the product list if it could not be restored from the savedInstanceState Bundle if (mProductList == null) { requestProducts(); } // Make sure the receipt ListView starts empty if the receipt list could not be restored // from the savedInstanceState Bundle. if (mReceiptList == null) { receiptListView.setAdapter(new ReceiptAdapter(this, new Receipt[0])); } // Create a PublicKey object from the key data downloaded from the developer portal. try { X509EncodedKeySpec keySpec = new X509EncodedKeySpec(loadApplicationKey()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); mPublicKey = keyFactory.generatePublic(keySpec); } catch (Exception e) { Log.e(LOG_TAG, "Unable to create encryption key", e); } }