4
Java Source Code Protection Based on JNI and AES Algorithm Zhao Hong Computer and Control Engineering Department Nankai University Tianjin , China Email: e-mail: [email protected] Guo Nan Computer and Control Engineering Department Nankai University Tianjin , China Email: [email protected] Abstract—In the Java web security field, the encryption and decryption code of the traditional Java source code protection scheme, which is based on custom class loader encryption, can be easily decompiled. This effect results in the leakage of the source code and the low efficiency of encryption and decryption. This study proposes a scheme based on the Java Native Interface (JNI) and the Advanced Encryption Standard (AES) to protect the Java source code. The proposed scheme adopts the JNI for calling the encryption and decryptions codes implemented by C/C ++, and the encryption and decryptions codes adopt an improved AES encryption algorithm. This scheme greatly improves both the encryption and decryption of the implementation efficiency and the security of the source code. Index Terms—Advanced Encryption Standard (AES), Data Encryption Standard(DES), Java Native Interface(JNI), Java, encryption. I. I NTRODUCTION With the advancement of Internet technology, Java language has become widely recognized and used for its simplici- ty, portability, cross-platform compatibility, and other unique advantages. Although the Java language offers convenience, it also presents security risks. After a Java source code is compiled, a Java bytecode file, which can be easily decom- piled, is generated [1]. Consequently, the source code can be easily stolen, and software intellectual property rights are not protected. Decompilers can also steal the source code to analyze the logic of the idea development and then find loopholes that indicate the stability of the software system cannot be guaranteed. Therefore, protection of the Java source code must be ensured. The traditional Java source code protection scheme typically uses code obfuscation and custom class loader encryption. Code obfuscation converts bytecode files into a format that is hard to read, making it difficult for the confused code to be decompiled and ensuring that the true semantics of the program will be challenging to understand even if the confused code can be decompiled successfully. Although the confused code increases the difficulty of decompiling and reading the source code, it cannot completely prevent the bytecode file from being decompiled, and the efficiency is not ideal [2]. Thus, a custom class loader can be utilized for encryption, which offers improved security. Each time the Java virtual machine loads class files, it requires a class loader object, which is mainly responsible for loading class files. This virtual machine provides the string containing the class files name, and the class loader object then finds the class file and converts it to a class object. Therefore, this mechanism can be used to rewrite the class loader. The class loader first decrypts the encrypted class file and then converts it into a class object. Given that the decrypted class file will not be saved in the file system, the attacker cannot obtain the source code. However, traditional custom class loader encryption suffers from many risks. First, the encryption and decryption code is implemented by the Java language and the codes class files cannot be encrypted, because once they are encrypted, the Java virtual machine cannot load the class files. Therefore, using Java language to implement encryption and decryption raises the risk of decompilation. Once an attacker obtains the encryption and decryption code, the source code can then be easily obtained. Second, the traditional scheme encrypts and decrypts class files through the encryption function API pro- vided by the J2EE Connector Architecture (JCA) and the Java Cryptography Extension (JCE). Java programs run on virtual machines and are not good at interacting with hardware and operating systems. Complex mathematical operations reveal that the implementation efficiency of Java programs is lower than that of local procedures implemented by C/C++. There- fore, using C/C++ to implement encryption and decryption promotes the efficiency of the upgrade [3], [4]. In addition, different encryption algorithms for implemen- tation efficiency present significant differences. The Advanced Encryption Standard (AES) and Data Encryption Standard (DES) algorithms have better efficiency [5], which will impact the speed of the class files encryption and decryption. There- fore, the different encryption algorithms must be explored, and an efficient encryption algorithm must be established to further enhance the efficiency of the system. The current study proposes a scheme based on the Java Native Interface (JNI) and the Advanced Encryption Standard (AES) to protect the Java source code and compare it with the traditional scheme in terms of security and efficiency. The proposed scheme outperforms the traditional one.

Java Source Code Protection Based on JNI and AES … Source Code... · Java Source Code Protection Based on JNI and AES Algorithm Zhao Hong Computer and Control Engineering Department

Embed Size (px)

Citation preview

