@Override public String toString() { return super.toString() + ", DHTkey=" + dhtKey + ", tstamp=" + storeTime + ", alg=" + cryptoImpl.getName() + ", delVerifHash=" + delVerificationHash + ", encrLen=" + encryptedData.length; } }
/** * 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); }
@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); } }
private TestIdentity(String base64PublicKeyPair, String base64PrivateKeyPair, CryptoImplementation cryptoImpl) throws GeneralSecurityException { this.base64PublicKeyPair = base64PublicKeyPair; this.base64PrivateKeyPair = base64PrivateKeyPair; this.cryptoImpl = cryptoImpl; publicKeys = cryptoImpl.createPublicKeyPair(base64PublicKeyPair); privateKeys = cryptoImpl.createPrivateKeyPair(base64PrivateKeyPair); signingKeys = new KeyPair(publicKeys.signingKey, privateKeys.signingKey); encryptionKeys = new KeyPair(publicKeys.encryptionKey, privateKeys.encryptionKey); identity = new EmailIdentity(base64PublicKeyPair + base64PrivateKeyPair); identity.setPublicName(cryptoImpl.getName()); } }
@Test public void testEncryptionDecryption() throws Exception { for (int i=0; i<encryptedPackets.length; i++) { EncryptedEmailPacket packet = encryptedPackets[i]; UnencryptedEmailPacket decryptedPacket = packet.decrypt(identities[i]); byte[] arrayA = decryptedPacket.getContent(); byte[] arrayB = message.getBytes(); assertArrayEquals("Email message differs after decryption! CryptoImplementation = " + packet.getCryptoImpl().getName(), arrayA, arrayB); } }
@Test public void signAndVerify() throws GeneralSecurityException, IOException, PasswordException { for (TestIdentity testIdentity: testIdentities) { KeyUpdateHandler keyUpdateHandler; if (testIdentity.cryptoImpl instanceof NTRUEncrypt1087_GMSS512) keyUpdateHandler = TestUtil.createVerifyingKeyUpdateHandler(testMessages.size()); // verify that KeyUpdateHandler is called once for each signed message else keyUpdateHandler = TestUtil.createDummyKeyUpdateHandler(); for (byte[] message: testMessages) { CryptoImplementation cryptoImpl = testIdentity.cryptoImpl; KeyPair signingKeys = testIdentity.signingKeys; byte[] signature = cryptoImpl.sign(message, signingKeys.getPrivate(), keyUpdateHandler); assertTrue("Invalid signature for crypto implementation <" + cryptoImpl.getName() + ">", cryptoImpl.verify(message, signature, signingKeys.getPublic())); } } } }
@Test public void testHash() throws Exception { for (EncryptedEmailPacket packet: encryptedPackets) { assertTrue("Hash not valid! CryptoImplementation = " + packet.getCryptoImpl().getName(), packet.verifyPacketHash()); alterEncryptedData(packet); assertFalse("Hash is valid, but should be invalid! CryptoImplementation = " + packet.getCryptoImpl().getName(), packet.verifyPacketHash()); } }
@Test public void toByteArrayAndBack() throws Exception { for (EncryptedEmailPacket packet: encryptedPackets) { byte[] arrayA = packet.toByteArray(); byte[] arrayB = new EncryptedEmailPacket(arrayA).toByteArray(); assertArrayEquals("The two arrays differ! CryptoImplementation = " + packet.getCryptoImpl().getName(), arrayA, arrayB); } }