/** * @param cipherSuite * The Ciphersuite for which the BulkCipherAlgorithm should be * returned * @return The BulkCipherAlgorithm of the Cipher */ public static BulkCipherAlgorithm getBulkCipherAlgorithm(CipherSuite cipherSuite) { return BulkCipherAlgorithm.getBulkCipherAlgorithm(cipherSuite); }
public static byte[] decrypt(CipherAlgorithm cipherAlgorithm, byte[] ciphertext, byte[] key, byte[] iv) throws CryptoException { byte[] result = new byte[0]; try { Cipher cipher = Cipher.getInstance(cipherAlgorithm.getJavaName()); BulkCipherAlgorithm bulkcipher = BulkCipherAlgorithm.getBulkCipherAlgorithm(cipherAlgorithm); SecretKeySpec secretkey = new SecretKeySpec(key, bulkcipher.getJavaName()); IvParameterSpec ivspec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, secretkey, ivspec); result = cipher.doFinal(ciphertext); result = removePadding(result); } catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException ex) { LOGGER.warn("Encountered exception while encrypting the StatePlaintext with " + cipherAlgorithm.name()); LOGGER.debug(ex); throw new CryptoException("Error while StatePlaintext Decryption. See Debug-Log for more Information."); } return result; }
public static byte[] encrypt(CipherAlgorithm cipherAlgorithm, byte[] plaintextUnpadded, byte[] key, byte[] iv) throws CryptoException { byte[] result = new byte[0]; try { byte[] plaintext = addPadding(plaintextUnpadded, cipherAlgorithm.getKeySize()); Cipher cipher = Cipher.getInstance(cipherAlgorithm.getJavaName()); BulkCipherAlgorithm bulkcipher = BulkCipherAlgorithm.getBulkCipherAlgorithm(cipherAlgorithm); SecretKeySpec secretkey = new SecretKeySpec(key, bulkcipher.getJavaName()); IvParameterSpec ivspec = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, secretkey, ivspec); result = cipher.doFinal(plaintext); } catch (InvalidKeyException | InvalidAlgorithmParameterException | IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException | NoSuchAlgorithmException ex) { throw new CryptoException("Error while StatePlaintext Encryption. See Debug-Log for more Information.", ex); } return result; }