Skip to content

Missing asymmetric encryption method in ASAPCryptoAlgorithms #10

@RocketJannis

Description

@RocketJannis

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
 */
public static byte[] encryptAsymmetric(byte[] unencryptedBytes, CharSequence recipient, ASAPKeyStore ASAPKeyStore) throws ASAPSecurityException {
    PublicKey publicKey = ASAPKeyStore.getPublicKey(recipient);
    if (publicKey == null) {
        throw new ASAPSecurityException("recipients' public key cannot be found");
    } else {
        try {
            Cipher cipher = Cipher.getInstance(ASAPKeyStore.getAsymmetricEncryptionAlgorithm());
            cipher.init(1, publicKey);
            return cipher.doFinal(unencryptedBytes);
        } catch (InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException |
                 NoSuchAlgorithmException e) {
            throw new ASAPSecurityException("problems when encrypting", e);
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions