List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec
public X509EncodedKeySpec(byte[] encodedKey)
From source file:com.sammyun.util.RSAUtils.java
/** * RSA??//from w w w .j ava 2s .com * * @param content ??? * @param sign ?? * @param ali_public_key ? * @param input_charset ?? * @return */ public static boolean verify(String content, String sign, String ali_public_key, String input_charset) { try { KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] encodedKey = Base64Util.decode(ali_public_key); PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey)); java.security.Signature signature = java.security.Signature.getInstance(SIGN_ALGORITHMS); signature.initVerify(pubKey); signature.update(content.getBytes(input_charset)); boolean bverify = signature.verify(Base64Util.decode(sign)); return bverify; } catch (Exception e) { e.printStackTrace(); } return false; }
From source file:org.picketlink.pki.internal.DefaultEncryptionAuthority.java
@Override public File encryptFile(String inputFilePath, String publicKeyPath, String transformation, String encoding) { // TODO : Specify a valid encryptedFilePath or receive it as an argument File encryptedFile = null;//ww w .ja va2 s .co m try { String inputRawText = readFileAsString(inputFilePath); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( IOUtils.toByteArray(new FileInputStream(publicKeyPath))); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.ENCRYPT_MODE, KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)); String encryptedText = new String(Base64.encodeBase64(cipher.doFinal(inputRawText.getBytes(encoding)))); encryptedFile = new File("encryptedFilePath"); BufferedWriter out = new BufferedWriter(new FileWriter(encryptedFile)); out.write(encryptedText); out.close(); } catch (Exception e) { throw new RuntimeException("Could not encrypt inputFile.", e); } return encryptedFile; }
From source file:org.picketlink.pki.internal.DefaultDecryptionAuthority.java
@Override public File decryptFile(String cipherFilePath, String privateKeyPath, String transformation, String encoding) { // TODO : Specify a valid decryptedFilePath or receive it as an argument File decryptedFile = null;/*from w ww .ja va 2 s . c o m*/ try { String cipherText = readFileAsString(cipherFilePath); X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( IOUtils.toByteArray(new FileInputStream(privateKeyPath))); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.DECRYPT_MODE, KeyFactory.getInstance("RSA").generatePrivate(x509EncodedKeySpec)); String decryptedText = new String(Base64.decodeBase64(cipher.doFinal(cipherText.getBytes(encoding)))); decryptedFile = new File("decryptedFilePath"); BufferedWriter out = new BufferedWriter(new FileWriter(decryptedFile)); out.write(decryptedText); out.close(); } catch (Exception e) { throw new RuntimeException("Could not decrypt cipherFile.", e); } return decryptedFile; }
From source file:info.magnolia.cms.security.SecurityUtil.java
public static String decrypt(String message, String encodedKey) throws SecurityException { try {/*from w w w .j ava 2s . c o m*/ if (StringUtils.isBlank(encodedKey)) { throw new SecurityException( "Activation key was not found. Please make sure your instance is correctly configured."); } // decode key byte[] binaryKey = hexToByteArray(encodedKey); // create RSA public key cipher Cipher pkCipher = Cipher.getInstance(ALGORITHM, "BC"); try { // create private key X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(binaryKey); KeyFactory kf = KeyFactory.getInstance(ALGORITHM, "BC"); PublicKey pk = kf.generatePublic(publicKeySpec); pkCipher.init(Cipher.DECRYPT_MODE, pk); } catch (InvalidKeySpecException e) { // decrypting with private key? PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(binaryKey); KeyFactory kf = KeyFactory.getInstance(ALGORITHM, "BC"); PrivateKey pk = kf.generatePrivate(privateKeySpec); pkCipher.init(Cipher.DECRYPT_MODE, pk); } // decrypt String[] chunks = StringUtils.split(message, ";"); if (chunks == null) { throw new SecurityException( "The encrypted information is corrupted or incomplete. Please make sure someone is not trying to intercept or modify encrypted message."); } StringBuilder clearText = new StringBuilder(); for (String chunk : chunks) { byte[] byteChunk = hexToByteArray(chunk); clearText.append(new String(pkCipher.doFinal(byteChunk), "UTF-8")); } return clearText.toString(); } catch (NumberFormatException e) { throw new SecurityException( "The encrypted information is corrupted or incomplete. Please make sure someone is not trying to intercept or modify encrypted message.", e); } catch (IOException e) { throw new SecurityException( "Failed to read authentication string. Please use Java version with cryptography support.", e); } catch (NoSuchAlgorithmException e) { throw new SecurityException( "Failed to read authentication string. Please use Java version with cryptography support.", e); } catch (NoSuchPaddingException e) { throw new SecurityException( "Failed to read authentication string. Please use Java version with cryptography support.", e); } catch (InvalidKeySpecException e) { throw new SecurityException( "Failed to read authentication string. Please use Java version with cryptography support.", e); } catch (InvalidKeyException e) { throw new SecurityException( "Failed to read 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 decrypt message. It might have been corrupted during transport.", e); } catch (BadPaddingException e) { throw new SecurityException("Failed to decrypt message. It might have been corrupted during transport.", e); } }
From source file:org.bibsonomy.webapp.validation.opensocial.BibSonomyOAuthValidator.java
private PublicKey getPublicKeyFromPem(String pem) throws GeneralSecurityException, IOException { InputStream stream = new ByteArrayInputStream(pem.getBytes("UTF-8")); PEMReader reader = new PEMReader(stream); byte[] bytes = reader.getDerBytes(); PublicKey pubKey;//from w ww . j a va 2 s . com if (PEMReader.PUBLIC_X509_MARKER.equals(reader.getBeginMarker())) { KeySpec keySpec = new X509EncodedKeySpec(bytes); KeyFactory fac = KeyFactory.getInstance("RSA"); pubKey = fac.generatePublic(keySpec); } else if (PEMReader.CERTIFICATE_X509_MARKER.equals(reader.getBeginMarker())) { pubKey = getPublicKeyFromDerCert(bytes); } else { throw new IOException( "Invalid PEM fileL: Unknown marker for " + " public key or cert " + reader.getBeginMarker()); } return pubKey; }
From source file:be.fedict.eid.idp.model.CryptoUtil.java
public static PublicKey getPublicKey(byte[] publicKeyBytes) throws KeyLoadException { try {// w ww. ja v a2 s . c o m X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes); KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA"); return rsaKeyFactory.generatePublic(publicKeySpec); } catch (InvalidKeySpecException e) { throw new KeyLoadException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:net.arccotangent.pacchat.filesystem.KeyManager.java
@SuppressWarnings("ResultOfMethodCallIgnored") public static void saveKeyByIP(String ip_address, PublicKey publicKey) { km_log.i("Saving public key for " + ip_address); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(publicKey.getEncoded()); File pubFile = new File(installationPath + File.separator + ip_address + ".pub"); km_log.i("Deleting old key if it exists."); if (pubFile.exists()) pubFile.delete();/*from w w w.j a va2 s . c o m*/ try { km_log.i(pubFile.createNewFile() ? "Creation of public key file successful." : "Creation of public key file failed!"); FileOutputStream pubOut = new FileOutputStream(pubFile); pubOut.write(Base64.encodeBase64(pubSpec.getEncoded())); pubOut.flush(); pubOut.close(); } catch (IOException e) { km_log.e("Error while saving public key for " + ip_address + "!"); e.printStackTrace(); } }
From source file:com.sixsq.slipstream.cookie.CryptoUtils.java
static private void setKeyPairFromDb() throws NoSuchAlgorithmException, InvalidKeySpecException, CertificateException { CookieKeyPair ckp = CookieKeyPair.load(); if (ckp == null) { return;// w ww . j ava 2s. c om } String privateKeyBase64 = ckp.getPrivateKey(); String publicKeyBase64 = ckp.getPublicKey(); if (privateKeyBase64 == null || publicKeyBase64 == null) { return; } byte[] privateKeyBytes = new Base64().decode(privateKeyBase64); KeyFactory keyFactory = KeyFactory.getInstance(keyPairAlgorithm); KeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes); privateKey = keyFactory.generatePrivate(privateKeySpec); byte[] publicKeyBytes = new Base64().decode(publicKeyBase64); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKeyBytes); keyFactory = KeyFactory.getInstance(keyPairAlgorithm); publicKey = keyFactory.generatePublic(x509KeySpec); }
From source file:jp.alessandro.android.iab.Security.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key./*from ww w . j ava 2 s . c o m*/ * * @param encodedPublicKey rsa public key generated by Google Play Developer Console * @throws IllegalArgumentException if encodedPublicKey is invalid */ protected PublicKey generatePublicKey(String encodedPublicKey) throws NoSuchAlgorithmException, InvalidKeySpecException, IllegalArgumentException { byte[] decodedKey = Base64.decode(encodedPublicKey, Base64.DEFAULT); KeyFactory keyFactory = KeyFactory.getInstance(KEY_FACTORY_ALGORITHM); return keyFactory.generatePublic(new X509EncodedKeySpec(decodedKey)); }
From source file:gemlite.core.util.RSAUtils.java
/** * <p>/*w ww . j a v a 2s . c o m*/ * ?? * </p> * * @param data * ? * @param publicKey * (BASE64?) * @param sign * ?? * * @return * @throws Exception * */ public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { byte[] keyBytes = Base64Utils.decode(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey publicK = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initVerify(publicK); signature.update(data); return signature.verify(Base64Utils.decode(sign)); }