Public key signing in Java

package ch.codevo.app_signing;


import java.io.FileInputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.Base64;


public class AppSigning {


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        String cipherText = "Text to sign";
        String keyStorePath = "/opt/codevo.com.jks";
        String keyStorePassword = "changeit";
        String certificatePassword = "changeit";
        String certificateAlias = "codevo.com";

        String signedDataEncoded = null;

        try {
            signedDataEncoded = getSignedData(certificateAlias, certificatePassword, cipherText, keyStorePath, keyStorePassword);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (UnrecoverableKeyException e) {
            e.printStackTrace();
        } catch (SignatureException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        try {
            System.out.println(validateSignature(certificateAlias, cipherText, keyStorePath, signedDataEncoded, keyStorePassword));
        } catch (IOException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (CertificateException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (SignatureException e) {
            e.printStackTrace();
        }
    }

    public static String getSignedData(final String certificateAlias, final String certificatePassword, final String cipherText, final String keyStorePath, final String keyStorePassword) throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, UnrecoverableKeyException, SignatureException, InvalidKeyException {

        byte[] cipherTextBytes = cipherText.getBytes("UTF8");

        FileInputStream keystoreFile = new FileInputStream(keyStorePath);
        KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(keystoreFile, keyStorePassword.toCharArray());
        Key key = keystore.getKey(certificateAlias, certificatePassword.toCharArray());
        PrivateKey privateKey = (PrivateKey) key;

        Signature sig = null;
        sig = Signature.getInstance("MD5WithRSA");
        sig.initSign(privateKey);
        sig.update(cipherTextBytes);
        byte[] signatureBytes = sig.sign();
        String signedData = Base64.getEncoder().encodeToString(signatureBytes);

        return signedData;
    }


    public static boolean validateSignature(final String certificateAlias, final String cipherText,
                                            final String keyStorePath, final String signedDataEncoded, final String keyStorePassword) throws IOException, KeyStoreException, CertificateException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {

        boolean valid = false;
        final String alias = certificateAlias;
        final byte[] cipherTextBytes = cipherText.getBytes("UTF8");

        final FileInputStream pubKeystore = new FileInputStream(keyStorePath);
        KeyStore keystore = null;
        keystore = KeyStore.getInstance(KeyStore.getDefaultType());
        keystore.load(pubKeystore, keyStorePassword.toCharArray());

        PublicKey publicKey = null;
        publicKey = keystore.getCertificate(alias).getPublicKey();

        byte[] signedDataDecoded = null;
        if (signedDataEncoded != null) {
            signedDataDecoded = Base64.getDecoder().decode(signedDataEncoded);
        } else {
            throw new NullPointerException("SIGNATURE DATA IS MISSING");
        }
        Signature sig = null;
        sig = Signature.getInstance("MD5WithRSA");
        sig.initVerify(publicKey);
        sig.update(cipherTextBytes);
        valid = sig.verify(signedDataDecoded);

        return valid;
    }

}