Example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

List of usage examples for java.security.spec X509EncodedKeySpec X509EncodedKeySpec

Introduction

In this page you can find the example usage for java.security.spec X509EncodedKeySpec X509EncodedKeySpec.

Prototype

public X509EncodedKeySpec(byte[] encodedKey) 

Source Link

Document

Creates a new X509EncodedKeySpec with the given encoded key.

Usage

From source file:com.java.demo.RsaDemo.java

public static boolean Verify(String str, byte[] enstr) {
    try {/*from   w w w .ja  va2  s . c  o  m*/
        X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rSAPublicKey.getEncoded());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);
        Signature signature = Signature.getInstance("MD5withRSA");
        signature.initVerify(publicKey);
        signature.update(str.getBytes());
        boolean result = signature.verify(enstr);
        return result;
    } catch (Exception e) {
        System.out.println("com.Java.Demo.RsaDemo.Verify()" + e.getMessage());
        return false;
    }
}

From source file:org.hypoport.jwt.common.Toolbox.java

public static RSAPublicKey readRSAPublicKey(FileReader keyReader) throws Exception {
    return (RSAPublicKey) KeyFactory.getInstance("RSA")
            .generatePublic(new X509EncodedKeySpec(readPemFile(keyReader)));
}

From source file:org.intermine.web.security.Base64PublicKeyDecoder.java

@Override
public PublicKey decode(String input) throws DecodingException {
    byte[] decoded = decoder.decode(input);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(decoded);

    try {//  w  w w. j  a va 2 s . c  o m
        return fact.generatePublic(x509KeySpec);
    } catch (InvalidKeySpecException e) {
        throw new DecodingException(e);
    }
}

From source file:net.arccotangent.pacchat.filesystem.KeyManager.java

private static void saveKeys(PrivateKey privkey, PublicKey pubkey) {
    km_log.i("Saving keys to disk.");

    X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubkey.getEncoded());
    PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privkey.getEncoded());

    try {// w w  w .java2s  .  c o  m
        km_log.i(pubkeyFile.createNewFile() ? "Creation of public key file successful."
                : "Creation of public key file failed!");

        FileOutputStream pubOut = new FileOutputStream(pubkeyFile);
        pubOut.write(Base64.encodeBase64(pubSpec.getEncoded()));
        pubOut.flush();
        pubOut.close();

    } catch (IOException e) {
        km_log.e("Error while saving public key!");
        e.printStackTrace();
    }

    try {
        km_log.i(privkeyFile.createNewFile() ? "Creation of private key file successful."
                : "Creation of private key file failed!");

        FileOutputStream privOut = new FileOutputStream(privkeyFile);
        privOut.write(Base64.encodeBase64(privSpec.getEncoded()));
        privOut.flush();
        privOut.close();

    } catch (IOException e) {
        km_log.e("Error while saving private key!");
        e.printStackTrace();
    }

    km_log.i("Finished saving keys to disk. Operation appears successful.");
}

From source file:br.edu.ufcg.lsd.commune.network.signature.Util.java

public static PublicKey decodePublicKey(String pubKeyStr) throws InvalidKeySpecException {
    byte[] binaryArray = decodeStringOnBase64(pubKeyStr);
    KeyFactory keyFactory;/*from w  w w.j a v  a 2 s  .  c  o m*/
    try {
        keyFactory = KeyFactory.getInstance(SignatureConstants.KEY_GEN_ALGORITHM);
    } catch (NoSuchAlgorithmException e) {
        //We're assuming that we are always instantiating a valid algorithm
        throw new CommuneRuntimeException(e);
    }
    EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(binaryArray);
    return keyFactory.generatePublic(publicKeySpec);
}

From source file:com.github.ibole.infrastructure.security.key.PemUtils.java

private static PublicKey getPublicKey(byte[] keyBytes, String algorithm) {
    PublicKey publicKey = null;/* w w  w  . j  a va  2  s.  c  o  m*/
    try {
        KeyFactory kf = KeyFactory.getInstance(algorithm);
        EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
        publicKey = kf.generatePublic(keySpec);
    } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
        MoreThrowables.throwIfUnchecked(e);
    }
    return publicKey;
}

From source file:com.zf.decipher.DataEn.java

private static byte[] getSecretKey(byte[] publicKey, byte[] privateKey) throws Exception {
    KeyFactory keyFactory = KeyFactory.getInstance("DH");
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(publicKey);
    PublicKey pubKey = keyFactory.generatePublic(x509KeySpec);
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
    PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
    KeyAgreement keyAgree = KeyAgreement.getInstance(keyFactory.getAlgorithm());
    keyAgree.init(priKey);//from w w w  .  j a  v  a 2  s  . co  m
    keyAgree.doPhase(pubKey, true);
    return keyAgree.generateSecret(AES).getEncoded();
}

From source file:com.alliander.osgp.shared.security.CertificateHelper.java

/**
 * Create public key from public key file on disk
 *
 * @param keyPath//from   w w w .ja  v  a 2 s.  co m
 *            path to key
 * @param keyType
 *            type of key
 * @return instance of public key
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws IOException
 * @throws NoSuchProviderException
 */
public static PublicKey createPublicKey(final String keyPath, final String keyType, final String provider)
        throws NoSuchAlgorithmException, InvalidKeySpecException, IOException, NoSuchProviderException {
    final byte[] key = readKeyFromDisk(keyPath);

    final X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(key);
    final KeyFactory publicKeyFactory = KeyFactory.getInstance(keyType, provider);
    return publicKeyFactory.generatePublic(publicKeySpec);
}

From source file:cl.nic.dte.extension.AutorizacionTypeExtensionHandler.java

@SuppressWarnings("unchecked")
public static PublicKey getPublicKey(AutorizacionType auth)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    List<PEMItem> items = PEMUtil.decode(auth.getRSAPUBK().getBytes());

    for (PEMItem item : items) {
        if ("PUBLIC KEY".equals(item.pemType)) {
            X509EncodedKeySpec enc;
            try {
                enc = new X509EncodedKeySpec(item.getDerBytes());
                KeyFactory rsaKeyFac;
                rsaKeyFac = KeyFactory.getInstance("RSA");
                return (PublicKey) rsaKeyFac.generatePublic((enc));
            } catch (GeneralSecurityException e) {
                throw new InvalidKeySpecException(e);
            }//  ww w  . j  a  va 2s . c o  m
        }
    }
    return null;
}