Page 1: Java Source Code Protection Based on JNI and AES … Source Code... · Java Source Code Protection Based on JNI and AES Algorithm Zhao Hong Computer and Control Engineering Department

Java Source Code Protection Based on JNIand AES Algorithm

Zhao HongComputer and Control Engineering Department

Nankai UniversityTianjin , China

Email: e-mail: [email protected]

Guo NanComputer and Control Engineering Department

Nankai UniversityTianjin , China

Email: [email protected]

Abstract—In the Java web security field, the encryption anddecryption code of the traditional Java source code protectionscheme, which is based on custom class loader encryption, canbe easily decompiled. This effect results in the leakage of thesource code and the low efficiency of encryption and decryption.This study proposes a scheme based on the Java Native Interface(JNI) and the Advanced Encryption Standard (AES) to protectthe Java source code. The proposed scheme adopts the JNI forcalling the encryption and decryptions codes implemented by C/C++, and the encryption and decryptions codes adopt an improvedAES encryption algorithm. This scheme greatly improves boththe encryption and decryption of the implementation efficiencyand the security of the source code.

Index Terms—Advanced Encryption Standard (AES), DataEncryption Standard(DES), Java Native Interface(JNI), Java,encryption.

I. INTRODUCTION

With the advancement of Internet technology, Java languagehas become widely recognized and used for its simplici-ty, portability, cross-platform compatibility, and other uniqueadvantages. Although the Java language offers convenience,it also presents security risks. After a Java source code iscompiled, a Java bytecode file, which can be easily decom-piled, is generated [1]. Consequently, the source code can beeasily stolen, and software intellectual property rights are notprotected.

Decompilers can also steal the source code to analyzethe logic of the idea development and then find loopholesthat indicate the stability of the software system cannot beguaranteed. Therefore, protection of the Java source code mustbe ensured.

The traditional Java source code protection scheme typicallyuses code obfuscation and custom class loader encryption.Code obfuscation converts bytecode files into a format thatis hard to read, making it difficult for the confused code tobe decompiled and ensuring that the true semantics of theprogram will be challenging to understand even if the confusedcode can be decompiled successfully. Although the confusedcode increases the difficulty of decompiling and reading thesource code, it cannot completely prevent the bytecode filefrom being decompiled, and the efficiency is not ideal [2].Thus, a custom class loader can be utilized for encryption,which offers improved security. Each time the Java virtual

machine loads class files, it requires a class loader object,which is mainly responsible for loading class files. This virtualmachine provides the string containing the class files name,and the class loader object then finds the class file and convertsit to a class object. Therefore, this mechanism can be used torewrite the class loader. The class loader first decrypts theencrypted class file and then converts it into a class object.Given that the decrypted class file will not be saved in the filesystem, the attacker cannot obtain the source code.

However, traditional custom class loader encryption suffersfrom many risks. First, the encryption and decryption code isimplemented by the Java language and the codes class filescannot be encrypted, because once they are encrypted, theJava virtual machine cannot load the class files. Therefore,using Java language to implement encryption and decryptionraises the risk of decompilation. Once an attacker obtains theencryption and decryption code, the source code can then beeasily obtained. Second, the traditional scheme encrypts anddecrypts class files through the encryption function API pro-vided by the J2EE Connector Architecture (JCA) and the JavaCryptography Extension (JCE). Java programs run on virtualmachines and are not good at interacting with hardware andoperating systems. Complex mathematical operations revealthat the implementation efficiency of Java programs is lowerthan that of local procedures implemented by C/C++. There-fore, using C/C++ to implement encryption and decryptionpromotes the efficiency of the upgrade [3], [4].

In addition, different encryption algorithms for implemen-tation efficiency present significant differences. The AdvancedEncryption Standard (AES) and Data Encryption Standard(DES) algorithms have better efficiency [5], which will impactthe speed of the class files encryption and decryption. There-fore, the different encryption algorithms must be explored, andan efficient encryption algorithm must be established to furtherenhance the efficiency of the system.

The current study proposes a scheme based on the JavaNative Interface (JNI) and the Advanced Encryption Standard(AES) to protect the Java source code and compare it withthe traditional scheme in terms of security and efficiency. Theproposed scheme outperforms the traditional one.

