A source code encryption method based on compiled languages
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-02-06
- Publication Date
- 2026-08-14
AI Technical Summary
但这样仍有可能代码被破解或强行修改局部数据,而导致代码逻辑泄漏或与产生与设计不一致的运行状态或运行错误,并且系统软件也不像独立软件一样可以任意修改系统接口函数名称,其源码也是不能随意修改混淆的
[0009]本发明提供一种基于编译型语言的源码加密方法,在整个源码加密编译过程中,仅存在加密源代码的过程,没有解密为明文源码文件的过程,而是在编译器执行过程直接在程序地址空间内解密并编译,从而达到较好的源代码保护目的。
Smart Images

Figure CN116226881B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer software information security, specifically to a source code encryption method based on compiled languages. Background Technology
[0002] Compiled languages exist in contrast to interpreted languages. Compiled languages first compile source code into machine language, and then the machine runs the machine code (binary executable program).
[0003] Unlike ordinary computer application software, which can be released to users as compiled executable files to protect source code intellectual property rights, cross-platform programs typically need to be recompiled for different processor architectures or operating system versions to function correctly. Furthermore, in certain specialized industries, such as the military, plaintext source code cannot be provided to users due to intellectual property rights or the security of special equipment.
[0004] Currently, source code protection typically employs code obfuscation, which transforms variable and function names into meaningless strings to obscure the code's logical meaning and protect the source code without affecting compilation. However, this still allows the code to be cracked or partially modified, potentially leading to code logic leaks, inconsistent runtime states, or errors. Furthermore, unlike standalone software, system software cannot have its interface function names arbitrarily modified, and its source code cannot be easily obfuscated. Summary of the Invention
[0005] This invention provides a source code encryption method based on a compiled language, comprising the following steps: S1. Generating an asymmetric key pair using the OpenSSL software library; S2. Generating a code encryption tool based on the asymmetric algorithm and the public key generated in step S1; S3. Encrypting the target source code using the code encryption tool to obtain an encrypted source code file with the same name; S4. Modifying the compiler source code, adding the asymmetric algorithm, importing the private key generated in step S1 into the compiler source code, and modifying the compiler processing logic to form an executable file; S5. Exporting and distributing the encrypted source code file with the same name generated in step S3 and the modified compiler executable file in step S4; S6. Compiling the target source code using the distribution package in step S5 and executing it.
[0006] Further, step S4 includes the following sub-steps: S41. Encrypt the private key and import it into the compiler's executable address space; S42. Decrypt the encrypted private key to obtain a valid private key for decrypting the target source code; S43. Read the encrypted target source code file with the same name; S44. Calculate the ciphertext hash value of the target source code and compare it with the hash check header. If correct, continue execution; otherwise, terminate the compiler; S45. Decrypt the target source code into the compiler's executable address space using an asymmetric algorithm; S46. Compile the decrypted target source code file.
[0007] Furthermore, the code encryption tool generated in step S2 is written in C language.
[0008] Furthermore, steps S1 to S5 are all executed in a secure environment, while step S6 is executed in the target environment; asymmetric encryption algorithms are mainly used in steps S1 to S6.
[0009] This invention provides a source code encryption method based on compiled languages. In the entire source code encryption and compilation process, there is only the process of encrypting the source code, without the process of decrypting it into plaintext source code files. Instead, the decryption and compilation are performed directly in the program address space during the compiler execution process, thereby achieving a better source code protection purpose. Attached Figure Description
[0010] Figure 1 A flowchart illustrating a source code encryption method based on a compiled language provided by this invention;
[0011] Figure 2 The flowchart shows a modified compiler for a source code encryption method based on a compiled language provided by this invention. Detailed Implementation
[0012] The following detailed description of the implementation method of the present invention is in conjunction with the accompanying drawings. The description is only a partial embodiment and not all embodiments. For clarity, representations and descriptions unrelated to the present invention are omitted in the drawings and description.
[0013] Both SM2 and RSA are public-key cryptography algorithms. SM2 is a more advanced and secure algorithm, and it has been used to replace RSA in China's commercial cryptography system. With the development of cryptographic and computer technologies, the commonly used 1024-bit RSA algorithm faces serious security threats. After research, China's cryptography management department decided to replace RSA with the SM2 elliptic curve cryptography algorithm. This invention also uses the SM2 algorithm.
[0014] like Figure 1As shown, a source code encryption method based on a compiled language includes the following steps: S1. Generating an asymmetric key pair using the OpenSSL software library; S2. Generating a code encryption tool based on the asymmetric algorithm and the public key generated in step S1; S3. Encrypting the target source code using the code encryption tool to obtain an encrypted source code file with the same name; S4. Modifying the compiler source code, adding the asymmetric algorithm, importing the private key generated in step S1 into the compiler source code, and modifying the compiler processing logic to form an executable file; S5. Exporting and distributing the encrypted source code file with the same name generated in step S3 and the modified compiler executable file in step S4; S6. Compiling the target source code using the distribution package in step S5 and executing it.
[0015] In step S1: A public-private key pair is generated using an asymmetric algorithm. This key pair is used in a secure environment and is not publicly disclosed. In practice, an asymmetric key pair is generated using the OpenSSL software library; however, it is not limited to this and symmetric keys can also be generated.
[0016] In step S2: The code encryption tool implements an asymmetric algorithm and imports the public key from S1 to compile the source code file. This tool is used in a secure environment and its source code or executable file is not released to the public. It actually uses an asymmetric software algorithm, but is not limited to this; a symmetric algorithm can also be used.
[0017] In step S3: The tool from S2 is used to encrypt the target source code, resulting in an encrypted source code file with the same name. Although the encrypted file shares the same name as the source code, it is no longer readable text. Forcibly opening it with a text editor will only display garbled characters, making it impossible to change the code logic or its variables. The encrypted file contains two parts: a hash check header and the ciphertext of the source code.
[0018] In step S4: Modify the compiler source code to add a software asymmetric algorithm; simultaneously, encrypt the private key generated in S1 and import it into the compiler source code; modify the compiler's processing logic to enable it to directly compile encrypted source code files. The resulting custom compiler now contains the asymmetric algorithm and the encrypted private key, and can directly compile encrypted source code. The compiler source code will not be released; only the custom compiler executable file will be distributed.
[0019] Step S4 includes the following sub-steps: S41. Encrypt the private key and import it into the compiler's executable address space; S42. Decrypt the encrypted private key to obtain a valid private key for decrypting the target source code; S43. Read the encrypted target source code file with the same name; S44. Calculate the ciphertext hash value of the target source code and compare it with the hash check header. If correct, continue execution; otherwise, terminate the compiler; S45. Decrypt the target source code into the compiler's executable address space using an asymmetric algorithm; S46. Compile the decrypted target source code file.
[0020] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above descriptions are merely specific embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the protection scope of the present invention.
Claims
1. A source code encryption method based on a compiled language, characterized in that, Includes the following steps: S1. Use the OpenSSL software library to generate asymmetric key pairs; S2. Generate a code encryption tool based on the asymmetric algorithm and the public key generated in step S1; S3. Use a code encryption tool to encrypt the target source code to obtain an encrypted source code file with the same name; S4. Modify the compiler source code, add an asymmetric algorithm, import the private key generated in step S1 into the compiler source code, and modify the compiler processing logic to form an executable file; S5. Export and distribute the encrypted source code file with the same name generated in step S3 and the modified compiler executable file in step S4; S6. Compile the target source code using the release package from step S5, and execute it; Step S4 includes the following sub-steps: S41. The private key is encrypted and then imported into the compiler's executable program address space; S42. Decrypt the encrypted private key to obtain a valid private key for decrypting the target source code; S43. Read the encrypted file with the same name as the target source code; S44. Calculate the encrypted hash value of the target source code, compare it with the hash check header, and if it is correct, continue execution; otherwise, terminate the compiler execution. S45. Use an asymmetric algorithm to decrypt the target source code into the compiler's executable address space; S46. The compiled and decrypted target source code file; Steps S1 to S5 are all executed in a secure environment, while step S6 is executed in the target environment; asymmetric encryption algorithms are mainly used in steps S1 to S6.
2. The source code encryption method based on a compiled language according to claim 1, characterized in that, The code encryption tool generated in step S2 is written in C language.
Citation Information
Patent Citations
Compiler that decrypts encrypted source code
US6684389B1