@Test
 public void testEncryptCustomKeyEntry() {
   String keyEntry = "keyEntry";
   AsymmetricCrypto asymmetricCrypto =
       new AsymmetricCrypto(
           SecurityProviderConnectorFactory.getSecurityProviderConnector(), keyEntry);
   Certificates certificates =
       new Certificates(
           SecurityProviderConnectorFactory.getSecurityProviderConnector(),
           "issuer",
           keyEntry,
           null);
   StringWriter stringWriter = new StringWriter();
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   certificates.create("test", "123".toCharArray(), stringWriter, byteArrayOutputStream);
   String cert = stringWriter.toString();
   StringReader stringReader = new StringReader(cert);
   ByteArrayInputStream byteArrayInputStream =
       new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
   byte[] plain = "secret".getBytes();
   byte[] encrypted = asymmetricCrypto.encrypt(plain, stringReader);
   byte[] decrypted =
       asymmetricCrypto.decrypt(encrypted, "123".toCharArray(), byteArrayInputStream);
   Assert.assertArrayEquals(plain, decrypted);
 }
 @Test(expected = IllegalArgumentException.class)
 public void testWithNullForPrivateKey()
     throws CertificateException, NoSuchAlgorithmException, KeyStoreException,
         NoSuchProviderException, UnrecoverableEntryException, IOException {
   HybridCrypto hybridCrypto =
       new HybridCrypto(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   Certificates certificates =
       new Certificates(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   StringWriter stringWriter = new StringWriter();
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   String id = "test";
   char[] password = "******".toCharArray();
   X509Certificate cert = certificates.create(id, password, stringWriter, byteArrayOutputStream);
   hybridCrypto.addRecipient("test", cert);
   String message = "I am hybrid encrypted!";
   HybridEncrypted hybridEncrypted =
       hybridCrypto.build(
           message.getBytes(),
           new PrivateKeyExtractor()
               .extractPrivateKey(
                   password, new ByteArrayInputStream(byteArrayOutputStream.toByteArray())));
   HybridCrypto freshHybridCrypto =
       new HybridCrypto(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   byte[] decryptedMessage = freshHybridCrypto.decrypt(hybridEncrypted, id, null);
   Assert.assertArrayEquals(message.getBytes(), decryptedMessage);
   Signature signature =
       new Signature(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   Assert.assertTrue(
       signature.verify(
           hybridEncrypted.getEncryptedBody(),
           hybridEncrypted.getSignature(),
           cert.getPublicKey()));
 }
 @Test(expected = IllegalArgumentException.class)
 public void testWithNullMessage() {
   HybridCrypto hybridCrypto =
       new HybridCrypto(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   Certificates certificates =
       new Certificates(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   StringWriter stringWriter = new StringWriter();
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   String id = "test";
   char[] password = "******".toCharArray();
   certificates.create(id, password, stringWriter, byteArrayOutputStream);
   String cert = stringWriter.toString();
   StringReader stringReader = new StringReader(cert);
   hybridCrypto.addRecipient("test", stringReader);
   hybridCrypto.build(
       null, password, new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
 }
 @Test
 public void testEncryptPublicPrivate() throws Exception {
   AsymmetricCrypto asymmetricCrypto =
       new AsymmetricCrypto(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   Certificates certificates =
       new Certificates(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   StringWriter stringWriter = new StringWriter();
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   X509Certificate cert =
       certificates.create("test", "123".toCharArray(), stringWriter, byteArrayOutputStream);
   ByteArrayInputStream byteArrayInputStream =
       new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
   byte[] plain = "secret".getBytes();
   byte[] encrypted = asymmetricCrypto.encrypt(plain, cert.getPublicKey());
   byte[] decrypted =
       asymmetricCrypto.decrypt(
           encrypted,
           new PrivateKeyExtractor().extractPrivateKey("123".toCharArray(), byteArrayInputStream));
   Assert.assertArrayEquals(plain, decrypted);
 }
 @Test
 public void test() {
   HybridCrypto hybridCrypto =
       new HybridCrypto(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   Certificates certificates =
       new Certificates(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   StringWriter stringWriter = new StringWriter();
   ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
   String id = "test";
   char[] password = "******".toCharArray();
   certificates.create(id, password, stringWriter, byteArrayOutputStream);
   String cert = stringWriter.toString();
   StringReader stringReader = new StringReader(cert);
   hybridCrypto.addRecipient("test", stringReader);
   String message = "I am hybrid encrypted!";
   HybridEncrypted hybridEncrypted =
       hybridCrypto.build(
           message.getBytes(),
           password,
           new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
   HybridCrypto freshHybridCrypto =
       new HybridCrypto(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   byte[] decryptedMessage =
       freshHybridCrypto.decrypt(
           hybridEncrypted,
           id,
           password,
           new ByteArrayInputStream(byteArrayOutputStream.toByteArray()));
   Assert.assertArrayEquals(message.getBytes(), decryptedMessage);
   Signature signature =
       new Signature(SecurityProviderConnectorFactory.getSecurityProviderConnector());
   Assert.assertTrue(
       signature.verify(
           hybridEncrypted.getEncryptedBody(),
           hybridEncrypted.getSignature(),
           new StringReader(cert)));
 }