use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class CipherTest method checkCipher_ShortBlock_Failure.
private void checkCipher_ShortBlock_Failure(CipherTestParam p, String provider) throws Exception {
SecretKey key = new SecretKeySpec(p.key, "AES");
Cipher c = Cipher.getInstance(p.mode + "/NoPadding", provider);
if (c.getBlockSize() == 0) {
return;
}
c.init(Cipher.ENCRYPT_MODE, key);
try {
c.doFinal(new byte[] { 0x01, 0x02, 0x03 });
fail("Should throw IllegalBlockSizeException on wrong-sized block");
} catch (IllegalBlockSizeException expected) {
}
}
use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class CipherTest method testRSA_ECB_NoPadding_Private_CombinedUpdateAndDoFinal_TooBig_Failure.
private void testRSA_ECB_NoPadding_Private_CombinedUpdateAndDoFinal_TooBig_Failure(String provider) throws Exception {
KeyFactory kf = KeyFactory.getInstance("RSA");
RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
final PrivateKey privKey = kf.generatePrivate(keySpec);
Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
/*
* You're actually encrypting with public keys, but there is no
* distinction made here. It's all keyed off of what kind of key you're
* using. ENCRYPT_MODE and DECRYPT_MODE are the same.
*/
c.init(Cipher.ENCRYPT_MODE, privKey);
c.update(RSA_Vector1_ZeroPadded_Encrypted);
try {
c.doFinal(RSA_Vector1_ZeroPadded_Encrypted);
fail("Should have error when block size is too big.");
} catch (IllegalBlockSizeException success) {
assertFalse(provider, "BC".equals(provider));
} catch (ArrayIndexOutOfBoundsException success) {
assertEquals("BC", provider);
}
}
use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class IllegalBlockSizeExceptionTest method testIllegalBlockSizeException03.
/**
* Test for <code>IllegalBlockSizeException(String)</code> constructor
* Assertion: constructs IllegalBlockSizeException when <code>msg</code>
* is null
*/
public void testIllegalBlockSizeException03() {
String msg = null;
IllegalBlockSizeException tE = new IllegalBlockSizeException(msg);
assertNull("getMessage() must return null.", tE.getMessage());
assertNull("getCause() must return null", tE.getCause());
}
use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class AlgorithmParameterAsymmetricHelper method test.
@Override
public void test(AlgorithmParameters parameters) {
KeyPairGenerator generator = null;
try {
generator = KeyPairGenerator.getInstance(algorithmName);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
}
generator.initialize(1024);
KeyPair keyPair = generator.generateKeyPair();
Cipher cipher = null;
try {
cipher = Cipher.getInstance(algorithmName);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
} catch (NoSuchPaddingException e) {
Assert.fail(e.getMessage());
}
try {
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), parameters);
} catch (InvalidKeyException e) {
Assert.fail(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
Assert.fail(e.getMessage());
}
byte[] bs = null;
try {
bs = cipher.doFinal(plainData.getBytes());
} catch (IllegalBlockSizeException e) {
Assert.fail(e.getMessage());
} catch (BadPaddingException e) {
Assert.fail(e.getMessage());
}
try {
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate(), parameters);
} catch (InvalidKeyException e) {
Assert.fail(e.getMessage());
} catch (InvalidAlgorithmParameterException e) {
Assert.fail(e.getMessage());
}
byte[] decrypted = null;
try {
decrypted = cipher.doFinal(bs);
} catch (IllegalBlockSizeException e) {
Assert.fail(e.getMessage());
} catch (BadPaddingException e) {
Assert.fail(e.getMessage());
}
Assert.assertTrue(Arrays.equals(plainData.getBytes(), decrypted));
}
use of javax.crypto.IllegalBlockSizeException in project platform_frameworks_base by android.
the class AndroidKeyStoreCipherSpiBase method engineWrap.
@Override
protected final byte[] engineWrap(Key key) throws IllegalBlockSizeException, InvalidKeyException {
if (mKey == null) {
throw new IllegalStateException("Not initilized");
}
if (!isEncrypting()) {
throw new IllegalStateException("Cipher must be initialized in Cipher.WRAP_MODE to wrap keys");
}
if (key == null) {
throw new NullPointerException("key == null");
}
byte[] encoded = null;
if (key instanceof SecretKey) {
if ("RAW".equalsIgnoreCase(key.getFormat())) {
encoded = key.getEncoded();
}
if (encoded == null) {
try {
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(key.getAlgorithm());
SecretKeySpec spec = (SecretKeySpec) keyFactory.getKeySpec((SecretKey) key, SecretKeySpec.class);
encoded = spec.getEncoded();
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new InvalidKeyException("Failed to wrap key because it does not export its key material", e);
}
}
} else if (key instanceof PrivateKey) {
if ("PKCS8".equalsIgnoreCase(key.getFormat())) {
encoded = key.getEncoded();
}
if (encoded == null) {
try {
KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
PKCS8EncodedKeySpec spec = keyFactory.getKeySpec(key, PKCS8EncodedKeySpec.class);
encoded = spec.getEncoded();
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new InvalidKeyException("Failed to wrap key because it does not export its key material", e);
}
}
} else if (key instanceof PublicKey) {
if ("X.509".equalsIgnoreCase(key.getFormat())) {
encoded = key.getEncoded();
}
if (encoded == null) {
try {
KeyFactory keyFactory = KeyFactory.getInstance(key.getAlgorithm());
X509EncodedKeySpec spec = keyFactory.getKeySpec(key, X509EncodedKeySpec.class);
encoded = spec.getEncoded();
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new InvalidKeyException("Failed to wrap key because it does not export its key material", e);
}
}
} else {
throw new InvalidKeyException("Unsupported key type: " + key.getClass().getName());
}
if (encoded == null) {
throw new InvalidKeyException("Failed to wrap key because it does not export its key material");
}
try {
return engineDoFinal(encoded, 0, encoded.length);
} catch (BadPaddingException e) {
throw (IllegalBlockSizeException) new IllegalBlockSizeException().initCause(e);
}
}
Aggregations