use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class CipherTest method test_doFinal.
/**
* javax.crypto.Cipher#doFinal()
*/
public void test_doFinal() throws Exception {
for (int index = 1; index < 4; index++) {
Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".key");
DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial);
SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE");
Key k = skf.generateSecret(keySpec);
byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".iv");
IvParameterSpec iv = new IvParameterSpec(ivMaterial);
c.init(Cipher.ENCRYPT_MODE, k, iv);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] input = new byte[256];
String resPath = "hyts_" + "des-ede3-cbc.test" + index + ".plaintext";
File resources = Support_Resources.createTempFolder();
Support_Resources.copyFile(resources, null, resPath);
InputStream is = Support_Resources.getStream(resPath);
int bytesRead = is.read(input, 0, 256);
while (bytesRead > 0) {
byte[] output = c.update(input, 0, bytesRead);
if (output != null) {
baos.write(output);
}
bytesRead = is.read(input, 0, 256);
}
byte[] output = c.doFinal();
if (output != null) {
baos.write(output);
}
byte[] encryptedPlaintext = baos.toByteArray();
is.close();
byte[] cipherText = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".ciphertext");
assertEquals("Operation produced incorrect results for index " + index, Arrays.toString(cipherText), Arrays.toString(encryptedPlaintext));
}
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);
c.update(b, 0, 10, b1, 5);
try {
c.doFinal();
fail();
} catch (IllegalBlockSizeException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
try {
c.doFinal();
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, 0);
assertEquals(16, len);
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
assertTrue(Arrays.equals(c.getIV(), IV));
c.update(b1, 0, 24, b, 0);
try {
c.doFinal();
fail();
} catch (BadPaddingException expected) {
}
}
use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class CipherTest method test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer.
public void test_doFinalLjava_nio_ByteBufferLjava_nio_ByteBuffer() throws Exception {
byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
ByteBuffer bInput = ByteBuffer.allocate(64);
ByteBuffer bOutput = ByteBuffer.allocate(64);
AlgorithmParameterSpec ap = new IvParameterSpec(IV);
Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
bInput.put(b, 0, 10);
try {
c.doFinal(bInput, bOutput);
fail();
} catch (IllegalBlockSizeException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
try {
c.doFinal(bInput, bOutput);
fail();
} catch (IllegalStateException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
bInput = ByteBuffer.allocate(16);
bInput.put(b, 0, 16);
int len = c.doFinal(bInput, bOutput);
assertEquals(0, len);
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
bInput = ByteBuffer.allocate(64);
try {
c.doFinal(bOutput, bInput);
fail();
} catch (BadPaddingException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput.put(b, 0, 16);
try {
c.doFinal(bInput, bInput);
fail();
} catch (IllegalArgumentException expected) {
}
c = Cipher.getInstance("DES/CBC/NoPadding");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES);
bInput.put(b, 0, 16);
try {
c.doFinal(bInput, bOutput.asReadOnlyBuffer());
fail();
} catch (ReadOnlyBufferException expected) {
}
bInput.rewind();
bInput.put(b, 0, 16);
bOutput = ByteBuffer.allocate(8);
c = Cipher.getInstance("DESede");
c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES);
try {
c.doFinal(bInput, bOutput);
fail();
} catch (ShortBufferException expected) {
}
}
use of javax.crypto.IllegalBlockSizeException in project robovm by robovm.
the class BaseBlockCipher method engineDoFinal.
protected byte[] engineDoFinal(byte[] input, int inputOffset, int inputLen) throws IllegalBlockSizeException, BadPaddingException {
int len = 0;
byte[] tmp = new byte[engineGetOutputSize(inputLen)];
if (inputLen != 0) {
len = cipher.processBytes(input, inputOffset, inputLen, tmp, 0);
}
try {
len += cipher.doFinal(tmp, len);
} catch (DataLengthException e) {
throw new IllegalBlockSizeException(e.getMessage());
} catch (InvalidCipherTextException e) {
throw new BadPaddingException(e.getMessage());
}
if (len == tmp.length) {
return tmp;
}
byte[] out = new byte[len];
System.arraycopy(tmp, 0, out, 0, len);
return out;
}
use of javax.crypto.IllegalBlockSizeException in project cloudstack by apache.
the class CertServiceImpl method validateKeys.
private void validateKeys(final PublicKey pubKey, final PrivateKey privKey) {
Preconditions.checkNotNull(pubKey);
Preconditions.checkNotNull(privKey);
if (!pubKey.getAlgorithm().equals(privKey.getAlgorithm())) {
throw new IllegalArgumentException("Public and private key have different algorithms");
}
// No encryption for DSA
if (pubKey.getAlgorithm() != "RSA") {
return;
}
try {
final String data = "ENCRYPT_DATA";
final SecureRandom random = new SecureRandom();
final Cipher cipher = Cipher.getInstance(pubKey.getAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, privKey, random);
final byte[] encryptedData = cipher.doFinal(data.getBytes());
cipher.init(Cipher.DECRYPT_MODE, pubKey, random);
final String decreptedData = new String(cipher.doFinal(encryptedData));
if (!decreptedData.equals(data)) {
throw new IllegalStateException("Bad public-private key");
}
} catch (final BadPaddingException | IllegalBlockSizeException | InvalidKeyException | NoSuchPaddingException e) {
throw new IllegalStateException("Bad public-private key", e);
} catch (final NoSuchAlgorithmException e) {
throw new IllegalStateException("Invalid algorithm for public-private key", e);
}
}
use of javax.crypto.IllegalBlockSizeException in project cloudstack by apache.
the class ConsoleProxyPasswordBasedEncryptor method decryptText.
public String decryptText(String encryptedText) {
if (encryptedText == null || encryptedText.isEmpty())
return encryptedText;
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec keySpec = new SecretKeySpec(keyIvPair.getKeyBytes(), "AES");
cipher.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(keyIvPair.getIvBytes()));
byte[] encryptedBytes = Base64.decodeBase64(encryptedText);
return new String(cipher.doFinal(encryptedBytes));
} catch (NoSuchAlgorithmException e) {
s_logger.error("Unexpected exception ", e);
return null;
} catch (NoSuchPaddingException e) {
s_logger.error("Unexpected exception ", e);
return null;
} catch (IllegalBlockSizeException e) {
s_logger.error("Unexpected exception ", e);
return null;
} catch (BadPaddingException e) {
s_logger.error("Unexpected exception ", e);
return null;
} catch (InvalidKeyException e) {
s_logger.error("Unexpected exception ", e);
return null;
} catch (InvalidAlgorithmParameterException e) {
s_logger.error("Unexpected exception ", e);
return null;
}
}
Aggregations