List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec
public X509EncodedKeySpec(byte[] encodedKey)
From source file:com.zxy.commons.codec.rsa.RSAUtils.java
/** * <p>// w w w. j a v a 2 s. c o m * * </p> * * @param data ?? * @param publicKey (BASE64?) * @return byte * @throws Exception Exception */ public static byte[] encryptByPublicKey(byte[] data, String publicKey) throws Exception { byte[] keyBytes = Base64.decodeBase64(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 index = 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); index++; offSet = index * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = out.toByteArray(); out.close(); return encryptedData; }
From source file:edu.vt.middleware.crypt.util.CryptReader.java
/** * Reads a DER-encoded X.509 public key from an input stream into a {@link * PublicKey} object.// w ww . java2s . c om * * @param keyStream Input stream containing DER-encoded X.509 public key. * @param algorithm Name of encryption algorithm used by key. * * @return Public key containing data read from stream. * * @throws CryptException On key format errors. * @throws IOException On key read errors. */ public static PublicKey readPublicKey(final InputStream keyStream, final String algorithm) throws CryptException, IOException { final KeyFactory kf = CryptProvider.getKeyFactory(algorithm); try { final X509EncodedKeySpec keySpec = new X509EncodedKeySpec(readData(keyStream)); return kf.generatePublic(keySpec); } catch (InvalidKeySpecException e) { throw new CryptException("Invalid public key format.", e); } }
From source file:RGSDigestTools.SignatureTool.java
public void initKeysWithFiles(String PrivkeyResource, String PubkeyResource) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException, UnrecoverableEntryException, InvalidKeySpecException { // KeyStore ks = TrustStoreLoader.loadKeyStore(pKeyStorePath,pKeyStorePasswd); // KeyStore.PasswordProtection passProtection = new KeyStore.PasswordProtection(pPrivKeyPasswd.toCharArray()); // KeyStore.PrivateKeyEntry DSKeyEnt = (KeyStore.PrivateKeyEntry)ks.getEntry(pDSAlias, passProtection); // // w w w . j a v a2s .co m InputStream is_piv = SignatureTool.class.getResourceAsStream(PrivkeyResource); ByteArrayOutputStream baos_priv = new ByteArrayOutputStream(); int read_priv = is_piv.read(); while (read_priv != -1) { baos_priv.write(read_priv); read_priv = is_piv.read(); } byte[] keyBytes_priv = baos_priv.toByteArray(); PKCS8EncodedKeySpec spec_pkcs8 = new PKCS8EncodedKeySpec(keyBytes_priv); KeyFactory keyFactoryPriv = KeyFactory.getInstance("RSA"); this.signKey = keyFactoryPriv.generatePrivate(spec_pkcs8); // this.signKey = DSKeyEnt.getPrivateKey(); InputStream is = SignatureTool.class.getResourceAsStream(PubkeyResource); ByteArrayOutputStream baos = new ByteArrayOutputStream(); int read = is.read(); while (read != -1) { baos.write(read); read = is.read(); } byte[] keyBytes = baos.toByteArray(); X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); this.verifyKey = keyFactory.generatePublic(spec); }
From source file:com.torresbueno.RSAEncryptionDecryptionUtil.java
/** * read Public Key From File//www . j a v a2s .c om * @param filePath * @return PublicKey * @throws IOException */ public PublicKey readPublicKeyFromFile(String filePath) throws Exception { // Read file to a byte array. Path path = Paths.get(filePath); byte[] pubKeyByteArray = Files.readAllBytes(path); X509EncodedKeySpec spec = new X509EncodedKeySpec(pubKeyByteArray); KeyFactory kf = KeyFactory.getInstance("RSA"); return kf.generatePublic(spec); }
From source file:org.bedework.util.security.pki.PKITools.java
/** * @param pubKeyBytes// ww w. j a va2s. c o m * @param item * @return encrypted value * @throws PKIException */ public String encrypt(final byte[] pubKeyBytes, final String item) throws PKIException { byte[] encryptedItem = null; Cipher asymmetricCipher; try { X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKeyBytes); Key publicKey; if (curSchema.pName == null) { KeyFactory kf = KeyFactory.getInstance(curSchema.algorithm); publicKey = kf.generatePublic(publicKeySpec); asymmetricCipher = Cipher.getInstance(curSchema.algorithm); } else { KeyFactory kf = KeyFactory.getInstance(curSchema.algorithm, curSchema.pName); publicKey = kf.generatePublic(publicKeySpec); asymmetricCipher = Cipher.getInstance(curSchema.algorithm, curSchema.pName); } asymmetricCipher.init(Cipher.ENCRYPT_MODE, publicKey); encryptedItem = asymmetricCipher.doFinal(item.getBytes()); } catch (Throwable t) { throw new PKIException(t); } return new String(b64.encode(encryptedItem)); }
From source file:org.psl.fidouaf.core.crypto.KeyCodec.java
public static PublicKey getRSAPublicKey(byte[] encodedPubKey) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException { RSAPublicKey pubKey8 = (RSAPublicKey) RSAPublicKey.getInstance(encodedPubKey); SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo( new RSAKeyParameters(false, pubKey8.getModulus(), pubKey8.getPublicExponent())); X509EncodedKeySpec spec = new X509EncodedKeySpec(info.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); return keyFactory.generatePublic(spec); }
From source file:JALPTest.java
/** * Creates a Producer using the given command line params. * * @param xml the ApplicationMetadataXML * @param socketPath a String which is the path to the socket * @param privateKeyPath a String which is the path to the private key in DER format * @param publicKeyPath a String which is the path to the public key in DER format * @param certPath a String which is the path to the certificate * @param hasDigest a Boolean, true to set a digest method in the producer * @return the created Producer// www . ja v a 2 s.c o m * @throws Exception */ private static Producer createProducer(ApplicationMetadataXML xml, String socketPath, String privateKeyPath, String publicKeyPath, String certPath, Boolean hasDigest) throws Exception { Producer producer = new Producer(xml); producer.setSocketFile(socketPath); if (privateKeyPath != null && !"".equals(privateKeyPath)) { File privateKeyFile = new File(privateKeyPath); DataInputStream privateDis = new DataInputStream(new FileInputStream(privateKeyFile)); byte[] privateKeyBytes = new byte[(int) privateKeyFile.length()]; privateDis.readFully(privateKeyBytes); privateDis.close(); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); KeySpec privateKs = new PKCS8EncodedKeySpec(privateKeyBytes); PrivateKey privateKey = keyFactory.generatePrivate(privateKs); File publicKeyFile = new File(publicKeyPath); DataInputStream publicDis = new DataInputStream(new FileInputStream(publicKeyFile)); byte[] publicKeyBytes = new byte[(int) publicKeyFile.length()]; publicDis.readFully(publicKeyBytes); publicDis.close(); KeySpec publicKs = new X509EncodedKeySpec(publicKeyBytes); PublicKey publicKey = keyFactory.generatePublic(publicKs); producer.setPrivateKey(privateKey); producer.setPublicKey(publicKey); } if (certPath != null && !"".equals(certPath)) { InputStream inputStream = new FileInputStream(certPath); CertificateFactory cf = CertificateFactory.getInstance("X.509"); X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream); inputStream.close(); producer.setCertificate(cert); } if (hasDigest) { producer.setDigestMethod(DMType.SHA256); } return producer; }
From source file:SecureConnection.java
private byte[] generateSecret(byte[] otherPubKeyBytes) throws Exception { KeyFactory keyFac = KeyFactory.getInstance("DH"); X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(otherPubKeyBytes); PublicKey otherPubKey = keyFac.generatePublic(x509KeySpec); this.keyAgree.doPhase(otherPubKey, true); return keyAgree.generateSecret(); }
From source file:mitm.common.security.KeyEncoderTest.java
private static PublicKey decodePublicKey(String encoded) throws DecoderException, InvalidKeySpecException { byte[] rawKey = Hex.decodeHex(encoded.toCharArray()); KeySpec keySpec = new X509EncodedKeySpec(rawKey); return keyFactory.generatePublic(keySpec); }
From source file:org.nick.ghettounlock.GhettoTrustAgent.java
public static RSAPublicKey getPublicKey(Context ctx) { String pubKeyStr = PreferenceManager.getDefaultSharedPreferences(ctx).getString(PREF_PUB_KEY, null); if (pubKeyStr == null) { return null; }/*from ww w . j av a2 s. c o m*/ try { X509EncodedKeySpec keySpec = new X509EncodedKeySpec( Base64.decode(pubKeyStr.getBytes("UTF-8"), Base64.DEFAULT)); KeyFactory kf = KeyFactory.getInstance("RSA"); RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(keySpec); return pubKey; } catch (Exception e) { throw new RuntimeException(e); } }