Skip to content

Commit 90f3286

Browse files
Add a test for trying to decrypt without a private key
1 parent 63bf389 commit 90f3286

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/test/java/com/amazonaws/encryptionsdk/keyrings/RawRsaKeyringTest.java

+26
Original file line numberDiff line numberDiff line change
@@ -153,4 +153,30 @@ void testEncryptWithNoPublicKey() throws Exception {
153153
assertThrows(AwsCryptoException.class, () -> noPublicKey.onEncrypt(encryptionMaterials));
154154
}
155155

156+
@Test
157+
void testDecryptWithNoPrivateKey() throws Exception {
158+
final KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
159+
keyPairGenerator.initialize(2048);
160+
final KeyPair keyPair = keyPairGenerator.generateKeyPair();
161+
162+
Keyring noPrivateKey = new RawRsaKeyring(KEYNAMESPACE, KEYNAME, keyPair.getPublic(), null, PADDING_SCHEME);
163+
164+
EncryptionMaterials encryptionMaterials = EncryptionMaterials.newBuilder()
165+
.setAlgorithm(ALGORITHM)
166+
.setCleartextDataKey(DATA_KEY)
167+
.setEncryptionContext(ENCRYPTION_CONTEXT)
168+
.build();
169+
170+
encryptionMaterials = noPrivateKey.onEncrypt(encryptionMaterials);
171+
172+
DecryptionMaterials decryptionMaterials = DecryptionMaterials.newBuilder()
173+
.setAlgorithm(ALGORITHM)
174+
.setEncryptionContext(ENCRYPTION_CONTEXT)
175+
.build();
176+
177+
DecryptionMaterials resultDecryptionMaterials = noPrivateKey.onDecrypt(decryptionMaterials, encryptionMaterials.getEncryptedDataKeys());
178+
179+
assertEquals(decryptionMaterials, resultDecryptionMaterials);
180+
}
181+
156182
}

0 commit comments

Comments
 (0)