Code encryption method and device, storage medium and program product
By packaging and XOR operations on Java code to generate encrypted code packages and deploying them in the proxy library, the problem of poor code encryption in the existing technology is solved, effective protection of Java code is achieved, and the risk of decompilation is avoided.
Patent Information
- Application Number
- CN202411375185.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-29
- Publication Date
- 2024-12-17
AI Technical Summary
Existing code encryption methods are poor in cross-platform languages such as Java, and cannot effectively prevent the code from being decompiled and read, making it difficult to implement source code protection.
By packaging the code to be encrypted, using encryption functions to XOR the character stream, generating the encrypted code package, and deploying the encrypted package in the application's proxy library to ensure the security of the code at runtime.
It effectively avoids the code being decompiled and read, improves the encryption quality of the code, protects Java code from being leaked, reduces the risks of piracy and competitors' plagiarism, and enhances the security and stability of the software.
Smart Images

Figure CN119150325A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present application relate to the field of computers, and more specifically, to a code encryption method and device, a storage medium, and a program product. Background Art
[0002] Source code protection refers to the use of various technical means to encrypt, obfuscate or otherwise protect the code to prevent it from being reverse engineered, stolen or leaked. With the intensification of software piracy and technological competition, the importance of source code protection is increasing.
[0003] For cross-platform languages (such as Java), there are many methods and means on the market to decompile code files. However, as a delivery type product, server products often do not want customers or peers to be able to view the source code developed on the server at will, so the developed code needs to be encrypted and protected.
[0004] The existing encryption method is mainly code obfuscation, which makes the code difficult to understand by renaming classes, methods and fields, but the function of the bytecode remains unchanged. The disadvantage is that the code can still be decompiled, and the logical function of the source code can still be understood through careful study. Therefore, the above method cannot effectively encrypt the code to protect the code.
[0005] Therefore, there is a technical problem in the prior art that the encryption effect of the code is poor. Summary of the invention
[0006] The embodiments of the present application provide a code encryption method and device, a storage medium, and a program product to at least solve the technical problem of poor code encryption effect in the related art.
[0007] According to one embodiment of the present application, a code encryption method is provided, including: packaging the code to be encrypted to obtain a first code package; compiling and generating the encryption package based on an encryption function; and when a character stream to be encrypted corresponding to the first code package is obtained, calling the encryption package to encrypt the character stream to be encrypted through the encryption function to generate an encrypted second code package.
[0008] As an optional scheme, before calling the encryption package to encrypt the character stream through the encryption function to generate an encrypted second code package, the method also includes: reading all character streams corresponding to the first code package; determining a character stream that meets the expected encryption condition from all character streams according to the code path where the code to be encrypted is located, and determining the character stream that meets the expected encryption condition as the character stream to be encrypted, wherein the expected encryption condition is used to indicate that the code path where the code associated with the character stream is located is the expected code path.
[0009] As an optional scheme, the encrypting the character stream to be encrypted to generate an encrypted second code package includes: obtaining an encryption password associated with the encryption function; using the encryption password to perform an XOR operation on the character stream to be encrypted to obtain an encrypted character stream; and compiling the encrypted character stream to generate the second code package.
[0010] As an optional scheme, the method of using the encryption password to perform an XOR operation on the character stream to be encrypted to obtain an encrypted character stream includes: taking the length of the encryption password as a group, performing an XOR operation with each character of the character stream to be encrypted, and generating each encrypted character corresponding to each character of the character stream to be encrypted; and determining the encrypted character stream based on each encrypted character.
[0011] As an optional solution, the method further includes: deploying the encryption package in a proxy library in an application, wherein the application is used to encrypt the code to be encrypted.
[0012] As an optional solution, before packaging the code to be encrypted to obtain a first code package, the method also includes: obtaining the java code to be encrypted, wherein the java code to be encrypted is used for delivery server code development; determining the java code to be encrypted as the code to be encrypted; before compiling and generating an encryption package based on the encryption function, the method also includes: encoding the encryption function using C language.
[0013] According to another embodiment of the present application, a code encryption device is provided, including: a packaging unit, used to package the code to be encrypted to obtain a first code package; a compilation unit, used to compile and generate an encrypted package based on an encryption function; an encryption unit, used to call the encrypted package when a character stream to be encrypted corresponding to the first code package is obtained, so as to encrypt the character stream to be encrypted through the encryption function to generate an encrypted second code package.
[0014] As an optional scheme, the device also includes: a reading module, which is used to read all character streams corresponding to the first code package before calling the encryption package to encrypt the character stream through the encryption function to generate an encrypted second code package; a first determination module, which is used to determine, from all character streams, a character stream that meets the expected encryption condition according to the code path where the code to be encrypted is located, before calling the encryption package to encrypt the character stream through the encryption function to generate an encrypted second code package, and determine the character stream that meets the expected encryption condition as the character stream to be encrypted, wherein the expected encryption condition is used to indicate that the code path where the code associated with the character stream is located is the expected code path.
[0015] As an optional solution, the encryption unit includes: a first acquisition module, used to obtain an encryption password associated with the encryption function; an XOR module, used to use the encryption password to perform an XOR operation on the character stream to be encrypted to obtain an encrypted character stream; and a compilation module, used to compile the encrypted character stream to generate the second code package.
[0016] As an optional solution, the XOR module includes: an XOR submodule, which is used to perform an XOR operation with each character of the character stream to be encrypted, taking the length of the encryption password as a group, to generate each encrypted character corresponding to each character of the character stream to be encrypted; and a determination submodule, which is used to determine the encrypted character stream based on each encrypted character.
[0017] As an optional solution, the device also includes: a deployment module, which is used to deploy the encryption package in a proxy library in an application after the encryption package is compiled and generated based on the encryption function, wherein the application is used to encrypt the code to be encrypted.
[0018] As an optional solution, the device also includes: a second acquisition module, used to obtain the java code to be encrypted before packaging the code to be encrypted to obtain the first code package, wherein the java code to be encrypted is used for delivery server code development; a second determination module, used to determine the jaya code to be encrypted as the code to be encrypted before packaging the code to be encrypted to obtain the first code package; the device also includes: an encoding module, used to encode the encryption function using C language before compiling and generating an encryption package based on the encryption function.
[0019] According to another embodiment of the present application, a computer-readable storage medium is provided, in which a computer program is stored, wherein the computer program is configured to execute the steps of any of the above method embodiments when run.
[0020] According to another embodiment of the present application, an electronic device is provided, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.
[0021] According to another embodiment of the present application, a computer program product is provided, including a computer program, and when the computer program is executed by a processor, the steps of the method described in each embodiment of the present application are implemented.
[0022] Through the embodiments provided by the present application, the code to be encrypted is packaged to obtain a first code package; based on the encryption function, the encrypted package is compiled and generated; when the character stream to be encrypted corresponding to the first code package is obtained, the encrypted package is called to encrypt the character stream to be encrypted through the encryption function to generate an encrypted second code package, thereby achieving the purpose of preventing the code from being decompiled and read, thereby achieving the technical effect of improving the encryption quality of the code and solving the technical problem of poor encryption effect of the code. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] Figure 1 is a hardware structure block diagram of a code encryption method according to an embodiment of the present application;
[0024] Figure 2 is a flow chart of a code encryption method according to an embodiment of the present application;
[0025] Figure 3 is a schematic diagram of a code encryption method according to an embodiment of the present application;
[0026] Figure 4 is a schematic diagram of a code encryption method according to an embodiment of the present application;
[0027] Figure 5 is a schematic diagram of a code encryption method according to an embodiment of the present application;
[0028] Figure 6 It is a structural block diagram of a code encryption device according to an embodiment of the present application. DETAILED DESCRIPTION
[0029] The embodiments of the present application will be described in detail below with reference to the accompanying drawings and in combination with the embodiments.
[0030] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.
[0031] The method embodiments provided in the embodiments of the present application can be executed in a computer terminal or a similar computing device. Taking running on a computer terminal as an example, Figure 1 1 is a hardware structure block diagram of a computer terminal of a code encryption method according to an embodiment of the present application. Figure 1 As shown, the computer terminal may include one or more ( Figure 1 Only one is shown in the figure) a processor 102 (the processor 102 may include but is not limited to a processing device such as a microprocessor MCU or a programmable logic device FPGA) and a memory 104 for storing data, wherein the above-mentioned computer terminal may also include a transmission device 106 and an input / output device 108 for communication functions. It can be understood by those skilled in the art that Figure 1 The structure shown is only for illustration and does not limit the structure of the above-mentioned computer terminal. Figure 1 More or fewer components as shown, or with Figure 1 Different configurations shown.
[0032] The memory 104 can be used to store computer programs, for example, software programs and modules of application software, such as the computer program corresponding to the method for determining the mapping relationship in the embodiment of the present application. The processor 102 executes various functional applications and data processing by running the computer program stored in the memory 104, that is, to implement the above method. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include a memory remotely arranged relative to the processor 102, and these remote memories may be connected to the computer terminal via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0033] The transmission device 106 is used to receive or send data via a network. The specific example of the above network may include a wireless network provided by a communication provider of a computer terminal. In one example, the transmission device 106 includes a network adapter (Network Interface Controller, referred to as NIC), which can be connected to other network devices through a base station so as to communicate with the Internet. In one example, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.
[0034] As an optional solution, the code encryption method includes the following specific steps:
[0035] S202, packaging the code to be encrypted to obtain a first code package;
[0036] S204, compiling and generating an encryption package based on the encryption function;
[0037] S206: When the character stream to be encrypted corresponding to the first code package is obtained, the encryption package is called to encrypt the character stream to be encrypted through the encryption function to generate an encrypted second code package.
[0038] Optionally, in this embodiment, the code encryption method can be applied to, but is not limited to, Java source code protection scenarios. Java source code protection refers to encrypting, obfuscating or otherwise protecting Java code through various technical means to prevent the code from being reverse engineered, misappropriated or leaked. With the intensification of software piracy and technological competition, the importance of source code protection is increasing. Since Java is a cross-platform language, there are many methods and means on the market for decompiling Java .cIass files, but server products, as delivery-type products, often do not want customers or peers to be able to view the Java source code developed on the server at will. Therefore, it is necessary to encrypt and protect the developed code, and appropriate decryption means are required during operation to ensure that the program can run smoothly.
[0039] Optionally, the encryption method mainly includes the following:
[0040] Code obfuscation: The tools used can be ProGuard, ZelixKlassMaster, etc. This method is an obfuscation technology that makes the code difficult to understand by renaming classes, methods, and fields, but the function of the bytecode remains unchanged. This is one of the most commonly used and effective protection methods, but the drawback is that the code can still be decompiled, and through careful study, the logical function of the source code can still be understood.
[0041] Packaging and packaging, by packaging the code into Jar or War files, can reduce the risk of direct access to the source code to a certain extent. However, there are still tools that can decompile and read it.
[0042] The dynamic code loading method hides the code and dynamically loads it from the server when the code is executed, which reduces the amount of code stored locally. However, the jar package can be copied when the code is loaded, and then the code can be decompiled. The code cannot be effectively encrypted to protect it.
[0043] Based on the above problems, this embodiment proposes a code encryption method, which can solve the problem that the Java project code is decompiled and read by customers or peers in the above delivery server products. By using this method, the Java code can be greatly protected from being leaked.
[0044] Optionally, in this embodiment, the code to be encrypted may be, but is not limited to, a delivery server code, and may be, but is not limited to, a Java code.
[0045] Optionally, in this embodiment, the code to be encrypted is packaged to obtain a first code package. It can be understood that the code to be decrypted is packaged into a jar package or a war package.
[0046] It should be noted that if the code does not need to be encrypted, it can be run directly. You only need to specify the classpath, use java-jar followed by the classpath environment variable, the jar or war package path, and the main function name to run the java project.
[0047] Optionally, in this embodiment, an encryption package is compiled based on an encryption function. It should be noted that the encryption function may be, but is not limited to, a function written in C language, which is used to perform an XOR operation on the input character stream according to its position. An encryption password is set, and the character stream is XORed with each bit of the password as a group with the length of the password to generate a new character stream output.
[0048] Optionally, in this embodiment, obtaining the character stream to be encrypted corresponding to the first code package may include, but is not limited to, first reading all character streams corresponding to the first code package, and determining the character stream to be encrypted according to the code path.
[0049] Optionally, in this embodiment, when the character stream to be encrypted corresponding to the first code package is obtained, the encryption package is called to encrypt the character stream to be encrypted through the encryption function to generate an encrypted second code package.
[0050] To further illustrate, taking Java code as an example, for the jar or war package generated by compiling the Java code, use JNI technology to call the encryption package to read the encrypted package content, encrypt the character stream in the compiled jar package or war package through an encryption algorithm, and generate a new jar package. At this time, the source code content of the jar package cannot be viewed through decompilation.
[0051] Through the embodiments provided by the present application, the code to be encrypted is packaged to obtain a first code package; based on the encryption function, the encrypted package is compiled and generated; when the character stream to be encrypted corresponding to the first code package is obtained, the encrypted package is called to encrypt the character stream to be encrypted through the encryption function to generate an encrypted second code package, thereby achieving the purpose of preventing the code from being decompiled and read, thereby realizing the technical effect of improving the encryption quality of the code.
[0052] As an optional solution, before calling the encryption package to encrypt the character stream through the encryption function to generate an encrypted second code package, the method further includes:
[0053] Read all character streams corresponding to the first code package;
[0054] According to the code path where the code to be encrypted is located, a character stream that meets the expected encryption conditions is determined from all the character streams, and the character stream that meets the expected encryption conditions is determined as the character stream to be encrypted, wherein the expected encryption condition is used to indicate that the code path where the code associated with the character stream is located is the expected code path.
[0055] Optionally, in this embodiment, the method of reading the character stream may include but is not limited to: using the file operation function provided by the programming language to open the code package file and obtain the file handle; using the read function to read the content of the code package file line by line or character by character through the file handle; processing the read character stream, such as printing to the console, storing to a variable, etc.; after the reading is completed, using the file operation function to close the file and release resources.
[0056] It should be noted that the specific implementation method may vary depending on the programming language and file format used, and a suitable reading method may be selected according to the specific situation, which is not limited in this embodiment.
[0057] Optionally, in this embodiment, the code path of the code associated with the character stream will be judged first, and only the character stream part that meets the matching path will be determined as the code to be encrypted, and then encrypted subsequently, and combined with the unencrypted character stream to determine the corresponding second code package.
[0058] Optionally, in this embodiment, in addition to being used to indicate that the code path where the code associated with the character stream is located is the expected code path, the expected encryption condition may also be, but is not limited to, indicating that the access frequency of the code associated with the character stream is greater than a preset frequency threshold, thereby achieving the purpose of encrypting only important codes with higher access frequencies in the entire character stream.
[0059] Optionally, in this embodiment, the expected encryption condition may also include but is not limited to indicating an external API call, thereby achieving the purpose of focusing on encrypting the code of the external API call to protect the API key from being leaked.
[0060] Optionally, in this embodiment, the expected encryption condition may also, but is not limited to, indicate compliance with the expected version, thereby achieving the purpose of specifying encryption of a specific version of the code, thereby improving the flexibility and controllability of code encryption.
[0061] Optionally, in this embodiment, the expected encryption condition may also include but is not limited to indicating compliance with abnormal behavior, thereby encrypting the abnormal code that complies with the abnormal behavior to avoid exposing hidden defects of the program itself after the abnormal code is leaked.
[0062] Optionally, in this embodiment, the expected encryption condition may also include but is not limited to indicating the carrying of keywords or key fields, thereby achieving batch encryption operations on codes with keywords or key fields, thereby achieving improved flexibility and efficiency of code encryption.
[0063] It should be noted that the above-mentioned expected encryption conditions can be used alone or in combination to provide more comprehensive security protection and efficiency improvement. This embodiment does not specifically limit the order of use and the number of combinations when multiple expected encryption conditions are used in combination.
[0064] Through the embodiments provided by the present application, the code path where the code associated with the character stream is located is judged, and only the character stream part that meets the matching path will be determined as the code to be encrypted, thereby achieving the purpose of lambda live control of code encryption, thereby achieving the technical effect of improving the flexibility and efficiency of code encryption.
[0065] As an optional solution, encrypting the character stream to be encrypted to generate an encrypted second code package includes:
[0066] Obtaining an encryption password associated with the encryption function;
[0067] Using the encryption password to perform an XOR operation on the character stream to be encrypted to obtain an encrypted character stream;
[0068] The encrypted character stream is compiled to generate the second code package.
[0069] Optionally, in this embodiment, an XOR operation is a commonly used algorithm in encryption and decryption. In XOR encryption, data is encrypted by performing an XOR operation on data bits and key bits, and then decrypted by the same operation.
[0070] Optionally, in this embodiment, an encryption password associated with the encryption function is predefined and used to perform an XOR operation with the data. The encryption function performs an XOR operation on the input character stream and the key to generate an encrypted character stream.
[0071] It should be noted that the decryption function is similar to the encryption function. Since the XOR operation is reversible, the data can be restored using the same key and the same XOR operation.
[0072] Through the embodiments provided in the present application, an XOR operation is performed on the encrypted character stream through the encryption password associated with the encryption function to achieve encryption of the character stream, and an encrypted code package is compiled based on this, thereby achieving the purpose of effectively encrypting the code, thereby achieving the technical effect of improving the quality of code encryption.
[0073] As an optional solution, using the encryption password to perform an XOR operation on the character stream to be encrypted to obtain an encrypted character stream includes:
[0074] Taking the length of the encryption password as a group, performing an XOR operation with each character of the character stream to be encrypted, generating each encrypted character corresponding to each character of the character stream to be encrypted;
[0075] Based on each encrypted character, the encrypted character stream is determined.
[0076] Optionally, in this embodiment, the length of the encryption password is taken as a group, and an XOR operation is performed with each character of the character stream to be encrypted to generate each encrypted character corresponding to each character of the character stream to be encrypted.
[0077] To further illustrate, an optional schematic diagram of implementing character encryption through XOR operation is as follows: Figure 3 As shown in FIG. 1 , the original stream (i.e., the original character stream) is subjected to an XOR operation of the encryption password to obtain an encrypted stream (i.e., an encrypted character stream). It should be noted that an optional schematic diagram of implementing character decryption through an XOR operation is shown in FIG. Figure 4 As shown, the encrypted stream (i.e., the encrypted character stream) is subjected to an XOR operation with the decryption password (the same as the encryption password) to obtain the original stream (i.e., the original character stream).
[0078] Through the embodiments provided in the present application, an XOR operation is performed on the encrypted character stream through the encryption password associated with the encryption function to achieve encryption of the character stream, and an encrypted code package is compiled based on this, thereby achieving the purpose of effectively encrypting the code, thereby achieving the technical effect of improving the quality of code encryption.
[0079] As an optional solution, after compiling and generating the encryption package based on the encryption function, the method further includes:
[0080] The encryption package is deployed in a proxy library in an application program, wherein the application program is used to encrypt the code to be encrypted.
[0081] Optionally, in this embodiment, taking the code as java code as an example, the proxy library is a Java virtual machine (JVM) proxy library specified by the developer, which can be loaded when the JVM is started to perform some tasks, such as performance monitoring, memory analysis, etc. The proxy library is used for advanced purposes such as debugging and performance analysis, and provides a capability to modify the behavior of the JVM at runtime.
[0082] It can be understood that the encryption package is deployed in the proxy library in the application, and when the application starts, the encryption package is loaded and called from the proxy library, so that the code is encrypted quickly and efficiently.
[0083] As an optional solution, before packaging the code to be encrypted to obtain the first code package, the method further includes:
[0084] Obtaining a Java code to be encrypted, wherein the Java code to be encrypted is used for delivery server code development;
[0085] Determine the java code to be encrypted as the code to be encrypted;
[0086] Before compiling and generating an encryption package based on the encryption function, the method further includes:
[0087] Use C language to encode the encryption function.
[0088] Optionally, in this embodiment, taking the code as java code as an example, before obtaining the first code package, the java code to be encrypted is obtained, wherein the java code to be encrypted is used for delivery server code development, and the java code to be encrypted is determined as the code to be encrypted.
[0089] Optionally, in this embodiment, before compiling and generating the encryption package, the encryption function is encoded using C language.
[0090] It should be noted that Java is a cross-platform language, while C is a non-cross-platform language, and it is extremely difficult to decrypt it. In this way, based on the encryption function encoded in C language, the character stream of Java language is encrypted, which can make the encrypted code package difficult to crack, thereby achieving the technical effect of improving the quality of code encryption.
[0091] As an optional solution, the above code encryption method is applied to the loading and decryption scenario of Java code. In this scenario, after the Java project is developed, the code is packaged into a jar package or a war package. If it is not encrypted for running, you only need to specify the classpath, use java-jar followed by the classpath environment variable, the jar or war package path, and the main function name to run the Java project.
[0092] If encryption is required, the encryption function and decryption function are written in C language, and encrypt.so and decrypt.so are generated through compilation as encryption and decryption libc packages respectively. Since C language is not a cross-platform language, it is extremely difficult to decrypt it. For the jar or war package generated by java code compilation, encrypt.so is called by JNI technology to read the jar package content, and the character stream in the jar package is encrypted by the encryption algorithm to generate a new jar package. At this time, the jar package cannot view the source code content by decompilation. At runtime, decrypt.so needs to be put into the java environment variable. When the jvm loads the jar package, decrypy.so is loaded to decrypt the jar package code that meets its rules using the decryption algorithm, and loaded into the memory for operation and processing.
[0093] Optionally, in this scenario, C language is used to write encryption and decryption algorithm functions. The encryption algorithm mainly performs XOR operations on the input character stream according to its position. An encryption password can be set. The character stream is XORed with each bit of the password as a group with the length of the password to generate a new character stream output. During the encryption process, the java function is responsible for reading the jar package as a character stream, and can choose to encrypt the key part of the code or encrypt all of it. By calling the encryption function from JNI to C language, the encrypted stream returned by C language is regenerated into a jar package.
[0094] To further illustrate, a Java code encryption relationship flow chart is as follows Figure 5 As shown, it includes: reading the packaged jar package as a string, judging whether encryption is required according to the code path, and if encryption is required, encrypting the corresponding string by group XOR, and returning the character stream to generate an encrypted jar package.
[0095] It should be noted that the main process of the decryption function is to perform an XOR operation on the input character stream according to the same password stream used in the encryption function and return the character stream, which is input into the jvm memory.
[0096] Optionally, in this scenario, regarding the program startup method, agentlib in java-jar-agent is used to load a specific library in a Java application to perform a specific task or provide additional functions.
[0097] In Java, the -agentIib option allows developers to specify a Java virtual machine (JVM) agent library, which can be loaded when the JVM starts and used to perform specific tasks, such as performance monitoring and memory analysis. Agent libraries are usually used for advanced purposes such as debugging and performance analysis. They provide the ability to modify JVM behavior at runtime. Specifying agentlib as the decrypt.so package deployed on the server allows the decryption function to be loaded first when the program starts. In the process of loading the jar package, the path will be judged first, and only the character stream part that matches the matching path will pass through the decryption function. The schematic diagram of the encryption and decryption function recovering the metacharacter stream is shown below. Figure 3 and Figure 4 As shown, the left side of the figure shows the process of encrypting and recovering a single character, and the right side shows the process principle of encrypting and recovering a character stream of length 8.
[0098] It should be noted that in this scenario, based on the above code encryption method, first encrypt the compiled jar package to generate an encrypted package, specifically use java code to read the jar package as a character stream, use JNI to call libc's encrypt.so package to encrypt the character stream, generate an encrypted package from the encrypted character stream, and put the encrypted jar package and the decryption package decrypt.so package into the server. Secondly, during the program running process, the decryption package decrypt.so is loaded first when the jvm is loaded, and the character stream is decrypted according to the matching principle, and then the decrypted character stream is loaded into the memory for the normal operation of the program.
[0099] It is understandable that for projects developed using the Java language on delivery server products, the jar package of the project is encrypted, and the jvm decrypts the jar package when it is used, so that the program can run normally. The compiled jar package is encrypted to generate an encrypted package, specifically using java code to read the jar package as a character stream, using JNI to call libc's encrypt.so package to encrypt the character stream, generating an encrypted package from the encrypted character stream, and placing the encrypted jar package and the decryption package decrypt.so package into the server. Secondly, during the program running process, the decryption package decrypt.so is loaded first when the jvm is loaded, and the character stream is decrypted according to the matching principle, and then the decrypted character stream is loaded into the memory for the normal operation of the program.
[0100] Through the embodiments provided by the present application, first, the integrity and confidentiality of the code are protected to prevent malicious attackers from obtaining code logic and key information through decompilation and other means. For example, in Java applications, encrypted code can prevent trading algorithms and user data processing logic from being easily stolen, reducing the risk of fraud. Reduce the possibility of security vulnerabilities being exploited due to code leakage. Even if an attacker obtains part of the code fragment, it is difficult to understand and exploit the entire code system due to the existence of encryption.
[0101] Second, it can effectively prevent competitors from maliciously copying codes, protect their own development results from being stolen, and encourage developers to invest more energy in innovation and research and development.
[0102] Third, it can prevent malicious code injection and tampering to a certain extent. In complex network environments, it can reduce system crashes and anomalies caused by external malicious code modification. For example, in enterprise-level server applications, encrypted code can ensure the stable operation of the server and prevent service interruptions caused by code tampering. It helps to establish a reliable software operating environment, enhance users' trust in the software system, and improve the market reputation and user satisfaction of the software.
[0103] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present application.
[0104] In the present embodiment, a code encryption device is also provided, which is used to implement the above-mentioned embodiments and preferred implementation modes, and the descriptions thereof will not be repeated. As used below, the term "module" can implement a combination of software and / or hardware of a predetermined function. Although the device described in the following embodiments is preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and contemplated.
[0105] Figure 6 is a structural block diagram of a code encryption device according to an embodiment of the present application, such as Figure 6 As shown, the device comprises:
[0106] A packaging unit, used for packaging the code to be encrypted to obtain a first code package;
[0107] A compilation unit, used to compile and generate an encryption package based on the encryption function;
[0108] The encryption unit is used to call the encryption package when the character stream to be encrypted corresponding to the first code package is obtained, so as to encrypt the character stream to be encrypted through the encryption function and generate an encrypted second code package.
[0109] Optionally, in this embodiment, the code encryption method can be applied to, but is not limited to, Java source code protection scenarios. Java source code protection refers to encrypting, obfuscating or otherwise protecting Java code through various technical means to prevent the code from being reverse engineered, misappropriated or leaked. With the intensification of software piracy and technological competition, the importance of source code protection is increasing. Since Java is a cross-platform language, there are many methods and means on the market for decompiling Java .cIass files, but server products, as delivery-type products, often do not want customers or peers to be able to view the Java source code developed on the server at will. Therefore, it is necessary to encrypt and protect the developed code, and appropriate decryption means are required during operation to ensure that the program can run smoothly.
[0110] Optionally, the encryption method mainly includes the following:
[0111] Code obfuscation: The tools used can be ProGuard, ZelixKlassMaster, etc. This method is an obfuscation technology that makes the code difficult to understand by renaming classes, methods, and fields, but the function of the bytecode remains unchanged. This is one of the most commonly used and effective protection methods, but the drawback is that the code can still be decompiled, and through careful study, the logical function of the source code can still be understood.
[0112] Packaging and packaging, by packaging the code into a Jar or War file, can reduce the risk of direct access to the source code to a certain extent. However, there are still tools that can decompile and read it.
[0113] Dynamic code loading hides the code and dynamically loads it from the server when the code is executed, reducing the amount of code stored locally. However, when loading the code, the jar package can be copied and then decompiled. The code cannot be effectively encrypted to protect it.
[0114] Based on the above problems, this embodiment proposes a code encryption method, which can solve the problem that the Java project code is decompiled and read by customers or peers in the above delivery server products. By using this method, the Java code can be greatly protected from being leaked.
[0115] Optionally, in this embodiment, the code to be encrypted may be, but is not limited to, a delivery server code, and may be, but is not limited to, a Java code.
[0116] Optionally, in this embodiment, the code to be encrypted is packaged to obtain a first code package. It can be understood that the code to be decrypted is packaged into a jar package or a war package.
[0117] It should be noted that if the code does not need to be encrypted, it can be run directly. You only need to specify the classpath, use java-jar followed by the classpath environment variable, the jar or war package path, and the main function name to run the java project.
[0118] Optionally, in this embodiment, an encryption package is compiled based on an encryption function. It should be noted that the encryption function may be, but is not limited to, a function written in C language, which is used to perform an XOR operation on the input character stream according to its position. An encryption password is set, and the character stream is XORed with each bit of the password as a group with the length of the password to generate a new character stream output.
[0119] Optionally, in this embodiment, obtaining the character stream to be encrypted corresponding to the first code package may include, but is not limited to, first reading all character streams corresponding to the first code package, and determining the character stream to be encrypted according to the code path.
[0120] Optionally, in this embodiment, when the character stream to be encrypted corresponding to the first code package is obtained, the encryption package is called to encrypt the character stream to be encrypted through the encryption function to generate an encrypted second code package.
[0121] To further illustrate, taking Java code as an example, for the jar or war package generated by compiling the Java code, use JNI technology to call the encryption package to read the encrypted package content, encrypt the character stream in the compiled jar package or war package through an encryption algorithm, and generate a new jar package. At this time, the source code content of the jar package cannot be viewed through decompilation.
[0122] Through the embodiments provided by the present application, the code to be encrypted is packaged to obtain a first code package; based on the encryption function, the encrypted package is compiled and generated; when the character stream to be encrypted corresponding to the first code package is obtained, the encrypted package is called to encrypt the character stream to be encrypted through the encryption function to generate an encrypted second code package, thereby achieving the purpose of preventing the code from being decompiled and read, thereby realizing the technical effect of improving the encryption quality of the code.
[0123] As an optional solution, the device further includes:
[0124] A reading module, used for reading all character streams corresponding to the first code package before calling the encryption package to encrypt the character stream through the encryption function to generate an encrypted second code package;
[0125] The first determination module is used to determine, from all the character streams, a character stream that meets the expected encryption condition according to the code path where the code to be encrypted is located, before calling the encryption package to encrypt the character stream through the encryption function to generate an encrypted second code package, and determine the character stream that meets the expected encryption condition as the character stream to be encrypted, wherein the expected encryption condition is used to indicate that the code path where the code associated with the character stream is located is the expected code path.
[0126] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.
[0127] As an optional solution, the encryption unit includes:
[0128] A first acquisition module, used to acquire an encryption password associated with the encryption function;
[0129] An XOR module, used for performing an XOR operation on the character stream to be encrypted using the encryption password to obtain an encrypted character stream;
[0130] A compiling module is used to compile the encrypted character stream to generate the second code package.
[0131] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.
[0132] As an optional solution, the XOR module includes:
[0133] An XOR submodule, used for performing an XOR operation on each character of the character stream to be encrypted, taking the length of the encryption password as a group, to generate each encrypted character corresponding to each character of the character stream to be encrypted;
[0134] The determination submodule is used to determine the encrypted character stream based on each encrypted character.
[0135] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.
[0136] As an optional solution, the device further includes:
[0137] A deployment module is used to deploy the encryption package in a proxy library in an application after the encryption package is compiled and generated based on the encryption function, wherein the application is used to encrypt the code to be encrypted.
[0138] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.
[0139] As an optional solution, the device further includes:
[0140] A second acquisition module is used to acquire the Java code to be encrypted before packaging the code to be encrypted to obtain the first code package, wherein the Java code to be encrypted is used for delivery server code development;
[0141] A second determining module is used to determine the Java code to be encrypted as the code to be encrypted before packaging the code to be encrypted to obtain a first code package;
[0142] The device also includes:
[0143] The encoding module is used to encode the encryption function using C language before compiling and generating an encryption package based on the encryption function.
[0144] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.
[0145] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present application.
[0146] It should be noted that the above modules can be implemented by software or hardware. For the latter, it can be implemented in the following ways, but not limited to: the above modules are all located in the same processor; or the above modules are located in different processors in any combination.
[0147] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored, wherein the computer program is configured to execute the steps of any of the above method embodiments when running.
[0148] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk.
[0149] An embodiment of the present application further provides an electronic device, including a memory and a processor, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.
[0150] In an exemplary embodiment, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor, and the input / output device is connected to the processor.
[0151] An embodiment of the present application further provides a computer program product, comprising a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores a computer program product, and when the computer program is executed by a processor, the steps of the method described in each embodiment of the present application are implemented.
[0152] For specific examples in this embodiment, reference may be made to the examples described in the above embodiments and exemplary implementation modes, and this embodiment will not be described in detail herein.
[0153] Obviously, those skilled in the art should understand that the above modules or steps of the present application can be implemented by a general computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices, they can be implemented by a program code executable by a computing device, so that they can be stored in a storage device and executed by the computing device, and in some cases, the steps shown or described can be executed in a different order from that herein, or they can be made into individual integrated circuit modules, or multiple modules or steps therein can be made into a single integrated circuit module for implementation. Thus, the present application is not limited to any specific combination of hardware and software.
[0154] The above description is only the preferred embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the principles of the present application shall be included in the protection scope of the present application.
Claims
1. A code encryption method, characterized in that: include: Packing the code to be encrypted to obtain a first code package; Based on the encryption function, compile and generate the encryption package; When the character stream to be encrypted corresponding to the first code package is obtained, the encryption package is called to encrypt the character stream to be encrypted through the encryption function to generate an encrypted second code package.
2. The method according to claim 1, characterized in that Before calling the encryption package to encrypt the character stream through the encryption function to generate an encrypted second code package, the method further includes: Read all character streams corresponding to the first code package; According to the code path where the code to be encrypted is located, a character stream that meets the expected encryption conditions is determined from all the character streams, and the character stream that meets the expected encryption conditions is determined as the character stream to be encrypted, wherein the expected encryption condition is used to indicate that the code path where the code associated with the character stream is located is the expected code path.
3. The method according to claim 1, characterized in that The step of encrypting the character stream to be encrypted to generate an encrypted second code package includes: Obtaining an encryption password associated with the encryption function; Using the encryption password to perform an XOR operation on the character stream to be encrypted to obtain an encrypted character stream; The encrypted character stream is compiled to generate the second code package.
4. The method according to claim 3, characterized in that The using the encryption password to perform an XOR operation on the character stream to be encrypted to obtain an encrypted character stream includes: Taking the length of the encryption password as a group, performing an XOR operation with each character of the character stream to be encrypted, generating each encrypted character corresponding to each character of the character stream to be encrypted; Based on each encrypted character, the encrypted character stream is determined.
5. The method according to any one of claims 1 to 4, characterized in that: After compiling and generating the encryption package based on the encryption function, the method further includes: The encryption package is deployed in a proxy library in an application program, wherein the application program is used to encrypt the code to be encrypted.
6. The method according to any one of claims 1 to 4, characterized in that: Before packaging the code to be encrypted to obtain the first code package, the method further includes: Obtaining a Java code to be encrypted, wherein the Java code to be encrypted is used for delivery server code development; Determine the java code to be encrypted as the code to be encrypted; Before compiling and generating an encryption package based on the encryption function, the method further includes: Use C language to encode the encryption function.
7. A code encryption device, characterized in that: include: A packaging unit, used for packaging the code to be encrypted to obtain a first code package; A compilation unit, used to compile and generate an encryption package based on the encryption function; The encryption unit is used to call the encryption package when the character stream to be encrypted corresponding to the first code package is obtained, so as to encrypt the character stream to be encrypted through the encryption function and generate an encrypted second code package.
8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program implements the steps of the method according to any one of claims 1 to 6 when executed by a processor.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.
10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.
