Search in sources :

Example 46 with NoSuchProviderException

use of java.security.NoSuchProviderException in project jdk8u_jdk by JetBrains.

the class TestAESCipher method runTest.

public void runTest(String algo, String mo, String pad) throws Exception {
    Cipher ci = null;
    byte[] iv = null;
    AlgorithmParameterSpec aps = null;
    SecretKey key = null;
    try {
        // Initialization
        Random rdm = new Random();
        byte[] plainText = new byte[128];
        rdm.nextBytes(plainText);
        ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
        KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
        kg.init(KEY_LENGTH);
        key = kg.generateKey();
        // encrypt
        if (!mo.equalsIgnoreCase("GCM")) {
            ci.init(Cipher.ENCRYPT_MODE, key, aps);
        } else {
            ci.init(Cipher.ENCRYPT_MODE, key);
        }
        byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
        int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
        ci.doFinal(cipherText, offset);
        if (!mo.equalsIgnoreCase("ECB")) {
            iv = ci.getIV();
            aps = new IvParameterSpec(iv);
        } else {
            aps = null;
        }
        if (!mo.equalsIgnoreCase("GCM")) {
            ci.init(Cipher.DECRYPT_MODE, key, aps);
        } else {
            ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
        }
        byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
        int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
        byte[] tmp = new byte[len];
        System.arraycopy(recoveredText, 0, tmp, 0, len);
        // Comparison
        if (!java.util.Arrays.equals(plainText, tmp)) {
            System.out.println("Original: ");
            dumpBytes(plainText);
            System.out.println("Recovered: ");
            dumpBytes(tmp);
            throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
        }
    } catch (NoSuchAlgorithmException e) {
        //CFB7 and OFB150 are for negative testing
        if (!mo.equalsIgnoreCase("CFB7") && !mo.equalsIgnoreCase("OFB150")) {
            System.out.println("Unexpected NoSuchAlgorithmException with mode: " + mo);
            throw new RuntimeException("Test failed!");
        }
    } catch (NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
        System.out.println("Test failed!");
        throw e;
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) SecretKey(javax.crypto.SecretKey) Random(java.util.Random) ShortBufferException(javax.crypto.ShortBufferException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) KeyGenerator(javax.crypto.KeyGenerator)

Example 47 with NoSuchProviderException

use of java.security.NoSuchProviderException in project jdk8u_jdk by JetBrains.

the class MessageDigestAlgorithm method getDigestInstance.

private static MessageDigest getDigestInstance(String algorithmURI) throws XMLSignatureException {
    String algorithmID = JCEMapper.translateURItoJCEID(algorithmURI);
    if (algorithmID == null) {
        Object[] exArgs = { algorithmURI };
        throw new XMLSignatureException("algorithms.NoSuchMap", exArgs);
    }
    MessageDigest md;
    String provider = JCEMapper.getProviderId();
    try {
        if (provider == null) {
            md = MessageDigest.getInstance(algorithmID);
        } else {
            md = MessageDigest.getInstance(algorithmID, provider);
        }
    } catch (java.security.NoSuchAlgorithmException ex) {
        Object[] exArgs = { algorithmID, ex.getLocalizedMessage() };
        throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
    } catch (NoSuchProviderException ex) {
        Object[] exArgs = { algorithmID, ex.getLocalizedMessage() };
        throw new XMLSignatureException("algorithms.NoSuchAlgorithm", exArgs);
    }
    return md;
}
Also used : MessageDigest(java.security.MessageDigest) NoSuchProviderException(java.security.NoSuchProviderException) XMLSignatureException(com.sun.org.apache.xml.internal.security.signature.XMLSignatureException)

Example 48 with NoSuchProviderException

use of java.security.NoSuchProviderException in project bitsquare by bitsquare.

the class Hash method getHash.

/**
     * @param data Data as byte array
     * @return Hash of data
     */
public static byte[] getHash(byte[] data) {
    try {
        MessageDigest digest = MessageDigest.getInstance("SHA-256", "BC");
        digest.update(data, 0, data.length);
        return digest.digest();
    } catch (NoSuchAlgorithmException | NoSuchProviderException e) {
        log.error("Could not create MessageDigest for hash. " + e.getMessage());
        throw new RuntimeException(e);
    }
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest) NoSuchProviderException(java.security.NoSuchProviderException)

Example 49 with NoSuchProviderException

use of java.security.NoSuchProviderException in project cloudstack by apache.

the class RawHTTP method _getSocket.

private Socket _getSocket() throws IOException {
    if (useSSL) {
        SSLContext context = null;
        try {
            context = SSLUtils.getSSLContext("SunJSSE");
        } catch (NoSuchAlgorithmException e) {
            s_logger.error("Unexpected exception ", e);
        } catch (NoSuchProviderException e) {
            s_logger.error("Unexpected exception ", e);
        }
        if (context == null)
            throw new IOException("Unable to setup SSL context");
        SSLSocket ssl = null;
        try {
            context.init(null, trustAllCerts, new SecureRandom());
            SocketFactory factory = new SecureSSLSocketFactory(context);
            ssl = (SSLSocket) factory.createSocket(host, port);
            ssl.setEnabledProtocols(SSLUtils.getSupportedProtocols(ssl.getEnabledProtocols()));
        /* ssl.setSSLParameters(context.getDefaultSSLParameters()); */
        } catch (IOException e) {
            s_logger.error("IOException: " + e.getMessage(), e);
            throw e;
        } catch (KeyManagementException e) {
            s_logger.error("KeyManagementException: " + e.getMessage(), e);
        } catch (NoSuchAlgorithmException e) {
            s_logger.error("NoSuchAlgorithmException: " + e.getMessage(), e);
        }
        return ssl;
    } else {
        return new Socket(host, port);
    }
}
Also used : SocketFactory(javax.net.SocketFactory) SecureSSLSocketFactory(org.apache.cloudstack.utils.security.SecureSSLSocketFactory) SSLSocket(javax.net.ssl.SSLSocket) SecureRandom(java.security.SecureRandom) SecureSSLSocketFactory(org.apache.cloudstack.utils.security.SecureSSLSocketFactory) SSLContext(javax.net.ssl.SSLContext) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) NoSuchProviderException(java.security.NoSuchProviderException) KeyManagementException(java.security.KeyManagementException) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket)

