private byte[] encrypt(KeysetHandle publicKeysetHandle) throws GeneralSecurityException { HybridEncrypt hybridEncrypt = HybridEncryptFactory.getPrimitive(publicKeysetHandle); return hybridEncrypt.encrypt(INITIAL_TEXT.getBytes(), ASSOCIATED_DATA.getBytes()); }
private byte[] encrypt(KeysetHandle publicKeysetHandle) throws GeneralSecurityException { HybridEncrypt hybridEncrypt = HybridEncryptFactory.getPrimitive(publicKeysetHandle); return hybridEncrypt.encrypt(INITIAL_TEXT.getBytes(), ASSOCIATED_DATA.getBytes()); }
private byte[] encrypt(KeysetHandle publicKeysetHandle) throws GeneralSecurityException { HybridEncrypt hybridEncrypt = HybridEncryptFactory.getPrimitive(publicKeysetHandle); return hybridEncrypt.encrypt(INITIAL_TEXT.getBytes(), ASSOCIATED_DATA.getBytes()); }
@Override public byte[] encrypt(final byte[] plaintext, final byte[] contextInfo) throws GeneralSecurityException { return Bytes.concat( primitives.getPrimary().getIdentifier(), primitives.getPrimary().getPrimitive().encrypt(plaintext, contextInfo)); } };
/** * Encryptes the given plaintext into a Capillary ciphertext. * * @param data the plaintext. * @return the Capillary ciphertext. * @throws GeneralSecurityException if the encryption fails. */ public synchronized byte[] encrypt(byte[] data) throws GeneralSecurityException { if (!isLoaded) { throw new GeneralSecurityException("public key is not loaded"); } byte[] ciphertext = encrypter.encrypt(data, null); return CapillaryCiphertext.newBuilder() .setKeychainUniqueId(capillaryPublicKey.getKeychainUniqueId()) .setKeySerialNumber((capillaryPublicKey.getSerialNumber())) .setIsAuthKey(capillaryPublicKey.getIsAuth()) .setCiphertext(ByteString.copyFrom(ciphertext)) .build().toByteArray(); }
/** * Encryptes the given plaintext into a Capillary ciphertext. * * @param data the plaintext. * @return the Capillary ciphertext. * @throws GeneralSecurityException if the encryption fails. */ public synchronized byte[] encrypt(byte[] data) throws GeneralSecurityException { if (!isLoaded) { throw new GeneralSecurityException("public key is not loaded"); } byte[] ciphertext = encrypter.encrypt(data, null); return CapillaryCiphertext.newBuilder() .setKeychainUniqueId(capillaryPublicKey.getKeychainUniqueId()) .setKeySerialNumber((capillaryPublicKey.getSerialNumber())) .setIsAuthKey(capillaryPublicKey.getIsAuth()) .setCiphertext(ByteString.copyFrom(ciphertext)) .build().toByteArray(); }
@Test public void testNonNullContextInfo() { HybridEncrypt hybridEncrypt = new RsaEcdsaHybridEncrypt.Builder() .withSenderSigner(senderSigner) .withRecipientPublicKey(recipientPublicKey) .withPadding(Padding.PKCS1) .build(); byte[] plaintext = Random.randBytes(20); byte[] contextInfo = new byte[0]; try { hybridEncrypt.encrypt(plaintext, contextInfo); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException ex) { // This is expected. } } }
.withPadding(padding) .build(); byte[] ciphertext = hybridEncrypt.encrypt(plaintext, null);
@Test public void testNonNullContextInfo() throws GeneralSecurityException { HybridEncrypt hybridEncrypt = new RsaEcdsaHybridEncrypt.Builder() .withSenderSigner(senderSigner) .withRecipientPublicKey(recipientPublicKey) .withPadding(Padding.PKCS1) .build(); HybridDecrypt hybridDecrypt = new RsaEcdsaHybridDecrypt.Builder() .withSenderVerifier(senderVerifier) .withRecipientPrivateKey(recipientPrivateKey) .withPadding(Padding.PKCS1) .build(); byte[] plaintext = Random.randBytes(20); byte[] ciphertext = hybridEncrypt.encrypt(plaintext, null); byte[] contextInfo = new byte[0]; try { hybridDecrypt.decrypt(ciphertext, contextInfo); fail("Expected GeneralSecurityException"); } catch (GeneralSecurityException ex) { // This is expected. } } }
@Test public void testEncryptDecrypt() throws IOException, GeneralSecurityException { PublicKeyVerify senderVerifier = TestUtils.createTestSenderVerifier(); PrivateKey recipientPrivateKey = TestUtils.createTestRsaPrivateKey(); // Try Encryption/Decryption for each padding mode. for (Padding padding : Padding.values()) { // Try Encryption/Decryption for plaintext sizes 64, 128, 256, 512, and 1048 bytes. 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); } } }