use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.
the class CipherTest method test_initWithAlgorithmParameterSpec.
/**
* javax.crypto.Cipher#init(int, java.security.Key,
* java.security.spec.AlgorithmParameterSpec)
*/
public void test_initWithAlgorithmParameterSpec() throws Exception {
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap);
byte[] cipherIV = cipher.getIV();
assertTrue("IVs differ", Arrays.equals(cipherIV, IV));
cipher = Cipher.getInstance("DES/CBC/NoPadding");
try {
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap);
fail();
} catch (InvalidKeyException expected) {
}
cipher = Cipher.getInstance("DES/CBC/NoPadding");
ap = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
try {
cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
fail();
} catch (InvalidAlgorithmParameterException expected) {
}
}
use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.
the class SealedObject method getObject.
/**
* Returns the wrapped object, decrypting it using the specified key. The
* specified provider is used to retrieve the cipher algorithm.
*
* @param key
* the key to decrypt the data.
* @param provider
* the name of the provider that provides the cipher algorithm.
* @return the encapsulated object.
* @throws IOException
* if deserialization fails.
* @throws ClassNotFoundException
* if deserialization fails.
* @throws NoSuchAlgorithmException
* if the algorithm used to decrypt the data is not available.
* @throws NoSuchProviderException
* if the specified provider is not available.
* @throws InvalidKeyException
* if the specified key cannot be used to decrypt the data.
*/
public final Object getObject(Key key, String provider) throws IOException, ClassNotFoundException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException {
if (provider == null || provider.isEmpty()) {
throw new IllegalArgumentException("provider name empty or null");
}
try {
Cipher cipher = Cipher.getInstance(sealAlg, provider);
if ((paramsAlg != null) && (paramsAlg.length() != 0)) {
AlgorithmParameters params = AlgorithmParameters.getInstance(paramsAlg);
params.init(encodedParams);
cipher.init(Cipher.DECRYPT_MODE, key, params);
} else {
cipher.init(Cipher.DECRYPT_MODE, key);
}
byte[] serialized = cipher.doFinal(encryptedContent);
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(serialized));
return ois.readObject();
} catch (NoSuchPaddingException e) {
// with existing padding
throw new NoSuchAlgorithmException(e.toString());
} catch (InvalidAlgorithmParameterException e) {
// with correct algorithm parameters
throw new NoSuchAlgorithmException(e.toString());
} catch (IllegalBlockSizeException e) {
// was correctly made
throw new NoSuchAlgorithmException(e.toString());
} catch (BadPaddingException e) {
// was correctly made
throw new NoSuchAlgorithmException(e.toString());
} catch (IllegalStateException e) {
// should never be thrown because cipher is initialized
throw new NoSuchAlgorithmException(e.toString());
}
}
use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.
the class EncryptedPrivateKeyInfo method getKeySpec.
/**
* Returns the {@code PKCS8EncodedKeySpec} object extracted from the
* encrypted data.
*
* @param decryptKey
* the key to decrypt the encrypted data with.
* @return the extracted {@code PKCS8EncodedKeySpec}.
* @throws NoSuchAlgorithmException
* if no usable cipher can be found to decrypt the encrypted
* data.
* @throws InvalidKeyException
* if {@code decryptKey} is not usable to decrypt the encrypted
* data.
* @throws NullPointerException
* if {@code decryptKey} is {@code null}.
*/
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey) throws NoSuchAlgorithmException, InvalidKeyException {
if (decryptKey == null) {
throw new NullPointerException("decryptKey == null");
}
try {
Cipher cipher = Cipher.getInstance(algName);
if (algParameters == null) {
cipher.init(Cipher.DECRYPT_MODE, decryptKey);
} else {
cipher.init(Cipher.DECRYPT_MODE, decryptKey, algParameters);
}
byte[] decryptedData = cipher.doFinal(encryptedData);
try {
ASN1PrivateKeyInfo.verify(decryptedData);
} catch (IOException e1) {
throw invalidKey();
}
return new PKCS8EncodedKeySpec(decryptedData);
} catch (NoSuchPaddingException e) {
throw new NoSuchAlgorithmException(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
throw new NoSuchAlgorithmException(e.getMessage());
} catch (IllegalStateException e) {
throw new InvalidKeyException(e.getMessage());
} catch (IllegalBlockSizeException e) {
throw new InvalidKeyException(e.getMessage());
} catch (BadPaddingException e) {
throw new InvalidKeyException(e.getMessage());
}
}
use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.
the class EncryptedPrivateKeyInfo method getKeySpec.
/**
* Returns the {@code PKCS8EncodedKeySpec} object extracted from the
* encrypted data.
*
* @param decryptKey
* the key to decrypt the encrypted data with.
* @param providerName
* the name of a provider whose cipher implementation should be
* used.
* @return the extracted {@code PKCS8EncodedKeySpec}.
* @throws NoSuchProviderException
* if no provider with {@code providerName} can be found.
* @throws NoSuchAlgorithmException
* if no usable cipher can be found to decrypt the encrypted
* data.
* @throws InvalidKeyException
* if {@code decryptKey} is not usable to decrypt the encrypted
* data.
* @throws NullPointerException
* if {@code decryptKey} or {@code providerName} is {@code null}
* .
*/
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey, String providerName) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException {
if (decryptKey == null) {
throw new NullPointerException("decryptKey == null");
}
if (providerName == null) {
throw new NullPointerException("providerName == null");
}
try {
Cipher cipher = Cipher.getInstance(algName, providerName);
if (algParameters == null) {
cipher.init(Cipher.DECRYPT_MODE, decryptKey);
} else {
cipher.init(Cipher.DECRYPT_MODE, decryptKey, algParameters);
}
byte[] decryptedData = cipher.doFinal(encryptedData);
try {
ASN1PrivateKeyInfo.verify(decryptedData);
} catch (IOException e1) {
throw invalidKey();
}
return new PKCS8EncodedKeySpec(decryptedData);
} catch (NoSuchPaddingException e) {
throw new NoSuchAlgorithmException(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
throw new NoSuchAlgorithmException(e.getMessage());
} catch (IllegalStateException e) {
throw new InvalidKeyException(e.getMessage());
} catch (IllegalBlockSizeException e) {
throw new InvalidKeyException(e.getMessage());
} catch (BadPaddingException e) {
throw new InvalidKeyException(e.getMessage());
}
}
use of java.security.InvalidAlgorithmParameterException in project robovm by robovm.
the class EncryptedPrivateKeyInfo method getKeySpec.
/**
* Returns the {@code PKCS8EncodedKeySpec} object extracted from the
* encrypted data.
*
* @param decryptKey
* the key to decrypt the encrypted data with.
* @param provider
* the provider whose cipher implementation should be used.
* @return the extracted {@code PKCS8EncodedKeySpec}.
* @throws NoSuchAlgorithmException
* if no usable cipher can be found to decrypt the encrypted
* data.
* @throws InvalidKeyException
* if {@code decryptKey} is not usable to decrypt the encrypted
* data.
* @throws NullPointerException
* if {@code decryptKey} or {@code provider} is {@code null}.
*/
public PKCS8EncodedKeySpec getKeySpec(Key decryptKey, Provider provider) throws NoSuchAlgorithmException, InvalidKeyException {
if (decryptKey == null) {
throw new NullPointerException("decryptKey == null");
}
if (provider == null) {
throw new NullPointerException("provider == null");
}
try {
Cipher cipher = Cipher.getInstance(algName, provider);
if (algParameters == null) {
cipher.init(Cipher.DECRYPT_MODE, decryptKey);
} else {
cipher.init(Cipher.DECRYPT_MODE, decryptKey, algParameters);
}
byte[] decryptedData = cipher.doFinal(encryptedData);
try {
ASN1PrivateKeyInfo.verify(decryptedData);
} catch (IOException e1) {
throw invalidKey();
}
return new PKCS8EncodedKeySpec(decryptedData);
} catch (NoSuchPaddingException e) {
throw new NoSuchAlgorithmException(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
throw new NoSuchAlgorithmException(e.getMessage());
} catch (IllegalStateException e) {
throw new InvalidKeyException(e.getMessage());
} catch (IllegalBlockSizeException e) {
throw new InvalidKeyException(e.getMessage());
} catch (BadPaddingException e) {
throw new InvalidKeyException(e.getMessage());
}
}
Aggregations