Page 2: Java Source Code Protection Based on JNI and AES … Source Code... · Java Source Code Protection Based on JNI and AES Algorithm Zhao Hong Computer and Control Engineering Department

II. METHODOLOGY

A. Encryption Algorithm

At present, data encryption algorithms can be classified assymmetric encryption and asymmetric encryption technologiesaccording to the encryption key type. Symmetric encryptiontechnology refers to the encryption and decryption processusing the same key. In general, symmetric encryption algo-rithms are fast, highly secure, have small system overheads,and suitable for encrypting large amounts of data. However,key management is difficult in such algorithms; once thekey is leaked, confidential data are no longer secure. Typicalsymmetric encryption algorithms include DES and AES [6].Asymmetric encryption technology refers to the encryptionand decryption process using different keys: a public keyand a private key. The public key is used for encryption,and the private key is used for decryption; only the correctprivate key can decrypt the data [7], [8], [9]. At present,typical asymmetric encryption algorithms include RSA [5].Asymmetric encryption algorithms are highly complex, haveslow execution speeds, and suitable for encrypting a smallamount of data. Such algorithms are also more secure thansymmetric encryption algorithms.

1) DES algorithm: The DES algorithm, which is alsoknown as the United States DES, is a symmetric cryptosystemthat comprises a block encryption algorithm. The plaintext isgrouped according to 64 bits, and the key length is 64 bits.However, in reality, only 56 bits participate in the calculation,and the other 8 bits are check bits. After grouping, the groupedplaintext group and the 56-bit key form a cipher text group bybit substitution or exchange. The process of the DES algorithmshown in figure 1.

After the 64-bit plaintext is entered, the initial IP transfor-mation is performed. The purpose of the transformation is toreassemble the 64-bit data blocks in bits and divide the outputsinto L0 and R0. Each part is 32 bits long and is the initial inputinto the first iteration of the cryptographic function F (L0, R0),thereby forming a new 64-bit data after 16 iterations. The new64-bit data undergo an initial inverse transformation to outputa 64-bit cipher text.

The initial transform and the initial inverse transform aresimple shift operations. The iterative process is the replace-ment and shift operation under the key control, which is thecore of the DES algorithm.

2) AES algorithm: The AES algorithm was designed bythe National Institute of Standards and Technology to replacethe DES as a next-generation encryption standard. The AESalgorithm is a symmetric block cipher. The plaintext is groupedaccording to 128 bits, and the key length is 128/192/256 bits.When the plaintext is encrypted, the corresponding encryptionrounds are 10/12/14 rounds [10]. Figure 2 shows the specificprocess of a 128-bit AES algorithm.

a) Byte substitution: ByteSub operation is a reversiblenonlinear byte substitution operation of the S box for com-pleting byte-to-byte substitution. Here, the higher and lower4 bits of a byte in a data block are regarded as the row and

Fig. 1. Process of the DES Algorithm

Fig. 2. Process of the AES Algorithm

column values respectively. Moreover, the row and columnvalues are taken as an index for eliminating elements fromthe corresponding positions in the S box as output.

b) Shift Rows: This step is a linear transformation thatrealizes the permutation of the internal bytes in the matrix tomake the information fully chaotic and improve nonlinearity.Each row of the matrix is cyclic shifted. The first line isretained, the second line rotates left one byte, the third linerotates left two bytes, and so on.

c) Mix Columns: This step uses linear transformationto adequately mix the operations of each straight line in thematrix. Each byte in each column is mapped to a new value,which is converted from the 4 bytes in the column throughthe function.

d) Add Round Key: Each byte in the matrix performs theXOR operation with the sub key, and each sub key is generatedby the key generation scheme.

Page 3: Java Source Code Protection Based on JNI and AES … Source Code... · Java Source Code Protection Based on JNI and AES Algorithm Zhao Hong Computer and Control Engineering Department

Fig. 3. Process of JNI calling

B. JNI Technology

JNI is an important characteristic of Java. Although theJava cross-platform offers convenience, it lacks the capacity tointeract with hardware and operating systems. JNI overcomesthis shortcoming by enabling the Java language to interact withthe native code written in C/C++ or the assembly language,thereby utilizing the advantages of the C/C++ language indealing with complex operations and interactions with thehardware. The binary file compiled by C/C++ cannot bedecompiled.

