How to decrypt a JAR file or a jar File
Explore the powerful functions of the JAR File Format
Most Java programmers are familiar with the basic operations on JAR files. However, only a few programmers know the JAR file format.Powerful functions. In this article, the author discusses many functions and advantages of the JAR format, including packaging, executable JAR files, security, and indexing.
What is a JAR file?
The JAR file format is based on popular ZIP file formats and is used to aggregate many files into one file. Unlike ZIP files, JAR files are not only used for compression and release, but also for deployment and encapsulation of libraries, components, and plug-in programs, and can be directly used by tools such as compilers and JVM. JAR contains special files, such as manifests and deployment descriptor, to indicate how the tool handles specific JAR.
A jar file can be used:
- Used to publish and use Class Libraries
- As a building unit for applications and extensions
- As the deployment unit of components, applets, or plug-in programs
- Used to package auxiliary resources associated with components
The JAR file format provides many advantages and functions, many of which are not provided by traditional compression formats such as ZIP or TAR. They include:
- Security.You can add a digital signature to the JAR file. In this way, the tool that can identify the signature can selectively grant you software security privileges, which cannot be done by other files. It can also detect whether the code has been tampered.
- Reduce the download time.If an applet is bound to a JAR file, the browser can download the applet Class file and related resources in an HTTP transaction, rather than opening a new connection to each file.
- Compression.The JAR format allows you to compress files to improve storage efficiency.
- Transmission Platform extension.The Java extensionframework provides a method to add functions to the Java core platform, these extensions are packaged using JAR files (Java 3D and JavaMail are examples of extensions developed by Sun ).
- Pack seal.The package stored in the JAR file can be selectedSealEnhanced version consistency and security. Sealing a package means that all classes in the package must be found in the same JAR file.
- Package version control.A jar file can contain data about the files it contains, such as vendor and version information.
- Portability.The mechanism for processing JAR files is the standard part of the core APIs of the Java platform.
Compressed and uncompressed JAR
jarTool (for more information, seejarBy default, files are compressed. Uncompressed JAR files can generally be loaded faster than compressed JAR files, because files need to be decompressed during the loading process, but uncompressed files may be downloaded over the network for a longer time.
META-INF directory
Most JAR files contain a META-INF directory that is used to store package and extended configuration data such as security and version information. The Java 2 Platform recognizes and interprets the following files and directories in the META-INF directory to configure applications, extensions, and class loaders:
- MANIFEST. MF.ThisManifest FileDefines data related to extensions and packages.
- INDEX. LIST.This file is composed
jarNew Tool options-iIt contains the location information of the package defined in the application or extension. It is part of the implementation of JarIndex and is used by the class loader to accelerate the class loading process.
- Xxx. SF.This is the signature file of the JAR file. PlaceholderXxxIdentifies the signatory.
- Xxx. DSA.The signature block file associated with the signature file, which stores the public signature used to sign the JAR file.
Jar Tool
To execute basic tasks in a JAR file, use the Java Archive Tool (jarTool ). UsejarCommand calljarTool. Table 1 shows some common applications:
Table 1. Common
jarTool usage
| Function |
Command |
| Create a JAR file with a separate file |
Jar cf jar-file input-file... |
| Create a JAR file using a directory |
Jar cf jar-file dir-name |
| Create an uncompressed JAR File |
Jar cf0 jar-file dir-name |
| Update a JAR File |
Jar uf jar-file input-file... |
| View the content of a JAR file |
Jar tf jar-file |
| Extract the content of a JAR file |
Jar xf jar-file |
| Extract A specific file from a JAR file |
Jar xf jar-file archived-file... |
| Run an application packaged as an executable JAR File |
Java-jar app. jar |
Executable JAR
OneExecutable jarA file is a self-contained Java application. It is stored in a specially configured JAR file and can be directly executed by JVM without extracting the file or setting the class path in advance. To run an application stored in a non-executable JAR, you must add it to your class path and call the main class of the application by name. However, you can run an application without extracting the executable JAR file or knowing the main entry point. Executable JAR can facilitate the release and execution of Java applications.
Create executable JAR
It is easy to create an executable JAR. First, put all application code in a directory. Assume that the main class in the application iscom.mycompany.myapp.Sample. Create a JAR file containing the application code and identify the main class. To this end, createmanifestAnd add the following line to it:
Main-Class: com.mycompany.myapp.Sample
Then, create a JAR file like this:
jar cmf manifest ExecutableJar.jar application-dir
All you have to do is usejava -jarExecutableJar. JAR.
An executable JAR must be referenced by the menifest file header. If-jarThe environment variables CLASSPATH and all the class paths specified in the command line are ignored by JVM.
Start executable JAR
Now that we have packaged our application into an executable jar named ExecutableJar. JAR, we can use the following command to directly start the application from the file:
java -jar ExecutableJar.jar
Pack Seal
Sealing a package in the JAR file means that all classes defined in the package must be found in the same JAR file. This allows the package author to enhance version consistency between packaging classes. Sealing also provides a way to prevent code tampering.
To seal the package, you must addNameHeader, and then add the value "true" SealedHeader. Like an executable JAR, you can seal a JAR by specifying a manifest file with an appropriate Header element when creating the JAR, as shown below:
Name: com/samplePackage/ Sealed: true
NameHeader to identify the relative path of the output packet. It ends with a "/" and is different from the file name. InNameAll headers before the first blank line after the header are appliedNameThe file or package specified in the header. In the preceding exampleSealedHeader appears inNameHeader and no blank lines in the middle, soSealedThe header will be interpreted as applying only to the packagecom/samplePackage.
JVM throwsSecurityException.
Extended Packaging
Extended functions have been added to the Java platform, and the extended mechanism has been added to the JAR file format. The extension mechanism allows the JAR fileClass-PathSpecify other JAR files.
Assume that extension1.jar and extension2.jar are two JAR files in the same directory. The manifest file of extension1.jar contains the following headers:
Class-Path: extension2.jar
This header indicates that the classes in extension2.jar areExtension class. Classes in extension1.jar can call classes in extension2.jar, and do not require extension2.jar to be in the class path.
When loading the JAR using the extension mechanism, the JVM will efficiently and automaticallyClass-PathAdd the JAR referenced in the header to the class path. However, the extension JAR path is interpreted as a relative path, so in general, the extension JAR must be stored in the same directory where the JAR that references it is located.
For example, assume thatExtensionClientReferenced classExtensionDemo, It is bundled in a jar file named ExtensionClient. JAR, and the classExtensionDemoThen it is bundled in ExtensionDemo. jar. To enable ExtensionDemo. jar to be extended, you must list ExtensionDemo. jar in the manifestClass-PathHeader, as shown below:
Manifest-Version: 1.0 Class-Path: ExtensionDemo.jar
In this manifestClass-PathThe value of the header is ExtensionDemo. jar without a specified path, indicating that the ExtensionDemo. jar and ExtensionClient JAR files are in the same directory.
Security in JAR files
JAR files can be usedjarsignerTool or directly throughjava.securityAPI signature. A signed JAR file is exactly the same as the original JAR file, but it updates its manifest and adds two files, one signature file and one signature block file to the META-INF directory.
JAR files are stored inKeystoreThe certificate signature in the database. The certificate stored in the keystore is password-protected and must be directedjarsignerThe tool provides this password to sign the JAR file.
Figure 1. Keystore Database
Each signatory of the JAR is represented by a signature file with the. SF extension in the META-INF directory of the JAR file. The file format is similar to the manifest file-a set of RFC-822 headers. As shown below, its composition includes a main part, which includes information provided by the signatory, but not specifically for any specific JAR file items, and a series of separate items, these items must also be included in the menifest file. When verifying a signed JAR file, compare the digest value of the signature file with the digest value calculated for the corresponding items in the JAR file.
Listing 1. Manifest and signature files in the signature JAR
Contents of signature file META-INF/MANIFEST.MF Manifest-Version: 1.0 Created-By: 1.3.0 (Sun Microsystems Inc.) Name: Sample.java SHA1-Digest: 3+DdYW8INICtyG8ZarHlFxX0W6g= Name: Sample.class SHA1-Digest: YJ5yQHBZBJ3SsTNcHJFqUkfWEmI= Contents of signature file META-INF/JAMES.SF Signature-Version: 1.0 SHA1-Digest-Manifest: HBstZOJBuuTJ6QMIdB90T8sjaOM= Created-By: 1.3.0 (Sun Microsystems Inc.) Name: Sample.java SHA1-Digest: qipMDrkurQcKwnyIlI3Jtrnia8Q= Name: Sample.class SHA1-Digest: pT2DYby8QXPcCzv2NwpLxd8p4G4=
Digital Signature
A digital signature is the signed version of The. SF signature file. A digital signature file is a binary file and has the same file name as a. SF file, but its extension is different. There are different extensions based on the digital signature type-RSA, DSA, or PGP-and the certificate type used to sign the JAR.
Keystore
To sign a JAR file, you must first have a private key. The private key and Its Related Public Key Certificates are stored inkeystoresIn a password-protected database. JDK includes tools for creating and modifying keystores. Each key in the keystore can be identified by an alias. It is usually the name of the signatory who owns the key.
All keystore items (key and trusted Certificate items) are accessed with a unique alias. Alias is in usekeytool -genkeyCommand to generate the key pair (Public Key and private key) and specify it when adding an entry to the keystore. AfterkeytoolThe command must use the same alias to reference this item.
For example, to use the alias "james" to generate a new public/private key pair and wrap the public key into a self-signed certificate, run the following command:
keytool -genkey -alias james -keypass jamespass -validity 80 -keystore jamesKeyStore -storepass jamesKeyStorePass
This command sequence specifies an initial password "jamespass". Subsequent commands require this password when accessing the private key associated with the alias "james" in the keystore "jamesKeyStore. If the keystore "jamesKeyStore" does not existkeytoolIt is automatically created.
Jarsigner Tool
jarsignerThe tool uses keystore to generate or verify the digital signature of the JAR file.
Assume that the keystore "jamesKeyStore" is created as in the preceding example and contains a key named "james". You can use the following command to sign a JAR file:
jarsigner -keystore jamesKeyStore -storepass jamesKeyStorePass -keypass jamespass -signedjar SSample.jar Sample.jar james
This command uses the password "jamesKeyStorePass" to extract the key with the alias "james" and the password "jamespass" from the keystore named "jamesKeyStore" and Sample the key. jar file signature, create a signed JAR -- SSample. jar.
jarsignerThe tool can also verify a signed JAR file, which is much simpler than the signed JAR file. You only need to execute the following command:
jarsigner -verify SSample.jar
If the signed JAR file has not been tamperedjarsignerThe tool will tell you that the JAR has passed the verification. Otherwise, it will throwSecurityExceptionIndicates which files have not been verified.
You can also usejava.util.jarAndjava.securityThe API signs the JAR program programmatically (for more information, see references ). You can also use tools like Netscape Object Signing Tool.
JAR Index
If an application or applet is bundled into multiple JAR files, the Class Loader uses a simple linear search algorithm to search for each element in the class path, this allows the class loader to download and open many JAR files until it finds the desired class or resource. If the class loader tries to find a resource that does not exist, all JAR files in the application or applet will be downloaded. For large network applications and applets, this will lead to slow startup, slow response, and a waste of bandwidth.
After JDK 1.3, the JAR File Format supports indexing to optimize the Class search process in network applications, especially the applet. The JarIndex mechanism collects the content of all JAR files defined in the applet or application, and stores the information in the index file of the first JAR file. After downloading the first JAR file, the applet Class Loader efficiently loads the JAR file using the collected content. This directory information is stored in a simple text file named INDEX. LIST in the META-INF directory of the root JAR file.
Create a JarIndex
You canjarSpecify-iOption to create a JarIndex. Assume that the directory structure is shown in:
Figure 2. JarIndex
Run the following commands to create an index file for JarIndex_Main.jar, JarIndex_test.jar, and JarIndex_test1.jar:
jar -i JarIndex_Main.jar JarIndex_test.jar SampleDir/JarIndex_test1.jar
The format of the INDEX. LIST file is very simple. It contains the name of the package or class contained in each indexed JAR file, as shown in Listing 2:
Listing 2. JarIndex INDEX. LIST file example
JarIndex-Version: 1.0 JarIndex_Main.jar sp JarIndex_test.jar Sample SampleDir/JarIndex_test1.jar org org/apache org/apache/xerces org/apache/xerces/framework org/apache/xerces/framework/xml4j
Conclusion
The JAR format is much larger than a compression format. It has many features that can improve efficiency, security, and organize Java applications. Because these functions have been built on the core platform-including compilers and class loaders-, developers can simplify and improve the development and deployment processes by using the JAR File Format capabilities ..