Example usage for java.security.spec InvalidKeySpecException getMessage

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

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

From source file:com.clustercontrol.util.KeyCheck.java

/**
 * ?/*from ww w .j  ava  2 s. c  om*/
 * com.clustercontrol.key.KeyGenerator????????public??
 * @param str
 * @return
 * @throws HinemosUnknown
 */
public static PublicKey getPublicKey(String str) throws HinemosUnknown {
    try {
        X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(string2Byte(str));
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        return keyFactory.generatePublic(publicKeySpec);
    } catch (InvalidKeySpecException e) {
        throw new HinemosUnknown("getPublicKey fail " + e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new HinemosUnknown("getPublicKey fail " + e.getMessage(), e);
    }
}

From source file:com.clustercontrol.util.KeyCheck.java

/**
 * ?/*from w  ww  .j  av  a  2 s . com*/
 * com.clustercontrol.key.KeyGenerator????????public??
 * @param str
 * @return
 * @throws HinemosUnknown
 */
public static PrivateKey getPrivateKey(String str) throws HinemosUnknown {
    try {
        KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
        PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(string2Byte(str));
        return keyFactory.generatePrivate(privateKeySpec);
    } catch (InvalidKeySpecException e) {
        throw new HinemosUnknown("getPrivateKey fail " + e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new HinemosUnknown("getPrivateKey fail " + e.getMessage(), e);
    }
}

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

public static String savePublicKey(PublicKey key) {
    try {//www . j  a va  2 s .  c  om
        KeyFactory keyFactory = SAMLUtils.getKeyFactory();
        if (keyFactory == null)
            return null;
        X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
        return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()));
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create KeyFactory:" + e.getMessage());
    }
    return null;
}

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

public static String savePrivateKey(PrivateKey key) {
    try {/*from  ww  w .  j  a  va2s  .  co  m*/
        KeyFactory keyFactory = SAMLUtils.getKeyFactory();
        if (keyFactory == null)
            return null;
        PKCS8EncodedKeySpec spec = keyFactory.getKeySpec(key, PKCS8EncodedKeySpec.class);
        return new String(org.bouncycastle.util.encoders.Base64.encode(spec.getEncoded()));
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create KeyFactory:" + e.getMessage());
    }
    return null;
}

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

public static PublicKey loadPublicKey(String publicKey) {
    byte[] sigBytes = org.bouncycastle.util.encoders.Base64.decode(publicKey);
    X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(sigBytes);
    KeyFactory keyFact = SAMLUtils.getKeyFactory();
    if (keyFact == null)
        return null;
    try {/*from  w  w  w. j  a va2s .  c om*/
        return keyFact.generatePublic(x509KeySpec);
    } catch (InvalidKeySpecException e) {
        s_logger.error("Unable to create PrivateKey from privateKey string:" + e.getMessage());
    }
    return null;
}

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 {/*from  ww w .j  av  a  2s.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.cws.esolutions.security.utils.PasswordUtils.java

/**
 * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility
 * is required but encryption (obfuscation, technically) is required.
 *
 * @param value - The plain text data to encrypt
 * @param salt - The salt value to utilize for the request
 * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory
 * @param iterations - The number of times to loop through the keyspec
 * @param keyBits - The size of the key, in bits
 * @param algorithm - The algorithm to encrypt the data with
 * @param cipherInstance - The cipher instance to utilize
 * @param encoding - The text encoding/*w  w  w  .  ja va 2 s.  c  om*/
 * @return The encrypted string in a reversible format
 * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing
 */
public static final String decryptText(final String value, final String salt, final String secretInstance,
        final int iterations, final int keyBits, final String algorithm, final String cipherInstance,
        final String encoding) throws SecurityException {
    final String methodName = PasswordUtils.CNAME
            + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", secretInstance);
        DEBUGGER.debug("Value: {}", iterations);
        DEBUGGER.debug("Value: {}", keyBits);
        DEBUGGER.debug("Value: {}", algorithm);
        DEBUGGER.debug("Value: {}", cipherInstance);
        DEBUGGER.debug("Value: {}", encoding);
    }

    String decPass = null;

    try {
        String decoded = new String(Base64.getDecoder().decode(value));
        String iv = decoded.split(":")[0];
        String property = decoded.split(":")[1];

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance);
        PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits);
        SecretKey keyTmp = keyFactory.generateSecret(keySpec);
        SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm);

        Cipher pbeCipher = Cipher.getInstance(cipherInstance);
        pbeCipher.init(Cipher.DECRYPT_MODE, sks, new IvParameterSpec(Base64.getDecoder().decode(iv)));
        decPass = new String(pbeCipher.doFinal(Base64.getDecoder().decode(property)), encoding);
    } catch (InvalidKeyException ikx) {
        throw new SecurityException(ikx.getMessage(), ikx);
    } catch (NoSuchAlgorithmException nsx) {
        throw new SecurityException(nsx.getMessage(), nsx);
    } catch (NoSuchPaddingException npx) {
        throw new SecurityException(npx.getMessage(), npx);
    } catch (IllegalBlockSizeException ibx) {
        throw new SecurityException(ibx.getMessage(), ibx);
    } catch (BadPaddingException bpx) {
        throw new SecurityException(bpx.getMessage(), bpx);
    } catch (UnsupportedEncodingException uex) {
        throw new SecurityException(uex.getMessage(), uex);
    } catch (InvalidAlgorithmParameterException iapx) {
        throw new SecurityException(iapx.getMessage(), iapx);
    } catch (InvalidKeySpecException iksx) {
        throw new SecurityException(iksx.getMessage(), iksx);
    }

    return decPass;
}

