Example usage for java.security InvalidAlgorithmParameterException getMessage

List of usage examples for java.security InvalidAlgorithmParameterException getMessage

Introduction

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

Prototype

public String getMessage() 

Source Link

Document

Returns the detail message string of this throwable.

Usage

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/*  www  .  ja v  a  2s.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 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.trsst.Common.java

static final KeyPair generateSigningKeyPair() {
    try {/*ww w  .j a  v  a2 s .  co m*/
        KeyPairGenerator kpg;
        // kpg = KeyPairGenerator.getInstance("EC", "BC");
        kpg = new org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi.EC();
        kpg.initialize(new ECGenParameterSpec(CURVE_NAME));
        KeyPair kp = kpg.generateKeyPair();
        return kp;
        // } catch (NoSuchAlgorithmException e) {
        // log.error("Error while generating key: " + e.getMessage(), e);
        // } catch (NoSuchProviderException e) {
        // log.error("Error while generating key: " + e.getMessage(), e);
    } catch (InvalidAlgorithmParameterException e) {
        log.error("Error while generating key: " + e.getMessage(), e);
    }
    return null;
}

From source file:be.apsu.extremon.probes.ocsp.OCSPProbe.java

public OCSPProbe() {
    CertificateFactory certificateFactory = null;

    try {/*from  www.ja va  2 s.c  o  m*/
        certificateFactory = CertificateFactory.getInstance("X.509");
    } catch (CertificateException cex) {
        log("Don't Have Crypto Libs:" + cex.getMessage());
        System.exit(1);
    }

    try {
        certificate = (X509Certificate) certificateFactory
                .generateCertificate(new ByteArrayInputStream(Base64.decodeBase64(confStr("certificate"))));
        trustAnchorCert = (X509Certificate) certificateFactory
                .generateCertificate(new ByteArrayInputStream(Base64.decodeBase64(confStr("trustanchor"))));
    } catch (CertificateException cex) {
        log("certificate and trustanchor required in config:" + cex.getMessage());
        System.exit(2);
    }

    this.delay = confInt("delay", DEFAULT_DELAY);

    try {
        List<X509Certificate> certs = new ArrayList<X509Certificate>();
        certs.add(this.certificate);
        this.certificatePath = (CertPath) certificateFactory.generateCertPath(certs);

        TrustAnchor trustAnchor = new TrustAnchor(this.trustAnchorCert, null);
        Set<TrustAnchor> trustedCertsSet = new HashSet<TrustAnchor>();
        trustedCertsSet.add(trustAnchor);

        Set<X509Certificate> certSet = new HashSet<X509Certificate>();
        certSet.add(this.trustAnchorCert);
        CertStoreParameters storeParams = new CollectionCertStoreParameters(certSet);
        CertStore store = CertStore.getInstance("Collection", storeParams);

        pkixParams = new PKIXParameters(trustedCertsSet);
        pkixParams.addCertStore(store);

        Security.setProperty("ocsp.enable", "true");
        Security.setProperty("ocsp.responderURL", confStr("url"));
        Security.setProperty("ocsp.responderCertSubjectName",
                this.trustAnchorCert.getSubjectX500Principal().getName());

        this.certificatePathValidator = CertPathValidator.getInstance("PKIX");
    } catch (InvalidAlgorithmParameterException iaex) {
        log("Invalid Algorithm Parameter:" + iaex.getMessage());
        System.exit(3);
    } catch (CertificateException cex) {
        log("Certificate Exception:" + cex.getMessage());
        System.exit(4);
    } catch (NoSuchAlgorithmException nsaex) {
        log("No Such Algorithm:" + nsaex.getMessage());
        System.exit(5);
    } catch (Exception ex) {
        log(ex.getMessage());
        System.exit(6);
    }

    start();
    log("Initialized");
}

From source file:org.ejbca.util.keystore.KeyStoreContainerBase.java

