Example usage for java.security.spec PKCS8EncodedKeySpec PKCS8EncodedKeySpec

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

Introduction

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

Prototype

public PKCS8EncodedKeySpec(byte[] encodedKey) 

Source Link

Document

Creates a new PKCS8EncodedKeySpec with the given encoded key.

Usage

From source file:com.hoccer.api.android.AsyncLinccer.java

public static PrivateKey getPrivateKeyFromSharedPreferences(Context context)
        throws IOException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    SharedPreferences prefs = context.getSharedPreferences(PREFERENCES, Context.MODE_PRIVATE);

    String defaultValue = "";
    String storedValue = prefs.getString(PREF_PRIVATE_KEY, defaultValue);
    Log.v(LOG_TAG, "getPrivateKeyFromSharedPreferences, storedValue=" + storedValue);

    byte[] myEncodedPrivateKey = Base64.decode(storedValue);

    PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(CryptoHelper.wrapRSA1024_PKCS8(myEncodedPrivateKey));

    KeyFactory kf = KeyFactory.getInstance("RSA");
    PrivateKey myPrivateKey = kf.generatePrivate(privSpec);

    return myPrivateKey;
}

From source file:com.wso2telco.gsma.authenticators.GSMAMSISDNAuthenticator.java

/**
 * Read private key from file.//from   www. ja v a  2s . c  o m
 *
 * @param fileName the file name
 * @return the private key
 * @throws IOException              Signals that an I/O exception has occurred.
 * @throws NoSuchAlgorithmException the no such algorithm exception
 * @throws InvalidKeySpecException  the invalid key spec exception
 */
public PrivateKey readPrivateKeyFromFile(String fileName)
        throws IOException, NoSuchAlgorithmException, InvalidKeySpecException {

    String publicK = readStringKey(fileName);
    //byte[] keyBytes = new BASE64Decoder().decodeBuffer(publicK);
    byte[] keyBytes = Base64.decodeBase64(publicK.getBytes());
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    return fact.generatePrivate(keySpec);
}

From source file:org.parelon.pskc.CryptManager.java

/**
 * Recover the full private key as bytes array and initialize the PrivateKey.
 * Fragments MUST be Base64Decode first!
 * /*from   w  ww .  j  a v a 2s  .  c o  m*/
 * @param privateKeyFragment1 First half of the Private Key.
 * @param privateKeyFragment2 Second half of the Private Key.
 * 
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException 
 */
private void initPrivateKey(byte[] privateKeyFragment1, byte[] privateKeyFragment2)
        throws NoSuchAlgorithmException, InvalidKeySpecException, Base64DecodingException {
    this.rsaPrivateKey = KeyFactory.getInstance("RSA").generatePrivate(
            new PKCS8EncodedKeySpec(concatByteArrays(privateKeyFragment1, privateKeyFragment2)));
}

From source file:net.firejack.platform.web.security.x509.KeyUtils.java

public static Properties getProperties(byte[] key, byte[] data) {
    Properties properties = new Properties();
    try {//from  w w  w.j a  v  a  2 s. com
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(key);
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

        byte[] decrypt = decrypt(privateKey, data);

        InputStream stream = new ByteArrayInputStream(decrypt);
        properties.load(stream);
    } catch (Exception e) {
        logger.trace(e);
    }
    return properties;
}

From source file:nl.b3p.viewer.stripes.CycloramaActionBean.java

private String getTIDFromBase64EncodedString(String base64Encoded, String token)
        throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException,
        UnsupportedEncodingException {

    String tid = null;/*  w  w w  . j  a v a 2s.c  o  m*/
    Base64 encoder = new Base64();

    byte[] tempBytes = encoder.decode(base64Encoded.getBytes());

    KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA");
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(tempBytes);
    RSAPrivateKey privKey = (RSAPrivateKey) rsaKeyFac.generatePrivate(encodedKeySpec);

    byte[] signature = sign(privKey, token);

    String base64 = new String(encoder.encode(signature));
    tid = URLEncoder.encode(token + "&" + base64, URL_ENCODING);

    return tid;
}

From source file:com.torresbueno.RSAEncryptionDecryptionUtil.java

/**
 * Get a private key from the privateKeyString property.
 * @return a standard private key object.
 * @throws NoSuchAlgorithmException/*  w  w  w.  ja va2 s  .c om*/
 * @throws InvalidKeySpecException
 */
