List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec
public X509EncodedKeySpec(byte[] encodedKey)
From source file:com.vexsoftware.votifier.crypto.RSAIO.java
/** * Loads an RSA key pair from a directory. The directory must have the files * "public.key" and "private.key"./*from www . j a v a2 s . c o m*/ * * @param directory * The directory to load from * @return The key pair * @throws Exception * If an error occurs */ public static KeyPair load(File directory) throws Exception { // Read the public key file. File publicKeyFile = new File(directory + "/public.key"); byte[] encodedPublicKey = FileUtils.readFileToByteArray(publicKeyFile); encodedPublicKey = DatatypeConverter.parseBase64Binary(new String(encodedPublicKey)); // Read the private key file. File privateKeyFile = new File(directory + "/private.key"); byte[] encodedPrivateKey = FileUtils.readFileToByteArray(privateKeyFile); encodedPrivateKey = DatatypeConverter.parseBase64Binary(new String(encodedPrivateKey)); // Instantiate and return the key pair. KeyFactory keyFactory = KeyFactory.getInstance("RSA"); X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(encodedPublicKey); PublicKey publicKey = keyFactory.generatePublic(publicKeySpec); PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); return new KeyPair(publicKey, privateKey); }
From source file:com.streamsets.lib.security.util.DataSignature.java
public PublicKey decodePublicKey(String encodedPublicKey) throws GeneralSecurityException { byte[] bytes = Base64.decodeBase64(encodedPublicKey); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(bytes); KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN"); return keyFactory.generatePublic(pubKeySpec); }
From source file:com.vimukti.accounter.developer.api.PublicKeyGenerator.java
private static void generate() throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException, KeyStoreException, CertificateException, IOException, URISyntaxException { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("DSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed("VimTech".getBytes("UTF-8")); keyGen.initialize(1024, random);/*ww w . j a v a 2 s .co m*/ KeyPair pair = keyGen.generateKeyPair(); PrivateKey priv = pair.getPrivate(); PublicKey pub = pair.getPublic(); System.out.println(priv); System.out.println(pub); byte[] encoded = pub.getEncoded(); byte[] encodeBase64 = Base64.encodeBase64(encoded); System.out.println("Public Key:" + new String(encodeBase64)); byte[] encodedPrv = priv.getEncoded(); byte[] encodeBase64Prv = Base64.encodeBase64(encodedPrv); System.out.println("Private Key:" + new String(encodeBase64Prv)); byte[] decodeBase64 = Base64.decodeBase64(encodeBase64); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(decodeBase64); KeyFactory keyFactory = KeyFactory.getInstance("DSA"); System.out.println(keyFactory.generatePublic(pubKeySpec).equals(pub)); }
From source file:acceptable_risk.nik.uniobuda.hu.andrawid.util.Security.java
/** * Generates a PublicKey instance from a string containing the * Base64-encoded public key.// w w w.j av 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) { 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:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * /*from w ww . j a v a2 s .c o m*/ * * @param data? * @param key * * @return byte[] ? */ public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception { // KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ? // ??? X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); // PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, pubKey); return cipher.doFinal(data); }
From source file:my.adam.smo.common.AsymmetricEncryptionBox.java
@PostConstruct public void init() throws NoSuchAlgorithmException { keyGen = KeyPairGenerator.getInstance("RSA"); try {/*from w w w .ja v a 2 s .c o m*/ PKCS8EncodedKeySpec privKeySpec = new PKCS8EncodedKeySpec(Base64.decode(this.privKeyS)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); prvKey = keyFactory.generatePrivate(privKeySpec); } catch (InvalidKeySpecException e) { logger.error("invalid private key", e); } try { X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(Base64.decode(this.pubKeyS)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); pubKey = keyFactory.generatePublic(pubKeySpec); } catch (InvalidKeySpecException e) { logger.error("invalid public key", e); } }
From source file:com.buzzcoders.security.cryptoutils.asymmetric.AbstractAsymmetricEncryptionModule.java
public void storePublicKey(String path, PublicKey publicKey) { FileOutputStream fos = null;/*from w w w .ja v a 2s . c o m*/ try { X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded()); fos = new FileOutputStream(path); fos.write(x509EncodedKeySpec.getEncoded()); } catch (FileNotFoundException e) { LOG.error("Cannot save the public key to the specified path.", e); } catch (IOException e) { LOG.error("An I/O error occured while saving the public key", e); } finally { IOUtils.closeQuietly(fos); } }
From source file:org.orbeon.oxf.processor.SignatureVerifierProcessor.java
public ProcessorOutput createOutput(String name) { final ProcessorOutput output = new ProcessorOutputImpl(SignatureVerifierProcessor.this, name) { public void readImpl(PipelineContext context, final XMLReceiver xmlReceiver) { try { final Document pubDoc = readCacheInputAsDOM4J(context, INPUT_PUBLIC_KEY); final String pubString = XPathUtils.selectStringValueNormalize(pubDoc, "/public-key"); final byte[] pubBytes = Base64.decode(pubString); final X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(pubBytes); final KeyFactory keyFactory = KeyFactory.getInstance("DSA"); final PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); final Signature dsa = Signature.getInstance("SHA1withDSA"); dsa.initVerify(pubKey);/*from ww w . ja v a 2s. co m*/ final Document data = readInputAsDOM4J(context, INPUT_DATA); final Node sigDataNode = data.selectSingleNode("/signed-data/data/*"); final String sig = StringUtils .trimToEmpty(XPathUtils.selectStringValue(data, "/signed-data/signature")); sigDataNode.detach(); final Document sigData = new NonLazyUserDataDocument(); sigData.add(sigDataNode); dsa.update(Dom4jUtils.domToString(sigData).getBytes("utf-8")); // Verify signature and throw in case of failure try { if (!dsa.verify(Base64.decode(sig))) throw new OXFException("Signature verification failed"); } catch (SignatureException e) { throw e; } catch (Exception e) { // A number of things can fail above, including Base64 decoding // NOTE: We don't pas the cause so that we can match on SignatureException as root Exception throw new SignatureException("Signature verification failed"); } // Signature verification passed final LocationSAXWriter saw = new LocationSAXWriter(); saw.setContentHandler(xmlReceiver); saw.write(sigData); } catch (Exception e) { throw new OXFException(e); } } }; addOutput(name, output); return output; }
From source file:org.apache.cordova.crypt.Crypt.java
public String encrypt(String data, String publickey) throws Exception { publickey = publickey.replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", ""); try {// w w w .ja va2 s. c om byte[] publickeyRaw = Base64.decode(publickey, Base64.DEFAULT); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publickeyRaw); KeyFactory fact = KeyFactory.getInstance("RSA"); PublicKey pub = fact.generatePublic(keySpec); byte[] text = data.getBytes(Charset.forName("UTF-8")); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, pub); byte[] cipherString = cipher.doFinal(text); return new String(Base64.encode(cipherString, Base64.DEFAULT)); } catch (Exception e) { Log.w("Crypt", e); return null; } }
From source file:org.picketlink.pki.internal.DefaultEncryptionAuthority.java
@Override public String encrypt(String rawText, String publicKeyPath, String transformation, String encoding) { String encryptedText = null;//from w w w. j av a2 s .co m try { X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec( IOUtils.toByteArray(new FileInputStream(publicKeyPath))); Cipher cipher = Cipher.getInstance(transformation); cipher.init(Cipher.ENCRYPT_MODE, KeyFactory.getInstance("RSA").generatePublic(x509EncodedKeySpec)); encryptedText = new String(Base64.encodeBase64(cipher.doFinal(rawText.getBytes(encoding)))); } catch (Exception e) { throw new RuntimeException("Could not encrypt rawText.", e); } return encryptedText; }