/** * Asymmetrically encrypts content inheriting from {@link NetworkContent}. A default key length * will be used. * * @param content the content to be encrypted. * @param publicKey The asymmetric public key with which the content will be encrypted * @param keyLength the strength of the encryption * @return the encrypted content * @throws DataLengthException * @throws InvalidKeyException * @throws IllegalStateException * @throws InvalidCipherTextException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws IOException */ public static HybridEncryptedContent encryptHybrid(NetworkContent content, PublicKey publicKey) throws DataLengthException, InvalidKeyException, IllegalStateException, InvalidCipherTextException, IllegalBlockSizeException, BadPaddingException, IOException { byte[] serialized = EncryptionUtil.serializeObject(content); HybridEncryptedContent encryptHybrid = EncryptionUtil.encryptHybrid(serialized, publicKey, H2HConstants.KEYLENGTH_HYBRID_AES); encryptHybrid.setTimeToLive(content.getTimeToLive()); return encryptHybrid; }
/** * Symmetrically encrypts content inheriting from {@link NetworkContent} by means of the AES * algorithm. The content first gets serialized, then encrypted. * * @param content the content to be encrypted. Can be of any type that extends {@link * NetworkContent}. * @param aesKey The symmetric key with which the content will be encrypted. * @return EncryptedContent which contains the encrypted byte[] content as well as the AES * initialization vector (IV). * @throws InvalidCipherTextException * @throws IllegalStateException * @throws DataLengthException * @throws IOException */ public static EncryptedNetworkContent encryptAES(NetworkContent content, SecretKey aesKey) throws DataLengthException, IllegalStateException, InvalidCipherTextException, IOException { byte[] serialized = EncryptionUtil.serializeObject(content); byte[] initVector = EncryptionUtil.generateIV(); byte[] encryptedContent = EncryptionUtil.encryptAES(serialized, aesKey, initVector); EncryptedNetworkContent encryptedNetworkContent = new EncryptedNetworkContent(encryptedContent, initVector); encryptedNetworkContent.setTimeToLive(content.getTimeToLive()); return encryptedNetworkContent; }