Java Encryption decryption

Source: Internet
Author: User
Tags execution

Java is the language that interprets execution, and for different operating platforms there is a corresponding JVM that interprets bytecode files for execution. And this bytecode file, which is what we normally see in every. class file. This is common sense, as we all know, by the. java file, compiled by the compiler, into the. class file that the JVM can interpret. And this process, in the now open Network technology, using an inverse compiler, anyone can easily get its source files. This is not what many people want to see. For encryption and decryption technology, I do not know much, some can use some kind of technology "fuzzy" Java class files. This makes it more difficult to decompile. However, it is estimated that the technical level of the counter compiler is also increasing, which leads to the blocking of the method layer. There are also a number of other technologies that can also implement cryptographic decryption of Java files. What I want to study now is one of them. The flexibility of Java makes it easy to decompile and, at the same time, makes our methods of encrypting and decrypting more flexible. Take advantage of custom ClassLoader.

Every class in Java is loaded into memory through the ClassLoader. The workflow for the ClassLoader is represented as follows:

1.searchfile ()

Find the class file that I want to load. (The idea of throwing the jar pack is now just to load a. class file)

2.loadDataClass ()

The byte code that reads this class file.

3.difineClass ()

Load class file. (The loading process is really complicated, and we're not going to look at it now.) From this process we can clearly find that the custom class loading can easily control the loading process of each class file. So between the second step (Loaddataclass) and the third Step (Difineclass), we will have our own space to control this process flexibly. Our encryption and decryption technology is applied here. There are many techniques for encrypting and decrypting Java encryption and decryption. Java itself provides a good class library to support a variety of algorithms. There are different versions of the network for which algorithm to use. The following is a simple example of the DES symmetric encryption algorithm (setting a key and then encrypting all the data). First, a key is generated.

I saved it in the key.txt. This document is like a key. Whoever owns it will be able to unlock our class files.

Code references are as follows:

Package Com.neusoft.jiami
Import java.io.File;
import java.io.FileOutputStream;
Import Java.security.SecureRandom;
Import Javax.crypto.KeyGenerator;
Import Javax.crypto.SecretKey;   
Class Key {
Private String KeyName
  Public Key (String keyname) {
This.keyname = KeyName;

public void CreateKey (String keyname) throws Exception {
//Create a trusted random number source, the DES algorithm requires
securerandom sr = New SecureRandom ();     
//Create a Keygenerator object with the DES algorithm
keygenerator kg = keygenerator.getinstance ("DES");
    Initializes this key generator with the determined key length
Kg.init (SR);
    Generate key
Secretkey key = Kg.generatekey ();
    Gets the key data
byte rawkeydata[] = key.getencoded ();
    Saves the obtained key data to a file and uses the
FileOutputStream fo = new FileOutputStream (new file (KeyName)) to be decrypted;
Fo.write (Rawkeydata);     
}
public static void Main (String args[]) {
Try {
New Key ("Key.txt");
catch (ExceptiOn e) {
E.printstacktrace ();
}
}

Related Article

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.