Search in sources :

Example 96 with InvalidKeyException

use of java.security.InvalidKeyException in project jersey by jersey.

the class RsaSha1Method method sign.

/**
     * Generates the RSA-SHA1 signature of OAuth request elements.
     *
     * @param baseString the combined OAuth elements to sign.
     * @param secrets the secrets object containing the private key for generating the signature.
     * @return the OAuth signature, in base64-encoded form.
     * @throws InvalidSecretException if the supplied secret is not valid.
     */
@Override
public String sign(final String baseString, final OAuth1Secrets secrets) throws InvalidSecretException {
    final Signature signature;
    try {
        signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    } catch (final NoSuchAlgorithmException nsae) {
        throw new IllegalStateException(nsae);
    }
    byte[] decodedPrivateKey;
    try {
        decodedPrivateKey = Base64.decode(secrets.getConsumerSecret());
    } catch (final IOException ioe) {
        throw new InvalidSecretException(LocalizationMessages.ERROR_INVALID_CONSUMER_SECRET(ioe));
    }
    final KeyFactory keyFactory;
    try {
        keyFactory = KeyFactory.getInstance(KEY_TYPE);
    } catch (final NoSuchAlgorithmException nsae) {
        throw new IllegalStateException(nsae);
    }
    final EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedPrivateKey);
    final RSAPrivateKey rsaPrivateKey;
    try {
        rsaPrivateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec);
    } catch (final InvalidKeySpecException ikse) {
        throw new IllegalStateException(ikse);
    }
    try {
        signature.initSign(rsaPrivateKey);
    } catch (final InvalidKeyException ike) {
        throw new IllegalStateException(ike);
    }
    try {
        signature.update(baseString.getBytes());
    } catch (final SignatureException se) {
        throw new IllegalStateException(se);
    }
    final byte[] rsasha1;
    try {
        rsasha1 = signature.sign();
    } catch (final SignatureException se) {
        throw new IllegalStateException(se);
    }
    return Base64.encode(rsasha1);
}
Also used : NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) SignatureException(java.security.SignatureException) InvalidKeyException(java.security.InvalidKeyException) Signature(java.security.Signature) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory) EncodedKeySpec(java.security.spec.EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec)

Example 97 with InvalidKeyException

use of java.security.InvalidKeyException in project XobotOS by xamarin.

the class JCEMac method engineInit.

protected void engineInit(Key key, AlgorithmParameterSpec params) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    if (key == null) {
        throw new InvalidKeyException("key is null");
    }
    if (key instanceof JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) key;
        if (k.getParam() != null) {
            param = k.getParam();
        } else if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEMacParameters(k, params);
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else {
        throw new InvalidAlgorithmParameterException("unknown parameter type.");
    }
    macEngine.init(param);
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 98 with InvalidKeyException

use of java.security.InvalidKeyException in project XobotOS by xamarin.

