use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class CipherTest method test_doFinal$BII$B.
public void test_doFinal$BII$B() throws Exception {
byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
byte[] b1 = new byte[30];
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
try {
c.doFinal(b, 0, 10, b1);
fail();
} catch (IllegalBlockSizeException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
try {
c.doFinal(b, 0, 10, b1);
fail();
} catch (IllegalStateException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
int len = c.doFinal(b, 0, 16, b1);
assertEquals(16, len);
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
try {
c.doFinal(b1, 0, 24, new byte[42]);
fail();
} catch (BadPaddingException expected) {
}
b1 = new byte[6];
c = Cipher.getInstance("DESede");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
try {
c.doFinal(b, 3, 6, b1);
fail();
} catch (ShortBufferException expected) {
}
}
use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class SealedObjectTest method testSealedObject1.
/**
* SealedObject(Serializable object, Cipher c) method testing. Tests if the
* NullPointerException is thrown in the case of null cipher.
*/
public void testSealedObject1() throws Exception {
String secret = "secret string";
try {
new SealedObject(secret, null);
fail("NullPointerException should be thrown in the case " + "of null cipher.");
} catch (NullPointerException e) {
}
KeyGenerator kg = KeyGenerator.getInstance("DES");
Key key = kg.generateKey();
IvParameterSpec ips = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 });
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, ips);
SealedObject so = new SealedObject(secret, cipher);
cipher = Cipher.getInstance("DES/CBC/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, key, ips);
try {
new SealedObject(secret, cipher);
fail("IllegalBlockSizeException expected");
} catch (IllegalBlockSizeException e) {
//expected
}
}
use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class AlgorithmParameterSymmetricHelper method test.
@Override
public void test(AlgorithmParameters parameters) {
KeyGenerator generator = null;
try {
generator = KeyGenerator.getInstance(algorithmName);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
}
generator.init(keySize);
Key key = generator.generateKey();
Cipher cipher = null;
try {
String transformation = algorithmName;
if (blockmode != null) {
transformation += "/" + blockmode;
}
cipher = Cipher.getInstance(transformation);
} catch (NoSuchAlgorithmException e) {
Assert.fail(e.getMessage());
} catch (NoSuchPaddingException e) {
Assert.fail(e.getMessage());
}
try {
cipher.init(Cipher.ENCRYPT_MODE, key, 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, key, 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 jgnash by ccavanaugh.
the class EncryptionManager method encrypt.
/**
* Encrypts the supplied string.
*
* @param plain String to encrypt
* @return the encrypted string
*/
public String encrypt(final String plain) {
try {
final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key);
return printBase64Binary(cipher.doFinal(plain.getBytes(StandardCharsets.UTF_8)));
} catch (final InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
logger.log(Level.SEVERE, e.getMessage(), e);
}
return null;
}
use of javax.crypto.IllegalBlockSizeException in project jgnash by ccavanaugh.
the class EncryptionManager method decrypt.
/**
* Decrypts the supplied string.
*
* @param encrypted String to decrypt
* @return The decrypted string of {@code DECRYPTION_ERROR_TAG} if decryption fails
* @see #DECRYPTION_ERROR_TAG
*/
public String decrypt(final String encrypted) {
try {
final Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, key);
return new String(cipher.doFinal(parseBase64Binary(encrypted)), StandardCharsets.UTF_8);
} catch (final InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException e) {
logger.log(Level.SEVERE, "Invalid password");
return DECRYPTION_ERROR_TAG;
}
}
Aggregations