@Test public void encryptAndDecrypt() throws GeneralSecurityException, InvalidCipherTextException { for (TestIdentity testData: testIdentities) for (byte[] original: testMessages) { CryptoImplementation cryptoImpl = testData.cryptoImpl; KeyPair encryptionKeys = testData.encryptionKeys; byte[] encrypted = cryptoImpl.encrypt(original, encryptionKeys.getPublic()); byte[] decrypted = cryptoImpl.decrypt(encrypted, encryptionKeys.getPublic(), encryptionKeys.getPrivate()); assertArrayEquals("encrypted data != decrypted data for crypto implementation <" + cryptoImpl.getName() + ">", original, decrypted); } }
/** * Decrypts the encrypted part of the packet with the private key of an <code>EmailIdentity</code>. * The {@link CryptoImplementation} in the <code>EmailIdentity</code> must be the same as the one * in this <code>EncryptedEmailPacket</code>. * @param identity * @throws GeneralSecurityException * @throws InvalidCipherTextException */ public UnencryptedEmailPacket decrypt(EmailIdentity identity) throws GeneralSecurityException { if (cryptoImpl != identity.getCryptoImpl()) throw new IllegalArgumentException("CryptoImplementations don't match. Email Packet: <" + cryptoImpl.getName() + ">, Email Identity: <" + identity.getCryptoImpl().getName() + ">."); byte[] decryptedData = cryptoImpl.decrypt(encryptedData, identity.getPublicEncryptionKey(), identity.getPrivateEncryptionKey()); return new UnencryptedEmailPacket(decryptedData); }