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.launchkey.sdk.crypto.JCECrypto.java

/**
 * Get an RSA private key utilizing the provided provider and PEM formatted string
 * @param provider Provider to generate the key
 * @param pem PEM formatted key string/*from   ww w . j a v a2 s . c  o m*/
 * @return
 */
public static RSAPrivateKey getRSAPrivateKeyFromPEM(Provider provider, String pem) {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance("RSA", provider);
        return (RSAPrivateKey) keyFactory.generatePrivate(new PKCS8EncodedKeySpec(getKeyBytesFromPEM(pem)));
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException("Algorithm SHA256withRSA is not available", e);
    } catch (InvalidKeySpecException e) {
        throw new IllegalArgumentException("Invalid PEM provided", e);
    }
}

From source file:com.cloudbees.tftwoway.Client.java

private static PrivateKey loadRSAKey(String path) throws Exception {
    try (InputStream fis = new FileInputStream(path)) {
        try (PemReader pemReader = new PemReader(new InputStreamReader(fis))) {
            byte[] pemBytes = pemReader.readPemObject().getContent();

            KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
            KeySpec spec = new PKCS8EncodedKeySpec(pemBytes);

            return keyFactory.generatePrivate(spec);
        }//from  ww w  .  ja v  a2  s  .c  o  m
    }
}

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

private PrivateKey getPrivateKeyFromString(String key) throws Exception {

    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(key));

    PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);

    return privateKey;

}

From source file:org.soulwing.credo.service.crypto.bc.BcPrivateKeyWrapper.java

/**
 * {@inheritDoc}/*ww w. j a  v  a 2 s  .  co  m*/
 */
@Override
public PrivateKey derive() {
    try {
        PrivateKeyInfo keyInfo = derivePrivateKeyInfo();
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyInfo.getEncoded());
        KeyFactory kf = KeyFactory.getInstance("RSA");
        return kf.generatePrivate(keySpec);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException(ex);
    } catch (InvalidKeySpecException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.zxy.commons.codec.rsa.RSAUtils.java

/**
 * <P>/*from   www  . j  a  va2s .  c o m*/
 * ?
 * </p>
 * 
 * @param encryptedData ?
 * @param privateKey ?(BASE64?)
 * @return byte
 * @throws Exception Exception
 */
@SuppressWarnings({ "PMD.ShortVariable", "PMD.AvoidDuplicateLiterals" })
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey) throws Exception {
    byte[] keyBytes = Base64.decodeBase64(privateKey);
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    Key privateK = keyFactory.generatePrivate(pkcs8KeySpec);
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
    cipher.init(Cipher.DECRYPT_MODE, privateK);
    int inputLen = encryptedData.length;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int offSet = 0;
    byte[] cache;
    int i = 0;
    // ?
    while (inputLen - offSet > 0) {
        if (inputLen - offSet > MAX_DECRYPT_BLOCK) {
            cache = cipher.doFinal(encryptedData, offSet, MAX_DECRYPT_BLOCK);
        } else {
            cache = cipher.doFinal(encryptedData, offSet, inputLen - offSet);
        }
        out.write(cache, 0, cache.length);
        i++;
        offSet = i * MAX_DECRYPT_BLOCK;
    }
    byte[] decryptedData = out.toByteArray();
    out.close();
    return decryptedData;
}

From source file:com.cedarsoft.crypt.CertTest.java

@Test
public void testKey() throws Exception {
    InputStream inStream = new DataInputStream(getClass().getResource("/test.der").openStream());
    byte[] keyBytes = new byte[inStream.available()];
    inStream.read(keyBytes);/*from w  w  w.j  a  v a 2  s . co m*/
    inStream.close();

    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    // decipher private key
    PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(keyBytes);
    RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec);
    assertNotNull(privKey);

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.ENCRYPT_MODE, privKey);

    byte[] bytes = cipher.doFinal(PLAINTEXT.getBytes());
    assertEquals(SCRAMBLED, new String(Base64.encodeBase64(bytes)));
}

From source file:org.tolven.session.ShiroSessionWrapper.java

@Override
public PrivateKey getUserPrivateKey(String keyAlgorithm) {
    byte[] userPKCS8EncodedKeyBytes = (byte[]) getAttribute("userPKCS8EncodedKey");
    if (userPKCS8EncodedKeyBytes == null) {
        return null;
    } else {//from  ww w .  j  ava 2 s. c o  m
        try {
            PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(userPKCS8EncodedKeyBytes);
            return getKeyFactory(keyAlgorithm).generatePrivate(pkcs8EncodedKeySpec);
        } catch (Exception ex) {
            throw new RuntimeException("Could not get PrivateKey from pkcs8EncodedKey", ex);
        }
    }
}

From source file:com.security.ch08_rsa.RSACoderTextKey.java

/**
 * ?/*from  w ww .  ja va 2 s. com*/
 *
 * @param data
 *            ?
 * @param key
 *            ?
 * @return byte[] ?
 * @throws Exception
 */
private static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {

    // ??
    PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);

    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

    // ??
    PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);

    // ?
    Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());

    cipher.init(Cipher.ENCRYPT_MODE, privateKey);

    return cipher.doFinal(data);
}

From source file:com.znsx.util.licence.LicenceUtil.java

/**
 * ?????//ww  w . ja  va  2 s.c om
 * 
 * @param data
 *            ??
 * @param privateKey
 *            ???base64?
 * @return base64????
 * @throws Exception
 */
public static String sign(String data, String privateKeyString) throws Exception {
    Base64 base64 = new Base64();
    // ????
    // BASE64Decoder decoder = new BASE64Decoder();
    // byte[] bytes = decoder.decodeBuffer(privateKeyString);
    byte[] bytes = base64.decode(privateKeyString.getBytes("utf8"));
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
    PrivateKey privateKey = KeyFactory.getInstance("DSA").generatePrivate(keySpec);
    // ???
    Signature signature = Signature.getInstance("DSA");
    signature.initSign(privateKey);
    signature.update(data.getBytes("utf8"));
    // return new BASE64Encoder().encode(signature.sign());
    return new String(base64.encode(signature.sign()), "utf8");
}

From source file:com.vmware.identity.rest.idm.data.PrivateKeyDTO.java

private static PrivateKey decodePrivateKey(String encoded, String algorithm)
        throws InvalidKeySpecException, NoSuchAlgorithmException {
    if (encoded == null) {
        return null;
    }/*from  w  w  w.  j av a  2 s.  c o  m*/

    byte[] clear = Base64.decodeBase64(encoded);
    PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);
    KeyFactory fact = KeyFactory.getInstance(algorithm);
    PrivateKey privateKey = fact.generatePrivate(keySpec);
    Arrays.fill(clear, (byte) 0);
    return privateKey;
}