@Test public void testEncryptDecrypt() throws IOException, GeneralSecurityException { PublicKeyVerify senderVerifier = TestUtils.createTestSenderVerifier(); PrivateKey recipientPrivateKey = TestUtils.createTestRsaPrivateKey(); for (Padding padding : Padding.values()) { for (int plaintextSize : new int[]{64, 128, 256, 512, 1048}) { byte[] plaintext = Random.randBytes(plaintextSize); HybridEncrypt hybridEncrypt = new RsaEcdsaHybridEncrypt.Builder() .withSenderSigner(senderSigner) .withRecipientPublicKey(recipientPublicKey) .withPadding(padding) .build(); byte[] ciphertext = hybridEncrypt.encrypt(plaintext, null); HybridDecrypt hybridDecrypt = new RsaEcdsaHybridDecrypt.Builder() .withSenderVerifier(senderVerifier) .withRecipientPrivateKey(recipientPrivateKey) .withPadding(padding) .build(); byte[] decryptedPlaintext = hybridDecrypt.decrypt(ciphertext, null); assertArrayEquals(plaintext, decryptedPlaintext); } } }
@Override public byte[] encrypt(byte[] plaintext, byte[] contextInfo ) throws GeneralSecurityException { if (contextInfo != null) { throw new GeneralSecurityException("contextInfo must be null because it is unused"); } try { byte[] unsignedCiphertext = HybridRsaUtils.encrypt(plaintext, recipientPublicKey, padding, oaepParameterSpec); return signAndSerialize(unsignedCiphertext); } catch (IOException e) { throw new GeneralSecurityException("encryption failed", e); } }
RsaEcdsaHybridEncrypt implements HybridEncrypt { @Override public byte[] encrypt(byte[] plaintext, byte[] contextInfo ) throws GeneralSecurityException { if (contextInfo != null) { throw new GeneralSecurityException("contextInfo must be null because it is unused"); } try { byte[] unsignedCiphertext = HybridRsaUtils.encrypt(plaintext, recipientPublicKey, padding, oaepParameterSpec); return signAndSerialize(unsignedCiphertext); } catch (IOException e) { throw new GeneralSecurityException("encryption failed", e); } } }
RsaEcdsaHybridEncrypt implements HybridEncrypt { @Override public byte[] encrypt(byte[] plaintext, byte[] contextInfo ) throws GeneralSecurityException { if (contextInfo != null) { throw new GeneralSecurityException("contextInfo must be null because it is unused"); } try { byte[] unsignedCiphertext = HybridRsaUtils.encrypt(plaintext, recipientPublicKey, padding, oaepParameterSpec); return signAndSerialize(unsignedCiphertext); } catch (IOException e) { throw new GeneralSecurityException("encryption failed", e); } } private  RsaEcdsaHybridEncrypt(Builder builder); }
RsaEcdsaHybridEncrypt implements HybridEncrypt { @Override public byte[] encrypt(byte[] plaintext, byte[] contextInfo ) throws GeneralSecurityException { if (contextInfo != null) { throw new GeneralSecurityException("contextInfo must be null because it is unused"); } try { byte[] unsignedCiphertext = HybridRsaUtils.encrypt(plaintext, recipientPublicKey, padding, oaepParameterSpec); return signAndSerialize(unsignedCiphertext); } catch (IOException e) { throw new GeneralSecurityException("encryption failed", e); } } private  RsaEcdsaHybridEncrypt(Builder builder); @Override byte[] encrypt(byte[] plaintext, byte[] contextInfo /* unused */); }
RsaEcdsaHybridEncrypt implements HybridEncrypt { @Override public byte[] encrypt(byte[] plaintext, byte[] contextInfo ) throws GeneralSecurityException { if (contextInfo != null) { throw new GeneralSecurityException("contextInfo must be null because it is unused"); } try { byte[] unsignedCiphertext = HybridRsaUtils.encrypt(plaintext, recipientPublicKey, padding, oaepParameterSpec); return signAndSerialize(unsignedCiphertext); } catch (IOException e) { throw new GeneralSecurityException("encryption failed", e); } } private  RsaEcdsaHybridEncrypt(Builder builder); @Override byte[] encrypt(byte[] plaintext, byte[] contextInfo /* unused */);  }