Java Signature initSign() method with Examples

initSign(PrivateKey privateKey)

The initSign() method of java.security.Provider class is used to initialize this object for signing. If this method is called again with a different argument, it negates the effect of this call.

Syntax:

public final void 
    initSign(PrivateKey privateKey) 
        throws InvalidKeyException

Parameters: This method takes the private key of the identity as parameter whose signature is going to be generated

Exception: This method throws InvalidKeyException if the key is invalid.

Below are the examples to illustrate the initSign() method:

Note: The following program will not run in online IDE

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// initSign() method
  
import java.security.*;
import java.util.*;
import sun.misc.BASE64Encoder;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // calling getKeyPair() method and assining in keypair
            KeyPair keyPair = getKeyPair();
  
            // creating byte array object
            byte[] outbuff = new byte[1000];
  
            // data to be updated
            byte[] data = "test".getBytes("UTF8");
  
            // creating the object of Signature
            Signature sr = Signature.getInstance("SHA1WithRSA");
  
            // initializing the signature object with key pair
            // for signing
            // Using method initSign
            sr.initSign(keyPair.getPrivate());
  
            // updating the data
            sr.update(data);
  
            // getting the signature byte
            // of an signing operation
            // by using method sign()
            byte[] bytes = sr.sign();
  
            // pritning the number of byte
            System.out.println("Signature:" + Arrays.toString(bytes));
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (SignatureException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
  
    // defining getKeyPair method
    private static KeyPair getKeyPair() throws NoSuchAlgorithmException
    {
  
        // creating the object of KeyPairGenerator
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  
        // initializing with 1024
        kpg.initialize(1024);
  
        // returning the key pairs
        return kpg.genKeyPair();
    }
}

chevron_right


Output:

Signature:[-109, -21, 90, -114, -22,
....
....
...., 92, 1]

Example 2: To show InvalidKeyException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// initSign() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // data to be updated
            byte[] data = "test".getBytes("UTF8");
  
            // creating the object of Signature
            Signature sr = Signature.getInstance("SHA1WithRSA");
  
            // initializing the signature object with key pair
            // for signing
            // Using method initSign
            System.out.println("Trying to put null as private key ");
            sr.initSign(null);
  
            // updating the data
            sr.update(data);
  
            // getting the signature byte
            // of an signing operation
            // by using method sign()
            byte[] bytes = sr.sign();
  
            // pritning the number of byte
            System.out.println("Signature:" + Arrays.toString(bytes));
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (SignatureException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (InvalidKeyException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Trying to put null as private key 
Exception thrown : java.security.InvalidKeyException: Key must not be null

initSign(PrivateKey privateKey, SecureRandom random)

The initSign() method of java.security.Provider class is used to initialize this object for signing. If this method is called again with a different argument, it negates the effect of this call.

Syntax:

public final void 
    initSign(PrivateKey privateKey, SecureRandom random)
        throws InvalidKeyException

Parameters: This method takes following as a parameters:

  • privateKey– the private key of the identity whose signature is going to be generated.
  • random– the source of randomness for this signature.

Exception: This method throws InvalidKeyException if the key is invalid.

Below are the examples to illustrate the sign() method:

Note: The following program will not run in online IDE

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// sign() method
  
import java.security.*;
import java.util.*;
import sun.misc.BASE64Encoder;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // calling getKeyPair() method and assining in keypair
            KeyPair keyPair = getKeyPair();
  
            // creating byte array object
            byte[] outbuff = new byte[1000];
  
            // data to be updated
            byte[] data = "test".getBytes("UTF8");
  
            // creating the object of Signature
            Signature sr = Signature.getInstance("SHA1WithRSA");
  
            // creating the object of SecureRandom
            SecureRandom sb = SecureRandom.getInstance("SHA1PRNG");
  
            // initializing the signature object with key pair
            // for signing
            sr.initSign(keyPair.getPrivate(), sb);
  
            // updating the data
            sr.update(data);
  
            // getting the signature byte
            // of an signing operation
            // by using method sign()
            byte[] bytes = sr.sign();
  
            // pritning the number of byte
            System.out.println("Signature:" + Arrays.toString(bytes));
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (SignatureException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
  
    // defining getKeyPair method
    private static KeyPair getKeyPair() throws NoSuchAlgorithmException
    {
  
        // creating the object of KeyPairGenerator
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
  
        // initializing with 1024
        kpg.initialize(1024);
  
        // returning the key pairs
        return kpg.genKeyPair();
    }
}

chevron_right


Output:

Signature:[-121, -82, ....
....
....104, 114, -67]

Example 2: To show InvalidKeyException

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// initSign() method
  
import java.security.*;
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // creating byte array object
            byte[] outbuff = new byte[1000];
  
            // data to be updated
            byte[] data = "test".getBytes("UTF8");
  
            // creating the object of Signature
            Signature sr = Signature.getInstance("SHA1WithRSA");
  
            // creating the object of SecureRandom
            SecureRandom sb = SecureRandom.getInstance("SHA1PRNG");
  
            // initializing the signature object with null key pair
            // for signing using initSign() method
            System.out.println("Trying to put the null as key");
            sr.initSign(null, sb);
  
            // updating the data
            sr.update(data);
  
            // getting the signature byte
            // of an signing operation
            // by using method sign()
            byte[] bytes = sr.sign();
  
            // pritning the number of byte
            System.out.println("Signature:" + Arrays.toString(bytes));
        }
  
        catch (NoSuchAlgorithmException e) {
  
            System.out.println("Exception thrown : " + e);
        }
        catch (InvalidKeyException e) {
  
            System.out.println("Exception thrown : " + e);
        }
    }
}

chevron_right


Output:

Trying to put the null as key
Exception thrown : java.security.InvalidKeyException: Key must not be null


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.