The class ASAPCryptoAlgorithms is missing a useful method for encrypting bytes using asymmetric encryption without using the encrypted message.
Here is an example implementation:
/** * Uses asymmetric encryption to encrypt the unencryptedBytes using the public key of the recipient. * @param unencryptedBytes bytes to encrypt * @param recipient identifier of the recipient * @param ASAPKeyStore key store that holds the public key of the recipient * @return encrypted bytes * @throws ASAPSecurityException if the public key is missing or other problems occurred during encryption */publicstaticbyte[] encryptAsymmetric(byte[] unencryptedBytes, CharSequencerecipient, ASAPKeyStoreASAPKeyStore) throwsASAPSecurityException {
PublicKeypublicKey = ASAPKeyStore.getPublicKey(recipient);
if (publicKey == null) {
thrownewASAPSecurityException("recipients' public key cannot be found");
} else {
try {
Ciphercipher = Cipher.getInstance(ASAPKeyStore.getAsymmetricEncryptionAlgorithm());
cipher.init(1, publicKey);
returncipher.doFinal(unencryptedBytes);
} catch (InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException |
NoSuchAlgorithmExceptione) {
thrownewASAPSecurityException("problems when encrypting", e);
}
}
}
The class
ASAPCryptoAlgorithmsis missing a useful method for encrypting bytes using asymmetric encryption without using the encrypted message.Here is an example implementation: