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:
Java
import java.security.*;
import java.util.*;
import sun.misc.BASE64Encoder;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
KeyPair keyPair = getKeyPair();
byte [] outbuff = new byte [ 1000 ];
byte [] data = "test" .getBytes( "UTF8" );
Signature sr = Signature.getInstance( "SHA1WithRSA" );
sr.initSign(keyPair.getPrivate());
sr.update(data);
byte [] bytes = sr.sign();
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);
}
}
private static KeyPair getKeyPair() throws NoSuchAlgorithmException
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance( "RSA" );
kpg.initialize( 1024 );
return kpg.genKeyPair();
}
}
|
Output:
Signature:[-109, -21, 90, -114, -22,
....
....
...., 92, 1]
Example 2: To show InvalidKeyException
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
byte [] data = "test" .getBytes( "UTF8" );
Signature sr = Signature.getInstance( "SHA1WithRSA" );
System.out.println( "Trying to put null as private key " );
sr.initSign( null );
sr.update(data);
byte [] bytes = sr.sign();
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);
}
}
}
|
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 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:
Java
import java.security.*;
import java.util.*;
import sun.misc.BASE64Encoder;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
KeyPair keyPair = getKeyPair();
byte [] outbuff = new byte [ 1000 ];
byte [] data = "test" .getBytes( "UTF8" );
Signature sr = Signature.getInstance( "SHA1WithRSA" );
SecureRandom sb = SecureRandom.getInstance( "SHA1PRNG" );
sr.initSign(keyPair.getPrivate(), sb);
sr.update(data);
byte [] bytes = sr.sign();
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);
}
}
private static KeyPair getKeyPair() throws NoSuchAlgorithmException
{
KeyPairGenerator kpg = KeyPairGenerator.getInstance( "RSA" );
kpg.initialize( 1024 );
return kpg.genKeyPair();
}
}
|
Output:
Signature:[-121, -82, ....
....
....104, 114, -67]
Example 2: To show InvalidKeyException
Java
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
byte [] outbuff = new byte [ 1000 ];
byte [] data = "test" .getBytes( "UTF8" );
Signature sr = Signature.getInstance( "SHA1WithRSA" );
SecureRandom sb = SecureRandom.getInstance( "SHA1PRNG" );
System.out.println( "Trying to put the null as key" );
sr.initSign( null , sb);
sr.update(data);
byte [] bytes = sr.sign();
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);
}
}
}
|
Output:
Trying to put the null as key
Exception thrown : java.security.InvalidKeyException: Key must not be null
Last Updated :
27 Jan, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...