Java 8 PKCS8EncodedKeySpec - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 PKCS8EncodedKeySpec class.

This code example shows how to use the following methods:sleep
        return getCurrentHash(content);
    } 
 
    private PrivateKey createKey() throws Exception {
        byte[] derKey = Base64.decode(TEST_KEY.getBytes(), Base64.DEFAULT);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(derKey);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (PrivateKey) keyFactory.generatePrivate(keySpec);
    } 
 
    private X509Certificate createCertificate() throws Exception {
        byte[] derCert = Base64.decode(TEST_CERT.getBytes(), Base64.DEFAULT);
        InputStream istream = new ByteArrayInputStream(derCert);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        return (X509Certificate) cf.generateCertificate(istream);
    } 
 
    private String makeTemporaryContentFile(String content) throws Exception {
        FileOutputStream fw = mContext.openFileOutput("content.txt", mContext.MODE_WORLD_READABLE);
        fw.write(content.getBytes(), 0, content.length());

7
CodeRank
This code example shows how to use the following methods:
    if (key == null) {
      throw new RuntimeException("Missing private key path");
    } 
    byte[] value = loadPem(key, "PRIVATE KEY");
    KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA");
    return rsaKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(value));
  } 
 
  private static Certificate[] loadCert(Buffer cert) throws Exception {
    if (cert == null) {
      throw new RuntimeException("Missing X.509 certificate path");
    } 
    byte[] value = loadPem(cert, "CERTIFICATE");
    CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
    return certFactory.generateCertificates(new ByteArrayInputStream(value)).toArray(new Certificate[0]);
  } 
} 
7
CodeRank
This code example shows how to use the following methods:getEncoded
        } else if (keySpec instanceof PKCS8EncodedKeySpec) {
            PKCS8EncodedKeySpec pkcs8KeySpec = (PKCS8EncodedKeySpec) keySpec;
 
            try { 
                final OpenSSLKey key = new OpenSSLKey(
                        NativeCrypto.d2i_PKCS8_PRIV_KEY_INFO(pkcs8KeySpec.getEncoded()));
                return OpenSSLRSAPrivateKey.getInstance(key);
            } catch (Exception e) {
                throw new InvalidKeySpecException(e);
            } 
        } 
        throw new InvalidKeySpecException("Must use RSAPublicKeySpec or PKCS8EncodedKeySpec; was "
                + keySpec.getClass().getName());
    } 
 
    @Override 
    protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec)
            throws InvalidKeySpecException { 
        if (key == null) {
            throw new InvalidKeySpecException("key == null");
6
CodeRank
This code example shows how to use the following methods:sleep
7
CodeRank
This code example shows how to use the following methods:
     */ 
    public static PrivateKey privateKeyFromPKCS8(byte[] pkcs8) 
            throws InvalidKeySpecException 
    { 
        try { 
            EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(pkcs8);
            KeyFactory keyFactory = KeyFactory.getInstance(RSA);
            PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
            return privateKey;
        } catch (NoSuchAlgorithmException e) {
            throw new IllegalStateException(e);
        } 
    } 
 
    /** 
     * Returns a private key constructed from the given DER bytes in PKCS#1 format. 
     */ 
    public static PrivateKey privateKeyFromPKCS1(byte[] pkcs1)
            throws InvalidKeySpecException
6
CodeRank
This code example shows how to use the following methods:
This code example shows how to use the following methods:sleep
        return getCurrentHash(content);
    } 
 
    private PrivateKey createKey() throws Exception {
        byte[] derKey = Base64.decode(TEST_KEY.getBytes(), Base64.DEFAULT);
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(derKey);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return (PrivateKey) keyFactory.generatePrivate(keySpec);
    } 
 
    private X509Certificate createCertificate() throws Exception {
        byte[] derCert = Base64.decode(TEST_CERT.getBytes(), Base64.DEFAULT);
        InputStream istream = new ByteArrayInputStream(derCert);
        CertificateFactory cf = CertificateFactory.getInstance("X.509");
        return (X509Certificate) cf.generateCertificate(istream);
    } 
 
    private String makeTemporaryContentFile(String content) throws Exception {
        FileOutputStream fw = mContext.openFileOutput("content.txt", mContext.MODE_WORLD_READABLE);
        fw.write(content.getBytes(), 0, content.length());

6
CodeRank
This code example shows how to use the following methods:
      return null; 
    } 
 
    try { 
      KeyFactory fac = KeyFactory.getInstance("RSA");
      EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKey);
      return fac.generatePrivate(spec);
    } 
    catch (NoSuchAlgorithmException e) {
      throw new IllegalStateException(e);
    } 
    catch (InvalidKeySpecException e) {
      throw new IllegalStateException(e);
    } 
  } 
 
  /** 
   * Utility method for decoding a base-64-encoded string. 
   * 
   * @param value The base-64-encoded string. 
6
CodeRank
This code example shows how to use the following methods:
        keys.put("DH_public", new X509EncodedKeySpec(DH_public));
        keys.put("DH_private", new PKCS8EncodedKeySpec(DH_private));
        keys.put("DSA_public", new X509EncodedKeySpec(DSA_public));
        keys.put("DSA_private", new PKCS8EncodedKeySpec(DSA_private));
        keys.put("RSA_public", new X509EncodedKeySpec(RSA_public));
        keys.put("RSA_private", new PKCS8EncodedKeySpec(RSA_private));
    } 
 
    public static PrivateKey getPrivateKey(String algorithmName) throws NoSuchAlgorithmException, InvalidKeySpecException
    { 
        KeyFactory factory = KeyFactory.getInstance(algorithmName);
        return factory.generatePrivate(keys.get(algorithmName + "_private"));
    } 
 
    public static PublicKey getPublicKey(String algorithmName) throws NoSuchAlgorithmException, InvalidKeySpecException
    { 
        KeyFactory factory = KeyFactory.getInstance(algorithmName);
        return factory.generatePublic(keys.get(algorithmName + "_public"));
    } 
} 
6
CodeRank
This code example shows how to use the following methods:
    Provider provider = (Provider)providerClass.newInstance();
    Security.insertProviderAt(provider, 1);
    try { 
        // Load the private key 
        PKCS8EncodedKeySpec privateKeySpec
        = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory
        = KeyFactory.getInstance("RSA");
        PrivateKey privateKey
        = keyFactory.generatePrivate(privateKeySpec);
         
        System.out.println("Loaded " + privateKey.getAlgorithm()
                   + " " + privateKey.getFormat()
                   + " private key."); 
        return privateKey;
    } finally { 
        // Dynamically deinstall the RSA provider  
        Security.removeProvider(provider.getName());
    } 
    } 
6
CodeRank

Related Java 8 PKCS8EncodedKeySpec Questions & Answers:

See Code Examples for Java 8 PKCS8EncodedKeySpec Methods: