Java Signature sign()方法及其示例

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

这个 符号() 方法 JAVA安全供应商 类用于完成签名操作,并将生成的签名字节存储在提供的缓冲区dataBuffer中,从偏移量开始。签名的格式取决于基础签名方案。 此签名对象将重置为其初始状态(调用一个initSign方法后的状态),并可重用以生成具有相同私钥的进一步签名。 语法:

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

参数: 此方法将以下参数作为参数 数据缓冲 –作为字节[]数组的签名结果缓冲区。 抵消 –偏移到存储签名的数据缓冲区。 –dataBuffer中为签名分配的字节数。 返回值: 此方法返回放入dataBuffer的字节数。

例外情况: 这个方法抛出 签名例外 如果此签名对象未正确初始化,或者此签名算法无法处理提供的输入数据。 下面是一些例子来说明 符号() 方法: 注: 以下程序将不会在联机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();
}
}


输出:

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安全供应商 类用于返回所有更新数据的签名字节。签名的格式取决于基础签名方案。 调用此方法会将此签名对象重置为之前通过调用initSign(PrivateKey)初始化签名时的状态。也就是说,对象被重置,如果需要,可以通过新的更新和签名调用,从同一个签名者生成另一个签名。 返回值: 此方法返回签名操作结果的签名字节。 例外情况: 这个方法抛出 签名例外 如果此签名对象未正确初始化,或者此签名算法无法处理提供的输入数据。 下面是说明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 initializingException thrown : java.security.SignatureException: object not initialized for signing 
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享