Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing asymmetric encryption method in ASAPCryptoAlgorithms #10

Open
RocketJannis opened this issue Jan 24, 2025 · 0 comments
Open

Missing asymmetric encryption method in ASAPCryptoAlgorithms #10

RocketJannis opened this issue Jan 24, 2025 · 0 comments

Comments

@RocketJannis
Copy link

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);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant