Java jvm learning notes (practice: sign the jar package code)

Source: Internet
Author: User

This section focuses on practice. Before proceeding with the relevant operations, I hope you can understand the concepts mentioned in note 7, at least you should understand the big picture in note 7.
Okay! Friends who are used to using ecplise have to force you to build the jdk environment today! Let's sign the jar file!
Step 1: first configure the jdk environment variable. If your computer has already been configured, skip this step.
Path = % JAVA_HOME %/bin
JAVA_HOME = C:/Java/jdk1.6.0 _ 01
CLASSPATH =.; % JAVA_HOME %/lib/dt. jar; % JAVA_HOME %/lib/tools. jar
Configure the environment parameters of the jdk. After the configuration is complete, run java and javac in cmd to check whether the command takes effect. If the configuration is successful, execute step 2.
Step 2: write a few simple classes. What is simple is everyone's. You can copy my code directly. I don't understand it, ignore it, and do experiments. The signature of the jar file is not a signature. The Code logic in this example is used later, and you don't need to read it.
First class Doer
Package com. yfq. test;

Public abstract interface Doer {
Void doYourThing ();
}
Second category

Package com. yfq. test. friend;

Import java. security. AccessController;
Import java. security. PrivilegedAction;

Import com. yfq. test. Doer;

Public class Friend implements Doer {
Private Doer next;
Private boolean direct;
 
Public Friend (Doer next, boolean direct ){
This. next = next;
This. direct = direct;
}
 
@ Override
Public void doYourThing (){
System. out. println ("Im a Friend ");

If (direct ){
Next. doYourThing ();
} Else {
AccessController. doPrivileged (new PrivilegedAction (){

@ Override
Public Object run (){
Next. doYourThing ();
Return null;
}

});

}
}

}
Category 3

Package com. yfq. test. stranger;

Import java. security. AccessController;
Import java. security. PrivilegedAction;

Import com. yfq. test. Doer;

Public class Stranger implements Doer {

Private Doer next;
Private boolean direct;

Public Stranger (Doer next, boolean direct ){
This. next = next;
This. direct = direct;
}

@ Override
Public void doYourThing (){
System. out. println ("Im a Stranger ");

If (direct ){
Next. doYourThing ();
} Else {
AccessController. doPrivileged (new PrivilegedAction (){

@ Override
Public Object run (){
Next. doYourThing ();
Return null;
}

});

}
}

}
Well, compile it and compile it with powerful ecplise. Right-click the Project and choose Build Project (the tool is used. Do not waste these powerful functions !)

Step 3: open the jar package and use ecplise to export the jar package. I am still saying that tools are not needed, not good, but stupid.
Step 1: Right-click the project and choose "Export-java-JAR file-next ".
Step 2: Expand the directory list-for com. yfq. tes. friend and com. yfq. test. stranger packaging (friend. jar, stranger. jar), you can just put it anywhere, as long as you remember, I suppose it is placed in the root directory of the d disk.

Step 4: Use keytool of java to generate a key pair and use jarsigner of java to sign the key pair. (remember Note 7: Did we say that encryption of hash digest is not symmetric? Here we need two different keys.) Come with me step by step.
Step 1: In the cmd window, go to the directory where friend. jar and stranger. jar are stored. Assume that my jar file is placed under drive d and enter drive letter d.
Step 2: Enter keytool-genkey-alias friend-keypass friend4life-validity 10000-keystore ijvmkeys In the cmd window.
Generate the first key pair. the alias of this key pair is friend. The password required for viewing the key pair is friend4file. The expiration time of the key pair is 10000 days. The name of the key pair is ijvmkeys.
Check the vmkeys password and set it to ijvm2ed.

Step 3: Enter keytool-genkey-alias stranger-keypass stranger4life-validity 10000-keystore ijvmkeys In the cmd window.
Follow the steps in step 2 to create a key pair with the alias stranger.
After the key pair is generated, check whether there is an additional ijvmkeys file in your jar file directory. Here, a file is generated to store the key pair.
Step 4: digest the jar file and encrypt the hash digest to generate a signature, which is placed at the end of the jar file structure
In the cmd window, enter
Jarsigner-keystore ijvmkeys-storepass ijvm2ed-keypass firend4file friend. jar friend
Jarsigner-keystore ijvmkeys-storepass ijvm2ed-keypass stranger4file stranger. jar stranger
Step 5: view the generated key file and enter keytool-list-v-keystore ijvmkeys-storepass ijvm2ed In the cmd window.

Check the last one. Let's verify what we have said in note 7.
1. We have said that the hash digest is a 128 value, right? Check the line of the certificate fingerprint, md5 :....
You have a total of 32 hexadecimal numbers. A hexadecimal number can be expressed in four digits. The total number is 32*4 = 128, but there is another sha1 in the end. What's going on? It seems to be more than 128 bits. It's dropping. There are a variety of hash functions. Use the hash function, md5 or sha1. You just like it, which hash function can be specified? The keytool parameter-keyalg "DSA" is used to specify the hash algorithm. The default parameter is DSA, the normal 128-bit hash is safe.
2. in note 7, do not remember the bottom figure. One Certification Authority authenticates the Public Key (that is, the encrypted Public Key) of the decrypted signature (encrypted hash Digest ), we have no Certification Authority. Do you have any questions?
When the keytool program generates a key, it always generates a self-signed certificate (self-Signed means: If there is no authentication organization nearby, you can use the private key to sign the public key and generate a self-signed certificate)
Summary:
All right, we have completed the signature for the jar file,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.