Therefore, this study adopts JNI technology to call theencryption and decryption code implemented by C/C++. Thepurpose of such adoption is to prevent the encryption anddecryption code from being decompiled and improve theencryption and decryption speed.

Figure 3 shows the process of JNI calling of the encryptionand decryption code implemented by C/C++.

For the Java program calling encryption function encryptimplemented by C/C++, the specific steps are as follows:

• Use the MyEclipse tool to write the local functioninterface declaration class proEncrypt and declare thenative method encrypt.

• Execute javac proEncrypt.java to generateproEncrypt.class.

• Execute javac proEncrypt.java to generateproEncrypt.class.

• Build a C/C++ dynamic link library namedencryptAPI. Add the proEncrypt.h file tothe project and implement the local function interfaceJava_proEncrypt_encrypt.

TABLE IENCRYPTION EFFICIENCY COMPARISON

Java Time(ms) JNI Time (ms) Speed Increase(time)File Size

AES DES AES DES AES DES

50KB 122−127 119−125 3 4−6 39−41 28−30

200KB 127−137 123−134 3−4 8−9 41−44 14−16

500KB 132−137 138−145 4−5 17−18 32−33 7−8

1MB 146−153 159−167 5−6 33−34 28−29 3−4

5MB 236−246 333−340 16−17 153−156 13 1

20MB 581−593 963−1096 56−62 613−621 9 0.7

TABLE IIDECRYPTION EFFICIENCY COMPARISON

Java Time(ms) JNI Time (ms) Speed Increase(time)File Size

AES DES AES DES AES DES

50KB 128−132 120−130 3 4−5 41−43 29−32

200KB 133−137 124−128 3−4 8−10 43−44 14−15

500KB 140−146 138−141 4−5 16−17 34−35 7

1MB 150−160 159−164 6−8 32−33 24−26 4

5MB 210−260 296−297 19−21 148−149 10−12 1

20MB 564−588 833−855 50−69 591−598 10 0.4

• Compile the project, generate the DLL fileencryptAPI.dll, and place the dynamic linklibrary files in the project folder.

• Use the MyEclipse tool to write the Javaclass App, and use the System.loadLibrary(encryptAPI) to load the DLL file and call the codeof encryption.

III. EXPERIMENT VERIFICATION

Under the encryption and decryption conditions based onJNI technology and Java, the AES and DES algorithms wererespectively used to perform encryption and decryption oper-ations on files of different sizes. The time consumed by theencryption and decryption process was taken as the evaluationcriterion of efficiency.

A. Experimental design

For the unified use of 128-bit AES and DES algorithmsunder CBC mode conditions, this study compared the tradi-tional Java source code protection scheme and the proposedJNI technology-based scheme in terms of implementationefficiency. The test file was a randomly selected binary filewith a size ranging from 50 KB to 20 MB.

To make the test results more accurate, the encryptionand decryption operations were performed 100 times underdifferent conditions, leading to more general results. Thefindings indicate that the time of encryption and decryptionis always stable within a certain range. Experimental envi-ronment: Windows 7 Professional Edition, frequency 3.4 G,memory 8 G.

Page 4: Java Source Code Protection Based on JNI and AES … Source Code... · Java Source Code Protection Based on JNI and AES Algorithm Zhao Hong Computer and Control Engineering Department

B. Realization of Experimental Design

1) JNI-based encryption and decryption: Under the JNI-based scheme, the AES and DES algorithms respectivelyimplemented the encryption and decryption functions. Theprocess of JNI calling is referenced in Figure 3.

2) Java-based encryption and decryption: Class files wereencrypted and decrypted through the encryption function APIprovided by the JCA and the JCE.

C. Experimental results and analysis

Under the conditions of the encryption based on JNI tech-nology and Java, the efficiency of the AES and DES algorithmsis compared. The results are shows in Table 1.

Under the conditions of the decryption based on JNI tech-nology and Java, the efficiency of the AES and DES algorithmsis compared. The results are shows in Table 2.

