Code example for InvalidKeyException

Methods: toString

0
        } 
        catch(NoSuchAlgorithmException e) {
            System.err.println("Cannot use algorithm: "+algorithmName+": "+e.toString());
        } 
        catch(InvalidKeyException e) {
            System.err.println("Cannot use key: "+password+": "+e.toString());
        } 
    } 
     
    private static void trySignature() { 
        String algorithmName = "SHA1withRSA";
        try { 
            // generate keypair 
            KeyPair keyPair = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair(); // NoSuchAlgorithmException, NoSuchProviderException
            PrivateKey privateKey = keyPair.getPrivate();
            String plaintext = "This is the message being signed";
 
            // generate signature 
            Signature instance = Signature.getInstance("SHA1withRSAEncryption", "BC"); // NoSuchAlgorithmException, NoSuchProviderException
            instance.initSign(privateKey); // InvalidKeyException