Example usage for java.security KeyFactory generatePublic

List of usage examples for java.security KeyFactory generatePublic

Introduction

In this page you can find the example usage for java.security KeyFactory generatePublic.

Prototype

public final PublicKey generatePublic(KeySpec keySpec) throws InvalidKeySpecException 

Source Link

Document

Generates a public key object from the provided key specification (key material).

Usage

From source file:com.streamsets.datacollector.publicrestapi.CredentialsDeploymentResource.java

private boolean validateSignature(CredentialsBeanJson credentialsBeanJson)
        throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
    // getProperty so we can test it
    String publicKey = Preconditions.checkNotNull(System.getProperty(DPM_AGENT_PUBLIC_KEY));

    X509EncodedKeySpec kspec = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKey));
    KeyFactory kf = KeyFactory.getInstance("RSA");
    PublicKey key = kf.generatePublic(kspec);
    Signature sig = Signature.getInstance("SHA256withRSA");
    sig.initVerify(key);//from   w  w  w  . jav  a 2 s.c o  m
    sig.update(credentialsBeanJson.getToken().getBytes(Charsets.UTF_8));
    LOG.info("Token : {}, Signature {}", credentialsBeanJson.getToken(),
            credentialsBeanJson.getTokenSignature());
    return sig.verify(Base64.getDecoder().decode(credentialsBeanJson.getTokenSignature()));
}

From source file:com.tenduke.example.scribeoauth.JwtLoginServlet.java

/**
 * Initializes this servlet./*from   w w  w  . j a  va2 s . co  m*/
 * @param config Servlet configuration.
 * @throws ServletException For errors during init.
 */
@Override
public void init(final ServletConfig config) throws ServletException {
    //
    super.init(config);
    //
    final JSONObject jwtPublicKey = readConfiguration("idp.jwt.publickey.json", config.getServletContext());
    try {
        //
        byte[] publicKeyDecoded = Base64.decodeBase64(jwtPublicKey.getString("publicKey"));
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKeyDecoded);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        publicKey = keyFactory.generatePublic(keySpec);
    } catch (InvalidKeySpecException | NoSuchAlgorithmException ex) {
        //
        throw new ServletException("No way, basic RSA based key generation failed...", ex);
    }
}

From source file:com.boubei.tss.modules.license.LicenseManager.java

/**
 * <pre>/*from w  ww .j  av a  2  s .  c  om*/
 * ?license????????
 * </pre>
 * @param license
 * @return
 * @throws Exception
 */
boolean validate(License license) throws Exception {
    File keyFile = new File(LicenseFactory.PUBLIC_KEY_FILE);
    String publicKey = FileHelper.readFile(keyFile).trim();

    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey));
    KeyFactory keyFactory = KeyFactory.getInstance(LicenseFactory.KEY_ALGORITHM);
    java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

    Signature sig = Signature.getInstance(LicenseFactory.KEY_ALGORITHM);
    sig.initVerify(pubKey);
    sig.update(license.getFingerprint());
    return sig.verify(EasyUtils.decodeHex(license.signature));
}

From source file:com.jinhe.tss.core.common.license.LicenseManager.java

/**
 * ?license??//from  ww w .j a v  a 2 s.  co  m
 * ?Mac??????
 * ???????
 * 
 * @param license
 * @return
 * @throws Exception
 */
boolean validate(License license) throws Exception {
    String macAddress = license.getMacAddress();
    if (macAddress != null && macAddress.length() > 0) {
        String curMacAddress = MacAddressUtil.getMacAddress();
        if (!macAddress.equals(curMacAddress))
            return false;
    }
    String publicKey = FileHelper.readFile(new File(LicenseFactory.PUBLIC_KEY_FILE)).trim();

    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey));
    KeyFactory keyFactory = KeyFactory.getInstance("DSA");
    java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

    Signature sig = Signature.getInstance("DSA");
    sig.initVerify(pubKey);
    sig.update(license.getFingerprint());
    return sig.verify(EasyUtils.decodeHex(license.getLicenseSignature()));
}

