Example usage for java.security.spec InvalidKeySpecException InvalidKeySpecException

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

Introduction

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

Prototype

public InvalidKeySpecException(Throwable cause) 

Source Link

Document

Creates an InvalidKeySpecException with the specified cause and a detail message of (cause==null ?

Usage

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

@SuppressWarnings("unchecked")
public static PrivateKey getPrivateKey(AutorizacionType auth, char[] password)
        throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException,
        InvalidAlgorithmParameterException, IOException {

    List<PEMItem> items = PEMUtil.decode(auth.getRSASK().getBytes());

    for (PEMItem item : items) {
        if ("RSA PRIVATE KEY".equals(item.pemType)) {
            try {
                PKCS8Key pkcs8 = new PKCS8Key(item.getDerBytes(), password);

                return Utilities.readPrivateKey(pkcs8.getDecryptedBytes(), "RSA", password);
            } catch (GeneralSecurityException e) {
                throw new InvalidKeySpecException(e);
            }/*  w w w  . jav  a2s .c om*/
        }
    }

    return null;
}

From source file:com.google.gerrit.sshd.SshUtil.java

/**
 * Parse a public key into its Java type.
 *
 * @param key the account key to parse./*from   ww w.  j av  a 2 s  .  co m*/
 * @return the valid public key object.
 * @throws InvalidKeySpecException the key supplied is not a valid SSH key.
 * @throws NoSuchAlgorithmException the JVM is missing the key algorithm.
 * @throws NoSuchProviderException the JVM is missing the provider.
 */
public static PublicKey parse(final AccountSshKey key)
        throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    try {
        final String s = key.getEncodedKey();
        if (s == null) {
            throw new InvalidKeySpecException("No key string");
        }
        final byte[] bin = Base64.decodeBase64(Constants.encodeASCII(s));
        return new Buffer(bin).getRawPublicKey();
    } catch (RuntimeException re) {
        throw new InvalidKeySpecException("Cannot parse key", re);
    } catch (SshException e) {
        throw new InvalidKeySpecException("Cannot parse key", e);
    }
}

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);
            }/* w w  w.ja  v a  2s .  com*/
        }
    }
    return null;
}

From source file:com.zacwolf.commons.crypto.Crypter_AES.java

/**
 * @param keyfile//from  www.j  av a  2 s  .c o  m
 * @param keysize
 * @param salter
 * @throws InvalidKeySpecException
 * @throws IOException
 * @throws NoSuchAlgorithmException
 */
public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter)
        throws InvalidKeySpecException, IOException, NoSuchAlgorithmException {
    if (keysize > Cipher.getMaxAllowedKeyLength(mytype))
        throw new InvalidKeySpecException(
                "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of "
                        + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype);

    final FileOutputStream fos = new FileOutputStream(keyfile);
    try {
        final KeyGenerator kgen = KeyGenerator.getInstance(mytype);
        kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available
        final SecretKey sk = kgen.generateKey();
        fos.write(sk.getEncoded());
    } finally {
        fos.flush();
        fos.close();
        ;
    }
}

From source file:com.zacwolf.commons.crypto.Crypter_Blowfish.java

/**
 * /*from  ww w.ja  v  a2s.c om*/
 * Create a new key with a custom keysize less-than equal to mykeysizemax,
 * specifying a custom salter
 * 
 * @param keyfile
 * @param keysize
 * @param salter
 * @throws NoSuchAlgorithmException
 * @throws IOException
 * @throws InvalidKeySpecException
 */
public final static void generateNewKeyFile(final File keyfile, final int keysize, final SecureRandom salter)
        throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
    if (keysize > Cipher.getMaxAllowedKeyLength(mytype))
        throw new InvalidKeySpecException(
                "You specified a key size not supported by your current cryptographic libraries, which currently have a max key size of "
                        + Cipher.getMaxAllowedKeyLength(mytype) + " for " + mytype);

    final FileOutputStream fos = new FileOutputStream(keyfile);
    try {
        final KeyGenerator kgen = KeyGenerator.getInstance(mytype);
        kgen.init(keysize > mykeysizemax ? mykeysizemax : keysize, salter); // 192 and 256 bits may not be available
        final SecretKey sk = kgen.generateKey();
        fos.write(sk.getEncoded());
    } finally {
        if (fos != null) {
            fos.flush();
            fos.close();
        }
    }
}