當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Signature initSign()用法及代碼示例

initSign(PrivateKey privateKey)

java.security.Provider類的initSign()方法用於初始化此對象以進行簽名。如果使用不同的參數再次調用此方法,則該調用的效果無效。

用法:

public final void 
    initSign(PrivateKey privateKey) 
        throws InvalidKeyException

參數:此方法將身份的私鑰作為要生成其簽名的參數


Exception:如果 key 無效,則此方法引發InvalidKeyException。

下麵是說明initSign()方法的示例:

注意:以下程序將無法在在線IDE中運行

範例1:

// 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(); 
    } 
}

輸出:

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

範例2:顯示InvalidKeyException

// 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); 
        } 
    } 
}

輸出:

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

initSign(PrivateKey privateKey, SecureRandom random)

java.security.Provider類的initSign()方法用於初始化此對象以進行簽名。如果使用不同的參數再次調用此方法,則該調用的效果無效。

用法:

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

參數:此方法將以下參數作為參數:


  • privateKey-將要生成其簽名的身份的私鑰。
  • random-此簽名的隨機性來源。

異常:如果 key 無效,則此方法引發InvalidKeyException。

下麵是說明sign()方法的示例:

注意:以下程序將無法在在線IDE中運行

範例1:

// 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(); 
    } 
}

輸出:

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

範例2:顯示InvalidKeyException

// 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); 
        } 
    } 
}

輸出:

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


相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 Java Signature initSign() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。