From source file:com.cws.esolutions.security.utils.PasswordUtils.java

/**
 * Provides two-way (reversible) encryption of a provided string. Can be used where reversibility
 * is required but encryption (obfuscation, technically) is required.
 *
 * @param value - The plain text data to encrypt
 * @param salt - The salt value to utilize for the request
 * @param secretInstance - The cryptographic instance to use for the SecretKeyFactory
 * @param iterations - The number of times to loop through the keyspec
 * @param keyBits - The size of the key, in bits
 * @param algorithm - The algorithm to encrypt the data with
 * @param cipherInstance - The cipher instance to utilize
 * @param encoding - The text encoding/*ww w  .j  a  v a  2 s . c  o  m*/
 * @return The encrypted string in a reversible format
 * @throws SecurityException {@link java.lang.SecurityException} if an exception occurs during processing
 */
public static final String encryptText(final String value, final String salt, final String secretInstance,
        final int iterations, final int keyBits, final String algorithm, final String cipherInstance,
        final String encoding) throws SecurityException {
    final String methodName = PasswordUtils.CNAME
            + "#encryptText(final String value, final String salt, final String secretInstance, final int iterations, final int keyBits, final String algorithm, final String cipherInstance, final String encoding) throws SecurityException";

    if (DEBUG) {
        DEBUGGER.debug(methodName);
        DEBUGGER.debug("Value: {}", secretInstance);
        DEBUGGER.debug("Value: {}", iterations);
        DEBUGGER.debug("Value: {}", keyBits);
        DEBUGGER.debug("Value: {}", algorithm);
        DEBUGGER.debug("Value: {}", cipherInstance);
        DEBUGGER.debug("Value: {}", encoding);
    }

    String encPass = null;

    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(secretInstance);
        PBEKeySpec keySpec = new PBEKeySpec(salt.toCharArray(), salt.getBytes(), iterations, keyBits);
        SecretKey keyTmp = keyFactory.generateSecret(keySpec);
        SecretKeySpec sks = new SecretKeySpec(keyTmp.getEncoded(), algorithm);

        Cipher pbeCipher = Cipher.getInstance(cipherInstance);
        pbeCipher.init(Cipher.ENCRYPT_MODE, sks);

        AlgorithmParameters parameters = pbeCipher.getParameters();
        IvParameterSpec ivParameterSpec = parameters.getParameterSpec(IvParameterSpec.class);

        byte[] cryptoText = pbeCipher.doFinal(value.getBytes(encoding));
        byte[] iv = ivParameterSpec.getIV();

        String combined = Base64.getEncoder().encodeToString(iv) + ":"
                + Base64.getEncoder().encodeToString(cryptoText);

        encPass = Base64.getEncoder().encodeToString(combined.getBytes());
    } catch (InvalidKeyException ikx) {
        throw new SecurityException(ikx.getMessage(), ikx);
    } catch (NoSuchAlgorithmException nsx) {
        throw new SecurityException(nsx.getMessage(), nsx);
    } catch (NoSuchPaddingException npx) {
        throw new SecurityException(npx.getMessage(), npx);
    } catch (IllegalBlockSizeException ibx) {
        throw new SecurityException(ibx.getMessage(), ibx);
    } catch (BadPaddingException bpx) {
        throw new SecurityException(bpx.getMessage(), bpx);
    } catch (UnsupportedEncodingException uex) {
        throw new SecurityException(uex.getMessage(), uex);
    } catch (InvalidKeySpecException iksx) {
        throw new SecurityException(iksx.getMessage(), iksx);
    } catch (InvalidParameterSpecException ipsx) {
        throw new SecurityException(ipsx.getMessage(), ipsx);
    }

    return encPass;
}

From source file:org.jboss.aerogear.crypto.android.demo.CryptoActivity.java

public void login(String passphrase) {
    try {/*from  w w  w. j a v a  2  s  . c  om*/
        application.createStore(passphrase);
        displayList();
    } catch (InvalidKeySpecException e) {
        displayLogin();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}

From source file:com.tasktop.c2c.server.internal.profile.crypto.OpenSSHPublicKeyReader.java

public SshPublicKey readPublicKey(String keySpec) {
    keySpec = keySpec.trim();//from  www  . ja v  a2s. co m
    String[] parts = keySpec.split(" ");
    if (parts.length >= 2) {
        String algorithm = parts[0];
        String base64Data = parts[1];
        if (algorithm.equals("ssh-rsa")) {
            SshPublicKey sshPublicKey = new SshPublicKey();
            sshPublicKey.setAlgorithm("RSA");
            byte[] decodedData = Base64.decodeBase64(StringUtils.getBytesUtf8(base64Data));

            Rfc4253Reader reader = new Rfc4253Reader(decodedData, 0);

            try {
                byte[] format = reader.readBytes();
                byte[] exponent = reader.readBytes();
                byte[] modulus = reader.readBytes();

                if (Arrays.equals(FORMAT, format)) {
                    BigInteger exp = new BigInteger(exponent);
                    BigInteger mod = new BigInteger(modulus);
                    RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(mod, exp);
                    try {
                        PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(rsaPublicKeySpec);
                        sshPublicKey.setKeyData(publicKey.getEncoded());
                        return sshPublicKey;
                    } catch (InvalidKeySpecException t) {
                        getLogger().warn("Invalid key spec: " + t.getMessage(), t);
                    } catch (NoSuchAlgorithmException t) {
                        getLogger().warn("Invalid algorithm: " + t.getMessage(), t);
                    }
                }

            } catch (IOException e) {
                // ignore
            }
        }
    }
    return null;
}