/** * Build an <code>AlgorithmParameterSpec</code> instance used to initialize a <code>Cipher</code> instance * for block cipher encryption and decryption. * * @param algorithm the XML encryption algorithm URI * @param iv the initialization vector * @return the newly constructed AlgorithmParameterSpec instance, appropriate for the * specified algorithm */ private AlgorithmParameterSpec constructBlockCipherParameters(String algorithm, byte[] iv) { return XMLCipherUtil.constructBlockCipherParameters(algorithm, iv, this.getClass()); }
public static AlgorithmParameterSpec constructBlockCipherParameters(boolean gcmAlgorithm, byte[] iv, Class<?> callingClass) { if (gcmAlgorithm) { return constructBlockCipherParametersForGCMAlgorithm("AES/GCM/NoPadding", iv, callingClass); } else { LOG.debug("Saw non-AES-GCM mode block cipher, returning IvParameterSpec"); return new IvParameterSpec(iv); } }
private void initializeCipher() throws IOException { AlgorithmParameterSpec iv = XMLCipherUtil.constructBlockCipherParameters(cipher.getAlgorithm().toUpperCase().contains("GCM"), this.getIv(), this.getClass()); try { cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); } catch (InvalidKeyException e) { throw new IOException(e); } catch (InvalidAlgorithmParameterException e) { throw new IOException(e); } }
/** * Build an <code>AlgorithmParameterSpec</code> instance used to initialize a <code>Cipher</code> instance * for block cipher encryption and decryption. * * @param algorithm the XML encryption algorithm URI * @param iv the initialization vector * @return the newly constructed AlgorithmParameterSpec instance, appropriate for the * specified algorithm */ public static AlgorithmParameterSpec constructBlockCipherParameters(String algorithm, byte[] iv, Class<?> callingClass) { if (EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES128_GCM.equals(algorithm) || EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES192_GCM.equals(algorithm) || EncryptionConstants.ALGO_ID_BLOCKCIPHER_AES256_GCM.equals(algorithm)) { return constructBlockCipherParametersForGCMAlgorithm(algorithm, iv, callingClass); } else { LOG.debug("Saw non-AES-GCM mode block cipher, returning IvParameterSpec: {}", algorithm); return new IvParameterSpec(iv); } }
private void initCipher() throws IOException { int ivLen = JCEMapper.getIVLengthFromURI(encAlgo) / 8; byte[] ivBytes = new byte[ivLen]; int read = super.in.read(ivBytes, 0, ivLen); while (read != ivLen) { read += super.in.read(ivBytes, read, ivLen - read); } AlgorithmParameterSpec paramSpec = XMLCipherUtil.constructBlockCipherParameters(encAlgo, ivBytes, AttachmentUtils.class); try { cipher.init(Cipher.DECRYPT_MODE, key, paramSpec); } catch (InvalidKeyException | InvalidAlgorithmParameterException e) { throw new IOException(e); } }
private Cipher createCipher(String encryptionAlgorithm, SecretKey secretKey) throws WSSecurityException { String jceAlgorithm = JCEMapper.translateURItoJCEID(encryptionAlgorithm); try { Cipher cipher = Cipher.getInstance(jceAlgorithm); int ivLen = JCEMapper.getIVLengthFromURI(encryptionAlgorithm) / 8; byte[] iv = XMLSecurityConstants.generateBytes(ivLen); AlgorithmParameterSpec paramSpec = XMLCipherUtil.constructBlockCipherParameters(encryptionAlgorithm, iv, Encryptor.class); cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); return cipher; } catch (Exception e) { throw new WSSecurityException(WSSecurityException.ErrorCode.FAILED_ENCRYPTION, e); } }
byte[] iv = XMLSecurityConstants.generateBytes(ivLen); AlgorithmParameterSpec parameterSpec = XMLCipherUtil.constructBlockCipherParameters(encryptionSymAlgorithm, iv, this.getClass()); symmetricCipher.init(Cipher.ENCRYPT_MODE, encryptionPartDef.getSymmetricKey(), parameterSpec);
byte[] iv = XMLSecurityConstants.generateBytes(ivLen); AlgorithmParameterSpec paramSpec = XMLCipherUtil.constructBlockCipherParameters(encryptionSymAlgorithm, iv, this.getClass()); cipher.init(Cipher.ENCRYPT_MODE, encryptionPartDef.getSymmetricKey(), paramSpec);