Example 50 with NoSuchProviderException

use of java.security.NoSuchProviderException in project TinyKeePass by sorz.

the class SecureStringStorage method generateNewKey.

public void generateNewKey(boolean auth, int authSecs) throws SystemException {
    KeyGenParameterSpec keySpec = new KeyGenParameterSpec.Builder(KEY_ALIAS, KeyProperties.PURPOSE_ENCRYPT | KeyProperties.PURPOSE_DECRYPT).setBlockModes(KeyProperties.BLOCK_MODE_GCM).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE).setUserAuthenticationRequired(auth).setUserAuthenticationValidityDurationSeconds(authSecs).build();
    KeyGenerator keyGenerator;
    try {
        keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore");
        keyGenerator.init(keySpec);
    } catch (NoSuchAlgorithmException | NoSuchProviderException | InvalidAlgorithmParameterException e) {
        throw new SystemException(e);
    }
    keyGenerator.generateKey();
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyGenParameterSpec(android.security.keystore.KeyGenParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchProviderException(java.security.NoSuchProviderException) KeyGenerator(javax.crypto.KeyGenerator)

Aggregations

NoSuchProviderException (java.security.NoSuchProviderException)102 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)75 InvalidKeyException (java.security.InvalidKeyException)33 IOException (java.io.IOException)31 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)20 CertificateException (java.security.cert.CertificateException)19 SignatureException (java.security.SignatureException)15 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)14 Cipher (javax.crypto.Cipher)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 KeyStoreException (java.security.KeyStoreException)12 X509Certificate (java.security.cert.X509Certificate)12 BadPaddingException (javax.crypto.BadPaddingException)12 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)12 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)10 SecretKey (javax.crypto.SecretKey)10 CertificateFactory (java.security.cert.CertificateFactory)9 KeyFactory (java.security.KeyFactory)8 CertificateEncodingException (java.security.cert.CertificateEncodingException)8 IvParameterSpec (javax.crypto.spec.IvParameterSpec)8