From source file:RGSDigestTools.SignatureTool.java

/**
 * Init keys with private key from keystore and pubkey from resource
 * @param pKeyStorePath//from  ww  w.  j av a 2  s.  c  o  m
 * @param pKeyStorePasswd
 * @param pDSAlias
 * @param pPrivKeyPasswd
 * @param PubkeyResource 
 * @throws java.security.KeyStoreException 
 * @throws java.security.cert.CertificateException 
 * @throws java.security.NoSuchAlgorithmException 
 * @throws java.io.IOException 
 * @throws java.security.UnrecoverableEntryException 
 * @throws java.security.spec.InvalidKeySpecException 
 */
public void initKeysWithKeystoreAndFile(String pKeyStorePath, String pKeyStorePasswd, String pDSAlias,
        String pPrivKeyPasswd, 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);

    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:org.jboss.aerogear.cordova.crypto.CryptoPlugin.java

private java.security.KeyPair parseKeyPairFromJson(byte[] encodedPublicKey, byte[] encodedPrivateKey) {
    try {//from   w w w  .  ja v  a 2  s  . c om
        KeyFactory fact = KeyFactory.getInstance("ECDH", AeroGearCrypto.PROVIDER);
        PublicKey publicKey2 = fact.generatePublic(new X509EncodedKeySpec(encodedPublicKey));
        java.security.PrivateKey privateKey2 = fact.generatePrivate(new PKCS8EncodedKeySpec(encodedPrivateKey));
        return new java.security.KeyPair(publicKey2, privateKey2);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException("could not reconstruct key pair from json!", e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException("could not reconstruct key pair from json!", e);
    } catch (NoSuchProviderException e) {
        throw new RuntimeException("could not reconstruct key pair from json!", e);
    }
}

From source file:bftsmart.reconfiguration.util.RSAKeyLoader.java

private PublicKey getPublicKeyFromString(String key) throws Exception {
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.decodeBase64(key));
    PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
    return publicKey;
}

From source file:com.jinhe.tss.framework.license.LicenseManager.java

/**
 * <pre>/* w w w .  j  a  v  a 2  s  .  c o m*/
 * ?license??
 * ?Mac??????
 * ???????
 * </pre>
 * @param license
 * @return
 * @throws Exception
 */
boolean validate(License license) throws Exception {
    String macAddress = license.macAddress;
    if (!EasyUtils.isNullOrEmpty(macAddress)) {
        String curMacAddress = MacAddress.getMacAddress();
        if (!macAddress.equals(curMacAddress)) {
            return false;
        }
    }

    File keyFile = new File(LicenseFactory.PUBLIC_KEY_FILE);
    String publicKey = FileHelper.readFile(keyFile).trim();

    X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey));
    KeyFactory keyFactory = KeyFactory.getInstance(LicenseFactory.KEY_ALGORITHM);
    java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);

    Signature sig = Signature.getInstance(LicenseFactory.KEY_ALGORITHM);
    sig.initVerify(pubKey);
    sig.update(license.getFingerprint());
    return sig.verify(EasyUtils.decodeHex(license.licenseSignature));
}

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 ww . j  av  a  2 s  . c om*/
    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:tkbautobooking.TKBAutoBooking.java

private boolean checkPermission(String userId, String rsaData) {
    try {/*from  w  w  w  .j  av a2  s  .c  om*/

        KeyFactory kf = KeyFactory.getInstance("RSA");
        PublicKey publicKey = kf
                .generatePublic(new X509EncodedKeySpec(Hex.decodeHex(RSA_PUBLIC_KEY.toCharArray())));

        Cipher cipher = Cipher.getInstance("RSA");

        cipher.init(Cipher.DECRYPT_MODE, publicKey);
        byte[] decodeBytes = cipher.doFinal(Hex.decodeHex(rsaData.toCharArray()));
        String decodeString = new String(decodeBytes);

        return userId.equals(decodeString);
    } catch (NoSuchAlgorithmException | DecoderException | InvalidKeySpecException | NoSuchPaddingException
            | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) {
        return false;
    }
}