public PrivateKey getPrivateKeyFromString()
        throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    if (privateKeyString == null)
        return null;
    byte[] encodedPv = Base64.decodeBase64(privateKeyString);
    PKCS8EncodedKeySpec keySpecPv = new PKCS8EncodedKeySpec(encodedPv);
    return getKeyFactoryInstance().generatePrivate(keySpecPv);
}

From source file:com.streamsets.pipeline.lib.http.oauth2.OAuth2ConfigBean.java

private static PrivateKey parseRSAKey(String key, Stage.Context context, List<Stage.ConfigIssue> issues) {
    String privKeyPEM = key.replace("-----BEGIN PRIVATE KEY-----\n", "");
    privKeyPEM = privKeyPEM.replace("-----END PRIVATE KEY-----", "");
    privKeyPEM = privKeyPEM.replace("\n", "");
    privKeyPEM = privKeyPEM.replace("\r", "");

    try {/*from   w  w  w . j  a va 2s .  co  m*/
        // Base64 decode the data
        byte[] encoded = Base64.getDecoder().decode(privKeyPEM.getBytes());

        // PKCS8 decode the encoded RSA private key
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(encoded);

        KeyFactory kf = KeyFactory.getInstance(RSA);
        return kf.generatePrivate(keySpec);
    } catch (NoSuchAlgorithmException ex) {
        LOG.error(Utils.format("'{}' algorithm not available", RSA), ex);
        issues.add(context.createConfigIssue(CONFIG_GROUP, PREFIX + "algorithm", HTTP_25));
    } catch (InvalidKeySpecException ex) {
        LOG.error(Utils.format("'{}' algorithm not available", RSA), ex);
        issues.add(context.createConfigIssue(CONFIG_GROUP, PREFIX + "key", HTTP_26));
    } catch (IllegalArgumentException ex) {
        LOG.error("Invalid key", ex);
        issues.add(context.createConfigIssue(CONFIG_GROUP, PREFIX + "key", HTTP_27, ex.toString()));
    }
    return null;
}

From source file:org.teknux.jettybootstrap.keystore.JettyKeystore.java

private static PrivateKey loadPrivateKey(InputStream privateKeyOutputStream, String algorithm)
        throws JettyKeystoreException {
    try {//from   w ww  .ja  v  a2s.c o m
        String contentKeyFile = IOUtils.toString(privateKeyOutputStream);
        String contentKeyFileWithoutHeaderAndFooter = contentKeyFile
                .replaceAll(PRIVATE_KEY_HEADER_FOOTER_PATTERN, "");
        byte[] decodedKeyFile = Base64.decode(contentKeyFileWithoutHeaderAndFooter);

        KeyFactory keyFactory = KeyFactory.getInstance(algorithm);

        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(decodedKeyFile);
        PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

        return privateKey;
    } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException | DecoderException e) {
        throw new JettyKeystoreException(JettyKeystoreException.ERROR_LOAD_PRIVATE_KEY,
                "Can not load private key", e);
    }
}

From source file:org.apache.cloudstack.utils.auth.SAMLUtils.java

public static PrivateKey loadPrivateKey(String privateKey) {
    byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(privateKey);
    PKCS8EncodedKeySpec pkscs8KeySpec = new PKCS8EncodedKeySpec(sigBytes);
    KeyFactory keyFact = SAMLUtils.getKeyFactory();
    if (keyFact == null)
        return null;
    try {/* ww  w.jav a2s . co  m*/
        return keyFact.generatePrivate(pkscs8KeySpec);
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage());
    }
    return null;
}

From source file:com.torresbueno.RSAEncryptionDecryptionUtil.java

/**
 * Get a private key object from a string.
 * @param privateKey - A Base64 encoded string with a PKCS8 encoded key spec.
 * @return a standard private key object.
 * @throws NoSuchAlgorithmException//from   ww w.ja v a  2  s. c  om
 * @throws InvalidKeySpecException
 */
public PrivateKey getPrivateKeyFromString(String privateKey)
        throws NoSuchAlgorithmException, InvalidKeySpecException, IOException {
    byte[] encodedPv = Base64.decodeBase64(privateKey);
    PKCS8EncodedKeySpec keySpecPv = new PKCS8EncodedKeySpec(encodedPv);
    return getKeyFactoryInstance().generatePrivate(keySpecPv);
}