According to Tables 1 and 2, both the DES and AESalgorithms utilize JNI technology to implement the encryptionand decryption functions and attain higher efficiency than thetraditional Java code protection scheme. In addition, underJNI technology, the superiority of the AES algorithm overthe DES algorithm in implementation efficiency became moreobvious with increasing file size. [5] and [10] compared theDES and AES algorithms in terms of execution speed anddetermined that the AES algorithm is superior to the DESalgorithm in execution efficiency. This finding is consistentwith the experimental results of the present paper. The 128-bit AES algorithm is likewise more secure than the 64-bit DESalgorithm [10], indicating that data encryption using the AESalgorithm is safer.

IV. SUMMARY AND DISCUSSIONThe encryption and decryption code of the traditional Java

source code protection scheme, which is based on custom classloader encryption, can be easily decompiled. This effect resultsin the leakage of the source code and the low efficiency ofencryption and decryption.

This study proposed a scheme based on the JNI techniqueand the AES algorithm to protect the Java source code. TheJNI technique invokes the encryption and decryption codeimplemented by C/C++, and the encryption and decryptionfunctions are implemented by the AES algorithm. The mainconclusions of this study are as follows:

• Using JNI technology to encrypt and decrypt Java classfiles not only prevents the decompiling of the encryp-tion and decryption code but also achieves enhancedimplementation efficiency by realizing the encryption anddecryption functions implemented by C/C++.

• The performance of the AES and DES algorithms arecompared under JNI technology-based and Java-basedconditions respectively. The findings indicate that theAES algorithm has higher efficiency than the DES al-gorithm.

Compared with the traditional scheme, the proposed schemedemonstrates a higher efficiency and security. Nonetheless,certain aspects can still be improved, such as the secrecy of

key protection and the efficiency of encryption and decryption.Future works should further strengthen the key protection andenhance the efficiency of encryption and decryption.

REFERENCES

[1] W. Rui, “Java byte code file encryption research,” Advances in Intelligentand Soft Computing, vol. 119, pp. 629–633, 2011.

[2] P. Zhu and G. Xiang, “The protection methods for mobile code basedon homomorphic encryption and data confusion,” in Proceedings -2011 International Conference on Management of e-Commerce and e-Government, Nov. 2011, pp. 256–260.

[3] K.-J. K. E.-H. H. S.-H. Y. Yeong-Jun Kim, Seong-Jin Cho and J.-W.Jeon, “Benchmarking java application using jni and native c applicationon android,” in International Conference on Control, Automation andSystems, Oct. 2012, pp. 284–288.

[4] Y. S. Chenxiong Qian, Xiapu Luo and A. T. Chan, “On trackinginformation flows through jni in android applications,” in Proceedings- 44th Annual IEEE/IFIP International Conference on DependableSystems and Networks, 2014, pp. 180–191.

[5] S. Kansal and M. Mittal, “Performance evaluation of various symmetricencryption algorithms,” in Proceedings of 2014 3rd International Con-ference on Parallel, Distributed and Grid Computing, 2015, pp. 105–109.

[6] B. Mandal and S. Chandra, “A comparative and analytical study onsymmetric key cryptography,” in 2014 International Conference onElectronics, Communication and Computational Engineering, 2015, pp.131–136.

[7] E. D. P. Fanfara and M. Dufala, “Usage of asymmetric encryptionalgorithms to enhance the security of sensitive data in secure communi-cation,” in SAMI 2012 10th IEEE Jubilee International Symposium onApplied Machine Intelligence and Informatics, 2012.

[8] C. Paar and J. Pelzl. (2009) Introduction to public-key cryptography.[Online]. Available: Available:http://crypto.rub.de/Buch/movies.php

[9] B. B. Asma Chaouch and O. Bouraoui, “Software application forsimulation-based aes, rsa and elliptic-curve algorithms,” in 2nd Inter-national Conference on Advanced Technologies for Signal and ImageProcessing - ATSIP’2016.

[10] A. K. Mandal and C. Parakash, “Performance evaluation of crypto-graphic algorithms:des and aes,” in 2012 IEEE Students’ Conference onElectrical, Electronics and Computer Science: Innovation for Humanity,2012.