@Override
public byte[] generate(final AlgorithmParameterSpec spec, final String keyEntryName) throws Exception {
    if (log.isTraceEnabled()) {
        log.trace(">generate from AlgorithmParameterSpec: " + spec.getClass().getName());
    }/* ww w. jav a  2 s  .  c o  m*/
    // Generate the Keypair
    String algorithm = "EC";
    String sigAlg = "SHA1withECDSA";
    String specName = spec.getClass().getName();
    if (specName.contains("DSA")) {
        algorithm = "DSA";
        sigAlg = "SHA1withDSA";
    } else if (specName.contains("RSA")) {
        algorithm = "RSA";
        sigAlg = "SHA1withRSA";
    }
    final KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm, this.providerName);
    try {
        kpg.initialize(spec);
    } catch (InvalidAlgorithmParameterException e) {
        log.debug("Algorithm parameters not supported: " + e.getMessage());
        throw e;
    }
    final byte result[] = generate(kpg, keyEntryName, sigAlg);
    if (log.isTraceEnabled()) {
        log.trace("<generate from AlgorithmParameterSpec: " + spec.getClass().getName());
    }
    return result;
}

From source file:tds.itemrenderer.security.Encryption.java

private synchronized String decrypt(final String stringToDecrypt) {
    try {/*from   w w  w.j ava  2s  .  c om*/
        if (StringUtils.isEmpty(stringToDecrypt)) {
            return "";
        }

        byte[] encryptedBytes = DatatypeConverter.parseBase64Binary(stringToDecrypt);

        byte[] iv = new byte[IV_LENGTH];
        System.arraycopy(encryptedBytes, 0, iv, 0, iv.length);
        decryptCipher.init(Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv));
        byte[] cipherBytes = new byte[encryptedBytes.length - IV_LENGTH];
        System.arraycopy(encryptedBytes, IV_LENGTH, cipherBytes, 0, cipherBytes.length);
        byte[] decryptedBytes = decryptCipher.doFinal(cipherBytes);
        return new String(decryptedBytes, UTF_8);
    } catch (InvalidAlgorithmParameterException e) {
        log.error("Encyption.decrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Algorithm is not valid");
    } catch (InvalidKeyException e) {
        log.error("Encyption.decrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Key is not valid");
    } catch (IllegalBlockSizeException e) {
        log.error("Encyption.decrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Block size is not valid");
    } catch (BadPaddingException e) {
        log.error("Encyption.decrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Padding is not valid");
    }
}

From source file:TDS.Shared.Web.Encryption.java

/**
 * decrypts a string/*from   w ww .java  2s. c om*/
 * 
 * @param stringToDecrypt
 * @return decrypted string
 * @throws TDSEncryptionException
 */
private synchronized String decrypt(String stringToDecrypt) {
    try {

        if (StringUtils.isEmpty(stringToDecrypt)) {
            return "";
        }

        byte[] encryptedBytes = DatatypeConverter.parseBase64Binary(stringToDecrypt);

        byte[] iv = new byte[IV_LENGTH];
        System.arraycopy(encryptedBytes, 0, iv, 0, iv.length);
        _decryptCipher.init(Cipher.DECRYPT_MODE, _secretKey, new IvParameterSpec(iv));
        byte[] cipherBytes = new byte[encryptedBytes.length - IV_LENGTH];
        System.arraycopy(encryptedBytes, IV_LENGTH, cipherBytes, 0, cipherBytes.length);
        byte[] decryptedBytes = _decryptCipher.doFinal(cipherBytes);
        return new String(decryptedBytes, CHARSET_NAME);
    } catch (InvalidAlgorithmParameterException e) {
        _logger.error("Encyption.decrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Algorithm is not valid");
    } catch (InvalidKeyException e) {
        _logger.error("Encyption.decrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Key is not valid");
    } catch (IllegalBlockSizeException e) {
        _logger.error("Encyption.decrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Block size is not valid");
    } catch (BadPaddingException e) {
        _logger.error("Encyption.decrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Padding is not valid");
    } catch (UnsupportedEncodingException e) {
        _logger.error("Encyption.decrypt: " + e.getMessage(), e);
        throw new TDSEncryptionException("Encoding is not valid");
    }

}