use of javax.crypto.IllegalBlockSizeException in project oxAuth by GluuFederation.
the class TokenSignaturesHttpTest method testES256.
@Parameters({ "clientJwksUri", "ES256_keyId", "dnName", "keyStoreFile", "keyStoreSecret" })
@Test
public void testES256(final String clientJwksUri, final String keyId, final String dnName, final String keyStoreFile, final String keyStoreSecret) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, SignatureException, InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, IOException, NoSuchPaddingException, BadPaddingException {
try {
showTitle("Test ES256");
JwkClient jwkClient = new JwkClient(clientJwksUri);
JwkResponse jwkResponse = jwkClient.exec();
String signingInput = "eyJhbGciOiJIUzI1NiJ9.eyJub25jZSI6ICI2Qm9HN1QwR0RUZ2wiLCAiaWRfdG9rZW4iOiB7Im1heF9hZ2UiOiA4NjQwMH0sICJzdGF0ZSI6ICJTVEFURTAiLCAicmVkaXJlY3RfdXJpIjogImh0dHBzOi8vbG9jYWxob3N0L2NhbGxiYWNrMSIsICJ1c2VyaW5mbyI6IHsiY2xhaW1zIjogeyJuYW1lIjogbnVsbH19LCAiY2xpZW50X2lkIjogIkAhMTExMSEwMDA4IUU2NTQuQjQ2MCIsICJzY29wZSI6IFsib3BlbmlkIl0sICJyZXNwb25zZV90eXBlIjogWyJjb2RlIl19";
OxAuthCryptoProvider cryptoProvider = new OxAuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName);
String encodedSignature = cryptoProvider.sign(signingInput, keyId, null, SignatureAlgorithm.ES256);
System.out.println("Encoded Signature: " + encodedSignature);
boolean signatureVerified = cryptoProvider.verifySignature(signingInput, encodedSignature, keyId, jwkResponse.getJwks().toJSONObject(), null, SignatureAlgorithm.ES256);
assertTrue(signatureVerified, "Invalid signature");
} catch (Exception e) {
fail(e.getMessage(), e);
}
}
use of javax.crypto.IllegalBlockSizeException in project oxAuth by GluuFederation.
the class TokenSignaturesHttpTest method testES384.
@Parameters({ "clientJwksUri", "ES384_keyId", "dnName", "keyStoreFile", "keyStoreSecret" })
@Test
public void testES384(final String clientJwksUri, final String keyId, final String dnName, final String keyStoreFile, final String keyStoreSecret) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, SignatureException, InvalidKeyException, InvalidKeySpecException, IllegalBlockSizeException, IOException, NoSuchPaddingException, BadPaddingException {
try {
showTitle("Test ES384");
JwkClient jwkClient = new JwkClient(clientJwksUri);
JwkResponse jwkResponse = jwkClient.exec();
String signingInput = "eyJhbGciOiJIUzI1NiJ9.eyJub25jZSI6ICI2Qm9HN1QwR0RUZ2wiLCAiaWRfdG9rZW4iOiB7Im1heF9hZ2UiOiA4NjQwMH0sICJzdGF0ZSI6ICJTVEFURTAiLCAicmVkaXJlY3RfdXJpIjogImh0dHBzOi8vbG9jYWxob3N0L2NhbGxiYWNrMSIsICJ1c2VyaW5mbyI6IHsiY2xhaW1zIjogeyJuYW1lIjogbnVsbH19LCAiY2xpZW50X2lkIjogIkAhMTExMSEwMDA4IUU2NTQuQjQ2MCIsICJzY29wZSI6IFsib3BlbmlkIl0sICJyZXNwb25zZV90eXBlIjogWyJjb2RlIl19";
OxAuthCryptoProvider cryptoProvider = new OxAuthCryptoProvider(keyStoreFile, keyStoreSecret, dnName);
String encodedSignature = cryptoProvider.sign(signingInput, keyId, null, SignatureAlgorithm.ES384);
System.out.println("Encoded Signature: " + encodedSignature);
boolean signatureVerified = cryptoProvider.verifySignature(signingInput, encodedSignature, keyId, jwkResponse.getJwks().toJSONObject(), null, SignatureAlgorithm.ES384);
assertTrue(signatureVerified, "Invalid signature");
} catch (Exception e) {
fail(e.getMessage(), e);
}
}
use of javax.crypto.IllegalBlockSizeException in project jdk8u_jdk by JetBrains.
the class CTR method runTest.
public void runTest(String algo, String mo, String pad) throws Exception {
Cipher ci = null;
byte[] iv = null;
AlgorithmParameterSpec aps = null;
SecretKey key = null;
try {
Random rdm = new Random();
byte[] plainText;
ci = Cipher.getInstance(algo + "/" + mo + "/" + pad, PROVIDER);
KeyGenerator kg = KeyGenerator.getInstance(algo, PROVIDER);
kg.init(KEY_LENGTH);
key = kg.generateKey();
for (int i = 0; i < 15; i++) {
plainText = new byte[1600 + i + 1];
rdm.nextBytes(plainText);
if (!mo.equalsIgnoreCase("GCM")) {
ci.init(Cipher.ENCRYPT_MODE, key, aps);
} else {
ci.init(Cipher.ENCRYPT_MODE, key);
}
byte[] cipherText = new byte[ci.getOutputSize(plainText.length)];
int offset = ci.update(plainText, 0, plainText.length, cipherText, 0);
ci.doFinal(cipherText, offset);
if (!mo.equalsIgnoreCase("ECB")) {
iv = ci.getIV();
aps = new IvParameterSpec(iv);
} else {
aps = null;
}
if (!mo.equalsIgnoreCase("GCM")) {
ci.init(Cipher.DECRYPT_MODE, key, aps);
} else {
ci.init(Cipher.DECRYPT_MODE, key, ci.getParameters());
}
byte[] recoveredText = new byte[ci.getOutputSize(cipherText.length)];
int len = ci.doFinal(cipherText, 0, cipherText.length, recoveredText);
byte[] tmp = new byte[len];
for (int j = 0; j < len; j++) {
tmp[j] = recoveredText[j];
}
Arrays.toString(plainText);
if (!java.util.Arrays.equals(plainText, tmp)) {
System.out.println("Original: ");
dumpBytes(plainText);
System.out.println("Recovered: ");
dumpBytes(tmp);
throw new RuntimeException("Original text is not equal with recovered text, with mode:" + mo);
}
}
} catch (NoSuchAlgorithmException | NoSuchProviderException | NoSuchPaddingException | InvalidKeyException | InvalidAlgorithmParameterException | ShortBufferException | IllegalBlockSizeException | BadPaddingException e) {
System.out.println("Test failed!");
throw e;
}
}
use of javax.crypto.IllegalBlockSizeException in project jdk8u_jdk by JetBrains.
the class CipherInputStreamExceptions method cbc_readAllIllegalBlockSize.
/* Check that exception is thrown when message is fully read
* This test:
* 1) Encrypts a 96 byte message with AES/CBC/PKCS5Padding
* 2) Create a stream that sends 95 bytes.
* 3) Read stream to the end
* 4) Expect IllegalBlockSizeException thrown
*/
static void cbc_readAllIllegalBlockSize() throws Exception {
byte[] read = new byte[200];
System.out.println("Running cbc_readAllIllegalBlockSize test");
// Encrypt 96 byte with AES/CBC/PKCS5Padding
byte[] ct = encryptedText("CBC", 96);
// Create a stream with only 95 bytes of encrypted data
CipherInputStream in = getStream("CBC", ct, 95);
try {
int s, size = 0;
while ((s = in.read(read)) != -1) {
size += s;
}
throw new RuntimeException("Fail: No IllegalBlockSizeException. " + "CipherInputStream.read() returned " + size);
} catch (IOException e) {
Throwable ec = e.getCause();
if (ec instanceof IllegalBlockSizeException) {
System.out.println(" Pass.");
} else {
System.out.println(" Fail: " + ec.getMessage());
throw new RuntimeException(ec);
}
}
}
use of javax.crypto.IllegalBlockSizeException in project midpoint by Evolveum.
the class ProtectorImpl method decryptBytes.
@Override
protected <T> byte[] decryptBytes(ProtectedData<T> protectedData) throws SchemaException, EncryptionException {
EncryptedDataType encryptedDataType = protectedData.getEncryptedDataType();
EncryptionMethodType encryptionMethodType = encryptedDataType.getEncryptionMethod();
if (encryptionMethodType == null) {
throw new SchemaException("No encryptionMethod element in protected data");
}
String algorithmUri = encryptionMethodType.getAlgorithm();
if (StringUtils.isBlank(algorithmUri)) {
throw new SchemaException("No algorithm URI in encryptionMethod element in protected data");
}
KeyInfoType keyInfo = encryptedDataType.getKeyInfo();
if (keyInfo == null) {
throw new SchemaException("No keyInfo element in protected data");
}
String keyName = keyInfo.getKeyName();
if (StringUtils.isBlank(keyName)) {
throw new SchemaException("No keyName defined in keyInfo element in protected data");
}
SecretKey key = getSecretKeyByDigest(keyName);
CipherDataType cipherData = encryptedDataType.getCipherData();
if (cipherData == null) {
throw new SchemaException("No cipherData element in protected data");
}
byte[] encryptedBytes = cipherData.getCipherValue();
if (encryptedBytes == null || encryptedBytes.length == 0) {
throw new SchemaException("No cipherValue in cipherData element in protected data");
}
byte[] decryptedData;
try {
decryptedData = decryptBytes(encryptedBytes, algorithmUri, key);
} catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
throw new EncryptionException(e.getMessage(), e);
}
return decryptedData;
}
Aggregations