📜  Java Signature sign() 方法和示例

📅  最后修改于: 2022-05-13 01:55:14.401000             🧑  作者: Mango

Java Signature sign() 方法和示例

符号(字节 [] 数据缓冲区,整数偏移量,整数长度)

Java.security.Provider类的sign()方法用于完成签名操作,并将生成的签名字节存储在提供的缓冲区dataBuffer 中,从offset 开始。签名的格式取决于底层的签名方案。
此签名对象被重置为其初始状态(在调用其中一个 initSign 方法后所处的状态),并且可以重用以生成具有相同私钥的进一步签名。
句法:

public final int sign( byte[] data, int offset, int length )

异常:如果此签名对象未正确初始化或此签名算法无法处理提供的输入数据,则此方法将引发SignatureException
以下是说明sign()方法的示例:
注意:以下程序不会在在线 IDE 中运行
示例 1:

Java
// 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 assigning 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
            sr.initSign(keyPair.getPrivate());
 
            // updating the data
            sr.update(data);
 
            // getting the number of bytes
            // placed in outbuffer
            // by using method sign()
            int bytes = sr.sign(outbuff, 0, 550);
 
            // printing the number of byte
            System.out.println("Signature:" + 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();
    }
}


Java
// Java program to demonstrate
// sign() 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];
 
            // creating the object of Signature
            Signature sr
                = Signature.getInstance("SHA1WithRSA");
            ;
 
            // getting the number of bytes
            // placed in outbuffer
            // by using method sign()
            int bytes = sr.sign(outbuff, 0, 550);
 
            // printing the number of byte
            System.out.println("Signature:" + bytes);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (SignatureException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


Java
// 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 assigning in
            // keypair
            KeyPair keyPair = getKeyPair();
 
            // 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
            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();
 
            // printing 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();
    }
}


Java
// Java program to demonstrate
// sign() 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];
 
            // creating the object of Signature
            Signature sr
                = Signature.getInstance("SHA1WithRSA");
            ;
 
            // getting the number of bytes
            // placed in outbuffer
            // by using method sign()
            System.out.println(
                "Trying to get"
                + " the signature byte before initializing");
            byte[] bytes = sr.sign();
 
            // printing the number of byte
            System.out.println("Signature:" + bytes);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (SignatureException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}


输出:

Signature:128

示例 2:

Java

// Java program to demonstrate
// sign() 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];
 
            // creating the object of Signature
            Signature sr
                = Signature.getInstance("SHA1WithRSA");
            ;
 
            // getting the number of bytes
            // placed in outbuffer
            // by using method sign()
            int bytes = sr.sign(outbuff, 0, 550);
 
            // printing the number of byte
            System.out.println("Signature:" + bytes);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (SignatureException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出:

Exception thrown : java.security.SignatureException: object not initialized for signing

符号()

Java.security.Provider类的sign()方法用于返回所有更新数据的签名字节。签名的格式取决于底层的签名方案。
调用此方法会将此签名对象重置为之前通过调用 initSign(PrivateKey) 进行签名初始化时的状态。也就是说,如果需要,通过新的更新和签名调用,该对象被重置并可用于从同一签名者生成另一个签名。
返回值:此方法返回签名操作结果的签名字节。
异常:如果此签名对象未正确初始化或此签名算法无法处理提供的输入数据,则此方法将引发SignatureException
以下是说明 sign() 方法的示例:
注意:以下程序不会在在线 IDE 中运行
示例 1:

Java

// 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 assigning in
            // keypair
            KeyPair keyPair = getKeyPair();
 
            // 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
            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();
 
            // printing 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();
    }
}

输出:

Signature : [96, 101, 38, 76, ... -59]

示例 2:

Java

// Java program to demonstrate
// sign() 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];
 
            // creating the object of Signature
            Signature sr
                = Signature.getInstance("SHA1WithRSA");
            ;
 
            // getting the number of bytes
            // placed in outbuffer
            // by using method sign()
            System.out.println(
                "Trying to get"
                + " the signature byte before initializing");
            byte[] bytes = sr.sign();
 
            // printing the number of byte
            System.out.println("Signature:" + bytes);
        }
 
        catch (NoSuchAlgorithmException e) {
 
            System.out.println("Exception thrown : " + e);
        }
        catch (SignatureException e) {
 
            System.out.println("Exception thrown : " + e);
        }
    }
}

输出:

Trying to get the signature byte before initializing
Exception thrown : java.security.SignatureException: object not initialized for signing