BOUNCYCASTLE JCE Practice (6)

xiaoxiao2021-03-17  195

Signature implementation process

1) Read your private key

??? For your own private key file, use the File class to declare. When reading, use the FileInputStream format as an input stream. The read key is a byte array, so the read key should be saved with ByteArrayoutStream, and use the TobyTearray format to convert it into byte arrays.

Generate a signature to use your own private key, while the private key uses PKCS8 # encoding. So we have to convert byte arrays to pkcs8 # coding form. The implementation method is as follows:

PKCS8ENCODEDKEYSPEC Keyspec = New PKCS8ENCODEDKEYSPEC (KEYBYTES);

KeyFactory KeyFactory = KeyFactory.GetInstance ("RSA");

SyprivateKey = keyfactory.generateprivate (keySpec);

Where Keybytes is a key in the form of the byte array read from the original text. The algorithm is specified with the instantiation method of the KeyFactory object and generates a private key encoded by the GeneratePrivate method.

2) A file to be signed from the dialog

This step is relatively simple and does not explain too much.

3) Read the contents of the file as byte array format

Because the parameter of the Update () method of the Signature class is the form of byte arrays,

First read the original text as an array of bytes. Further, the content length of the original text can be obtained here.

4) Generate a signature

According to the previous description, first specify the MD5withRSA with the GetInstance () method of the Signature class.

Algorithm, then call the INITSIGN () method as a parameter using the previously obtained private key to initialize, and finally call the Update () method as a parameter to transfer the data, and use the private key in the form of the array as a parameter to call the SIGN () method to generate signature.

The generated signature is written in the file stream in the previously designed file format, completes all the signs. The implementation process of the signature can be represented by the following figure:

????? Figure digital signature process

The code is implemented as follows:

// Read the private key

????????????????????.

????? file syfile = new file ("c: // security file //" misclass.username "/-asymmetric // my private key //yhb.private");

????? Try

???? {

???? fileinputstream fis = new fileinputstream (syfile);

???? byterrayoutputstream baos = new byteArrayoutputStream ();

?

???? int thebyte = 0;

????? while ((thebyte = fis.read ())! = - 1)

????? {baos.write (thebyte);

?????}

????? fis.close ();

????? byte [] keybytes = baos.tobytearray ();

????? baos.close ();

?

????? pkcs8encodedkeyspec keySpec = new pkcs8encodedKeyspec (keybytes);

????? keyfactory keyfactory = keyfactory.getInstance ("RSA");

????? syprivatekey = keyfactory.generateprivate (keySpec);

???}

??? carat (Exception E9)

???? {

???? system.out.print ("Error When Read the Rsa Private Key); ???? System.exit (0);

????}

???? // From the dialog box to get the file to sign

???? file file = new file (Dirstring1, String1);

???? String filename = file.getname ();

???? // First read the file as a BYTE [] object

??? int Len = (int) file.Length ();

??? ration (len> 100000000)

??? {system.out.println ("The File Length Is Too Long!");

??? system.exit (0);

???}

??? byte [] inbuf = new byte [len];

??? Try {

??? fileinputstream instream = new fileinputstream (file);

??? Intintes = instream.available ();

???? inbuf [] = new byte [inbytes];

??? Int bytesread = instream.read (Inbuf, 0, Inbytes);

??? instream.close ();

??? //system.out.println (INBUF);

???}

??? catch (Exception EQ2)

??? {

??? system.out.println ("Error When Change the file to byte");

??? system.exit (0);

???}

??? // Signature specific process

??? Try {

??? // Byte [] signaturebytes = new byte [150];

??? Signature Sig = Signature.getInstance ("MD5withRSA");

??? sig.initsign (sypriVateKey);

??? Sig.Update (Inbuf);

??? byte [] signaturebytes = sig.sign ();

?????????? // written in the object stream

?? DataOutputStream outfile = new DataOutputStream (New FileoutputStream

?????????????????? "c: // security file // file //" filename ".yhb3")))

??? Outfile.writeint (SignatureBytes.length);

??? Outfile.write (SignatureBytes);

??? Outfile.writeint (len);

??? Outfile.write (Inbuf);

??? Outfile.close ();

???}

??? catch (Exception EH3)

??? {

??? System.out.println ("Error Whenrate the outfile);

??? system.exit (0);

???}

?

?

Author's name HONGSOFT, research area: 1) Research Based on Workflow 2) Based on Java-based information security technology. Welcome to discuss Java-related issues hongbosoftware@163.com

转载请注明原文地址:https://www.9cbs.com/read-129620.html

New Post(0)