the class JCEStreamCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameterSpec params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    CipherParameters param;
    this.pbeSpec = null;
    this.pbeAlgorithm = null;
    this.engineParams = null;
    //
    if (!(key instanceof SecretKey)) {
        throw new InvalidKeyException("Key for algorithm " + key.getAlgorithm() + " not suitable for symmetric enryption.");
    }
    if (key instanceof JCEPBEKey) {
        JCEPBEKey k = (JCEPBEKey) key;
        if (k.getOID() != null) {
            pbeAlgorithm = k.getOID().getId();
        } else {
            pbeAlgorithm = k.getAlgorithm();
        }
        if (k.getParam() != null) {
            param = k.getParam();
            pbeSpec = new PBEParameterSpec(k.getSalt(), k.getIterationCount());
        } else if (params instanceof PBEParameterSpec) {
            param = PBE.Util.makePBEParameters(k, params, cipher.getAlgorithmName());
            pbeSpec = (PBEParameterSpec) params;
        } else {
            throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
        }
        if (k.getIvSize() != 0) {
            ivParam = (ParametersWithIV) param;
        }
    } else if (params == null) {
        param = new KeyParameter(key.getEncoded());
    } else if (params instanceof IvParameterSpec) {
        param = new ParametersWithIV(new KeyParameter(key.getEncoded()), ((IvParameterSpec) params).getIV());
        ivParam = (ParametersWithIV) param;
    } else {
        throw new IllegalArgumentException("unknown parameter type.");
    }
    if ((ivLength != 0) && !(param instanceof ParametersWithIV)) {
        SecureRandom ivRandom = random;
        if (ivRandom == null) {
            ivRandom = new SecureRandom();
        }
        if ((opmode == Cipher.ENCRYPT_MODE) || (opmode == Cipher.WRAP_MODE)) {
            byte[] iv = new byte[ivLength];
            ivRandom.nextBytes(iv);
            param = new ParametersWithIV(param, iv);
            ivParam = (ParametersWithIV) param;
        } else {
            throw new InvalidAlgorithmParameterException("no IV set when one expected");
        }
    }
    switch(opmode) {
        case Cipher.ENCRYPT_MODE:
        case Cipher.WRAP_MODE:
            cipher.init(true, param);
            break;
        case Cipher.DECRYPT_MODE:
        case Cipher.UNWRAP_MODE:
            cipher.init(false, param);
            break;
        default:
            System.out.println("eeek!");
    }
}
Also used : CipherParameters(org.bouncycastle.crypto.CipherParameters) ParametersWithIV(org.bouncycastle.crypto.params.ParametersWithIV) SecretKey(javax.crypto.SecretKey) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) KeyParameter(org.bouncycastle.crypto.params.KeyParameter) SecureRandom(java.security.SecureRandom) IvParameterSpec(javax.crypto.spec.IvParameterSpec) InvalidKeyException(java.security.InvalidKeyException) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 99 with InvalidKeyException

use of java.security.InvalidKeyException in project XobotOS by xamarin.

the class JCEStreamCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        for (int i = 0; i != availableSpecs.length; i++) {
            try {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            } catch (Exception e) {
                continue;
            }
        }
        if (paramSpec == null) {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }
    engineInit(opmode, key, paramSpec, random);
    engineParams = params;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) DataLengthException(org.bouncycastle.crypto.DataLengthException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) InvalidKeyException(java.security.InvalidKeyException) ShortBufferException(javax.crypto.ShortBufferException)

Example 100 with InvalidKeyException

use of java.security.InvalidKeyException in project XobotOS by xamarin.

the class JCEBlockCipher method engineInit.

protected void engineInit(int opmode, Key key, AlgorithmParameters params, SecureRandom random) throws InvalidKeyException, InvalidAlgorithmParameterException {
    AlgorithmParameterSpec paramSpec = null;
    if (params != null) {
        for (int i = 0; i != availableSpecs.length; i++) {
            try {
                paramSpec = params.getParameterSpec(availableSpecs[i]);
                break;
            } catch (Exception e) {
            // try again if possible
            }
        }
        if (paramSpec == null) {
            throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
        }
    }
    engineInit(opmode, key, paramSpec, random);
    engineParams = params;
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidCipherTextException(org.bouncycastle.crypto.InvalidCipherTextException) DataLengthException(org.bouncycastle.crypto.DataLengthException) InvalidParameterException(java.security.InvalidParameterException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) ShortBufferException(javax.crypto.ShortBufferException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException)

Aggregations

InvalidKeyException (java.security.InvalidKeyException)499 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)263 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)124 SignatureException (java.security.SignatureException)95 IOException (java.io.IOException)94 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)93 BadPaddingException (javax.crypto.BadPaddingException)89 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)87 Cipher (javax.crypto.Cipher)77 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)63 SecretKeySpec (javax.crypto.spec.SecretKeySpec)63 Signature (java.security.Signature)58 SecretKey (javax.crypto.SecretKey)50 PublicKey (java.security.PublicKey)49 PrivateKey (java.security.PrivateKey)47 CertificateException (java.security.cert.CertificateException)46 Mac (javax.crypto.Mac)44 IvParameterSpec (javax.crypto.spec.IvParameterSpec)41 NoSuchProviderException (java.security.NoSuchProviderException)39 KeyStoreException (java.security.KeyStoreException)33