/** * Looks up an {@link EmailIdentity} that has the same public encryption key and the * same public signing key as a given {@link EmailDestination}.<br/> * Returns <code>null</code> if nothing is found. * @param destination * @throws PasswordException * @throws GeneralSecurityException * @throws IOException */ public EmailIdentity get(EmailDestination destination) throws PasswordException, IOException, GeneralSecurityException { initializeIfNeeded(); if (identities == null) return null; for (EmailIdentity identity: identities) if (identity.getPublicEncryptionKey().equals(destination.getPublicEncryptionKey()) && identity.getPublicSigningKey().equals(destination.getPublicSigningKey())) return identity; return null; }
/** * 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); }