Program encryption method and device, storage medium and program product
By employing a combination of asymmetric and symmetric encryption methods to encode WebAssembly programs, hiding the core code, and introducing environment verification logic, the problem of WebAssembly files being easily decompiled and tampered with is solved, achieving high security and reliable protection for programs in edge computing environments.
Patent Information
- Application Number
- CN202511402493.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-28
- Publication Date
- 2025-12-02
AI Technical Summary
WebAssembly files are easily decompiled and analyzed, leading to program leaks and tampering, especially in high-security environments such as edge computing, where serious code leakage problems exist.
The WebAssembly program is encrypted using a combination of asymmetric and symmetric encryption. The core code is hidden by generating custom sections, and additional logic such as time windows and device address verification is introduced during the decryption process to ensure that only legitimate environments can decrypt it.
It effectively prevents code leakage and tampering in illegal environments, improves program security and resistance to attacks, and ensures that only legitimate nodes can decrypt and execute core logic.
Smart Images

Figure CN121051722A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular to a program encryption method, apparatus, storage medium, and program product. Background Technology
[0002] WebAssembly (hereinafter referred to as Wasm) is a newly emerging, portable, and high-performance virtual machine instruction set architecture. Wasm was designed to achieve cross-platform compatibility and fast parsing; therefore, Wasm files generated by compiling high-level languages retain rich structured information, such as function signatures, type information, and control flow structures.
[0003] However, this characteristic also makes it easy to decompile, analyze, and reconstruct into more readable source code. This creates a vulnerability to program leaks. Summary of the Invention
[0004] This application provides a program encryption method, apparatus, storage medium, and program product, which can improve the problem of program leakage and ensure program security.
[0005] To achieve the above objectives, the embodiments of this application adopt the following technical solutions:
[0006] In a first aspect, a program encryption method is provided, which is applied to a first device. The method includes: obtaining a public key from a key pair and the source code of an application; compiling the source code using WebAssembly to obtain raw bytecode; the key pair is the same as the key pair in a second device, which is a device for deploying the application; generating a symmetric key based on the public key; encrypting the raw bytecode using the symmetric key to obtain encrypted bytecode; and encrypting the symmetric key using the public key to obtain an encrypted symmetric key; rewriting code sections in the raw bytecode to generate rewritten bytecode; the function sections of the rewritten bytecode are the same as the function sections of the raw bytecode, and the function code logic in the code sections of the rewritten bytecode satisfies the function declarations in the function sections; and writing the encrypted bytecode, the decryption logic of the encrypted bytecode, and the encrypted symmetric key into a custom section of the rewritten bytecode to obtain an encrypted program package.
[0007] In one possible implementation of the first aspect, the method further includes generating a name for the custom section based on the public key before writing the encrypted bytecode into the custom section of the rewritten bytecode.
[0008] The above scheme employs a combination of asymmetric and symmetric encryption, ensuring that even if the encrypted package is stolen, attackers cannot decrypt the core code. Furthermore, because the package's operation depends on decryption logic, any tampering will cause decryption failure, rendering the package inoperable. Additionally, the encrypted content is hidden within a custom section, appearing as ordinary data and thus difficult to detect.
[0009] In one possible implementation of the first aspect, generating the name of the custom section based on the public key includes: calculating a hash value for the public key; converting a preset byte of the hash value into a string to obtain the name of the custom section.
[0010] This scheme hashes the public key and converts a portion of the hash value into a specific string to serve as the name of a custom section, ensuring that each encrypted package has a unique, hidden entry point corresponding to the target node. This approach not only enhances the concealment and targeting of encrypted content, ensuring that only nodes possessing the corresponding private key can identify and trigger the decryption process, but also effectively prevents attackers from using common characteristics to identify or tamper with encrypted sections in bulk, thereby improving the overall security and resistance to attacks.
[0011] In one possible implementation of the first aspect, the decryption logic includes: reading the encrypted bytecode; decrypting the encrypted symmetric key using the private key in the key pair to obtain the symmetric key; and decrypting the encrypted bytecode using the symmetric key to obtain the original bytecode.
[0012] This scheme decrypts the symmetric key using the private key, and then uses the symmetric key to reconstruct the original bytecode, thus strongly binding the decryption process to the private key. Even if the encryption package and public key are leaked, attackers cannot successfully decrypt the code because they cannot obtain the private key stored in the secure environment. This effectively protects the code logic from being stolen or tampered with, achieving reliable end-to-end protection.
[0013] In one possible implementation of the first aspect, before distributing the encryption package to the second device, the method further includes: writing additional code logic in a custom section of the rewritten bytecode; wherein the additional code logic includes at least one of the following: executing decryption logic when the system time of the second device is within a preset time window; and executing decryption logic when the address of the second device is a preset address.
[0014] This solution adds a dynamic access control mechanism to the decryption process by embedding additional code logic such as time windows or device address verification in custom sections. Even if an attacker obtains the package and cracks the basic encryption, the decryption process can only be triggered when preset environmental conditions (such as a specific time or a specified device) are met. This effectively prevents code leakage or unauthorized execution under illegal conditions, significantly improving the protection strength of core business logic and system security.
[0015] In one possible implementation of the first aspect, the method further includes: compiling the decryption logic and the appended code logic into a dynamic link library supported by the second device; writing the decryption logic and the appended code logic in a custom section of the rewritten bytecode, including: writing the dynamic link library in the custom section of the rewritten bytecode.
[0016] This solution pre-compiles the decryption logic and additional code logic into a dynamic link library supported by the target deployment node and embeds it into a custom section. This highly encapsulates and protects the core decryption process and access control logic, effectively increasing the difficulty of reverse engineering and cracking. At the same time, it ensures the direct executableness and compatibility of the decryption component in different environments, further enhancing the overall solution's stealth and reliability.
[0017] In a second aspect, a program execution method is provided, which is applied to a second device. The method includes: obtaining an encrypted program package, the encrypted program package including rewritten bytecode, a custom section of the rewritten bytecode including encrypted bytecode, decryption logic of the encrypted bytecode, and an encrypted symmetric key; obtaining a private key from a key pair of the second device; decrypting the encrypted bytecode using the private key and the encrypted symmetric key according to the decryption logic to obtain the original bytecode; and executing the original bytecode; wherein the encrypted program package is obtained through the program encryption method in the first aspect.
[0018] In one possible implementation of the second aspect, the encryption package includes at least one bytecode file, which contains either rewritten bytecode or unencrypted raw bytecode. Before decrypting the encrypted bytecode using a private key and an encryption symmetric key according to the decryption logic to obtain the raw bytecode, the method further includes: obtaining the public key from the key pair and generating a second name for a custom section based on the public key; for each bytecode file, detecting whether the bytecode file contains a custom section with the second name; and decrypting the encrypted bytecode using the private key and an encryption symmetric key according to the decryption logic to obtain the raw bytecode, including: if the bytecode file contains a custom section with the second name, decrypting the encrypted bytecode using the private key and an encryption symmetric key according to the decryption logic in the custom section to obtain the raw bytecode.
[0019] In one possible implementation of the second aspect, the method further includes: executing the raw bytecode in the bytecode file that has not been encrypted, in the case that the custom section with the second name is not included in the bytecode file.
[0020] Thirdly, a program encryption device is provided, which is applied to a first device. The program encryption device includes a key module, a program generation module, and a distribution module.
[0021] The key module is used to obtain the public key from the key pair and the application's source code.
[0022] The program generation module is used to compile the source code into raw bytecode using WebAssembly; the key pair is the same as the key pair in the second device, which is the device for deploying the application; a symmetric key is generated based on the public key, and the raw bytecode is encrypted using the symmetric key to obtain encrypted bytecode; and the symmetric key is encrypted using the public key to obtain an encrypted symmetric key; code sections in the raw bytecode are rewritten to generate rewritten bytecode; the function sections of the rewritten bytecode are the same as the function sections of the raw bytecode, and the function code logic in the code sections of the rewritten bytecode satisfies the function declarations in the function sections; and the module is used to write the encrypted bytecode, the decryption logic of the encrypted bytecode, and the encrypted symmetric key into a custom section of the rewritten bytecode to obtain an encrypted program package.
[0023] In some embodiments, the program encryption device further includes a distribution module. The distribution module is used to distribute the encryption program package to the second device.
[0024] In one possible implementation of the third aspect, the apparatus further includes:
[0025] The name generation module is used to generate names for custom sections based on public keys.
[0026] In one possible implementation of the third aspect, the name generation module includes:
[0027] A hash calculation unit is used to calculate a hash value for a public key;
[0028] The byte conversion unit is used to convert the preset bytes of a hash value into a string to obtain the name of a custom section.
[0029] In one possible implementation of the third aspect, the decryption logic includes:
[0030] Read encrypted bytecode;
[0031] Use the private key from the key pair to decrypt the encrypted symmetric key to obtain the symmetric key;
[0032] The encrypted bytecode is decrypted using a symmetric key to obtain the original bytecode.
[0033] In one possible implementation of the third aspect, the program generation module is also used to write additional code logic in a custom section of the rewritten bytecode;
[0034] The additional code logic includes at least one of the following:
[0035] If the system time of the second device is within a preset time window, the decryption logic is executed.
[0036] If the address of the second device is a preset address, the decryption logic is executed.
[0037] In one possible implementation of the third aspect, the program generation module is also used to compile the decryption logic and the additional code logic into a dynamic link library supported by the second device; the dynamic link library is written in a custom section of the rewritten bytecode.
[0038] Fourthly, a program execution device is provided, applied to a second device. The device includes: an execution module for acquiring an encrypted program package, the encrypted program package including rewritten bytecode, and a custom section of the rewritten bytecode including encrypted bytecode, decryption logic of the encrypted bytecode, and an encrypted symmetric key; a key module for acquiring a private key from a key pair of the second device; an execution module for decrypting the encrypted bytecode according to the decryption logic using the private key and the encrypted symmetric key to obtain the original bytecode; and an execution module for executing the original bytecode.
[0039] The encrypted package is obtained through the program encryption method described in the first aspect.
[0040] In one possible implementation of the fourth aspect, the encryption package includes at least one bytecode file, which includes either rewritten bytecode or unencrypted raw bytecode.
[0041] The key module is used to retrieve the public key from the key pair.
[0042] The execution module is used to generate a second name for a custom section based on the public key; for each bytecode file, it checks whether the bytecode file contains a custom section with the second name; if the bytecode file contains a custom section with the second name, it decrypts the encrypted bytecode using the private key and the encryption symmetric key according to the decryption logic in the custom section to obtain the original bytecode.
[0043] In one possible implementation of the fourth aspect, the execution module is also used to execute the raw bytecode in the bytecode file that has not been encrypted, even if the bytecode file does not include a custom section with a second name.
[0044] Fifthly, a first apparatus is provided, comprising: a memory and at least one processor. The memory is communicatively connected to the processor. The memory is used to store computer program code, which includes computer instructions. When the processor executes the computer instructions, it causes the electronic device to perform the method as described in the first aspect and any possible implementation thereof.
[0045] Sixthly, embodiments of this application provide a computer-readable storage medium storing computer instructions. When executed by a processor, these computer instructions are used to implement the method described in the first aspect and any possible implementation thereof.
[0046] In a seventh aspect, embodiments of this application provide a computer program product that, when run on a computer or executed by a computer's processor, implements the method described in the first aspect and any possible design thereof. The computer may be the electronic device described in the second aspect and any possible implementation thereof.
[0047] It is understood that the beneficial effects achieved by the program encryption device described in the third aspect, the first device described in the fifth aspect, the computer-readable storage medium described in the sixth aspect, and the computer program product described in the seventh aspect can be referred to as the beneficial effects in the first aspect and any possible implementation thereof, and will not be repeated here. Attached Figure Description
[0048] Figure 1 This is a schematic diagram of the structure of a program encryption system provided in an embodiment of this application;
[0049] Figure 2 A schematic diagram of a program encryption method provided in this application embodiment. Figure 1 ;
[0050] Figure 3 A schematic diagram of a program encryption method provided in this application embodiment. Figure 2 ;
[0051] Figure 4 A schematic diagram illustrating a program execution method provided in an embodiment of this application;
[0052] Figure 5 This is a schematic diagram of the structure of a program encryption device provided in an embodiment of this application;
[0053] Figure 6 This is a schematic diagram of the structure of a program execution device provided in an embodiment of this application;
[0054] Figure 7 This is a schematic diagram of the structure of a first device provided in an embodiment of this application. Detailed Implementation
[0055] Hereinafter, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this embodiment, unless otherwise stated, "a plurality of" means two or more.
[0056] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numbers in different drawings represent the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this application. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this application as detailed in the appended claims.
[0057] The technical solutions provided in this application, including the collection, storage, use, processing, transmission, provision, and disclosure of financial data or user data, comply with relevant laws and regulations and do not violate public order and good morals.
[0058] It should be noted that in the embodiments of this application, certain software, components, models and other existing solutions in the industry may be mentioned. These should be regarded as exemplary and are only intended to illustrate the feasibility of implementing the technical solution of this application. However, it does not mean that the applicant has used or necessarily used the solution.
[0059] Due to its security, high performance, and portability, Wasm has expanded from its original browser domain to numerous other fields. For example, Wasm has gradually become the mainstream choice for smart contract virtual machines in the blockchain field. Furthermore, with the gradual improvement of the Wasm standard and its development ecosystem, Wasm is also further expanding into emerging fields such as edge computing and cloud-native containerization.
[0060] However, Wasm faces the same inherent challenges in code protection as other virtual machine languages like Java. Wasm files retain rich structured information compared to other virtual machine languages, such as function signatures, type information, and control flow structures. This makes Wasm files easily decompiled, analyzed, and restored into highly readable source code. In commercial closed-source environments like edge computing, where intellectual property and security requirements are extremely high, this poses a serious code leakage problem.
[0061] In some embodiments, a large number of edge servers are deployed in physical environments with complex structures and low security, such as factory workshops and substations. These edge servers face an extremely high risk of physical intrusion or network breaches by hackers. Once a server is compromised, its stored program packages, i.e., Wasm files, are easily stolen. Due to the ease with which Wasm files can be decompiled, attackers can easily obtain and reuse the core business logic and algorithms contained within them, leading to Wasm file leaks. Furthermore, attackers may also tamper with the Wasm files on the edge servers, injecting malicious code and executing it, resulting in serious consequences such as data leaks, service interruptions, or even direct damage to physical equipment.
[0062] Therefore, a program encryption system is provided, see [link to relevant documentation]. Figure 1 The encryption system comprises an operations and maintenance center, a key management center, and deployment nodes. The operations and maintenance center is responsible for encrypting the application and sending the encrypted or unencrypted application to the deployment nodes. The key management center generates and manages key pairs, which include a private key and a public key. The key management center interacts with the operations and maintenance center, sending the public key to the operations and maintenance center so that the operations and maintenance center can encrypt the application based on the public key. The deployment nodes are responsible for executing the encrypted or unencrypted application packages received from the operations and maintenance center. The deployment nodes also interact with the key management center, obtaining the key pair, including the private and public keys, from it.
[0063] This application provides a program encryption method applied to a first device. Here, the first device can be... Figure 1 The operation and maintenance center shown.
[0064] S201. Obtain the public key and application source code from the key pair, and compile the source code using WebAssembly to obtain the raw bytecode.
[0065] The key pair is the same as the key pair in the second device, which is a device for deploying the application, for example... Figure 1 The deployment nodes are shown.
[0066] Then, the first device can encrypt the application's source code through the following steps S202-S204.
[0067] S202. Generate a symmetric key based on the public key, encrypt the original bytecode using the symmetric key to obtain encrypted bytecode, and encrypt the symmetric key using the public key to obtain an encrypted symmetric key.
[0068] S203. Rewrite the code section in the original bytecode to generate rewritten bytecode.
[0069] In this rewritten bytecode, the function section is identical to the function section of the original bytecode, and the function logic in the code section of the rewritten bytecode satisfies the function declarations in the function section. Thus, a programmer unfamiliar with the program encryption method provided in this application, upon receiving the rewritten bytecode, would assume that the rewritten bytecode is the bytecode to be executed. Therefore, the rewritten bytecode can be used to achieve obfuscation.
[0070] Programmers who are unaware of the program encryption method provided in the embodiments of this application are generally attackers.
[0071] S204. Write the encrypted bytecode, the decryption logic of the encrypted bytecode, and the encryption symmetric key into the custom section of the rewritten bytecode to obtain the encrypted package.
[0072] In this way, even if the encrypted package is leaked, the core original bytecode is encrypted and hidden in a custom section of the rewritten bytecode, increasing the difficulty for attackers to find the core original bytecode. Furthermore, the key used to decrypt the encrypted bytecode is protected by asymmetric encryption, meaning that even if an attacker finds the encrypted bytecode, they would first need to crack the decryption key, further increasing the difficulty for attackers to obtain the original bytecode and effectively ensuring program security.
[0073] like Figure 3 As shown in the figure, this application provides a program encryption method, which is applied to a first device and includes the following steps:
[0074] S310. Obtain the public key and application source code from the key pair, and compile the source code using network assembly to obtain the raw bytecode.
[0075] In some embodiments, the application's source code can be compiled into one or more regular Wasm files using a suitable compiler. The raw bytecode included in the Wasm file can be denoted as OW.
[0076] S320. Generate a symmetric key based on the public key, encrypt the original bytecode using the symmetric key to obtain encrypted bytecode, and encrypt the symmetric key using the public key to obtain an encrypted symmetric key.
[0077] In some embodiments, after obtaining the public key mpki, a symmetric encryption key ki can be temporarily generated based on the public key mpki. Then, OW is encrypted using the symmetric key ki to obtain the encrypted bytecode CW. ki is then encrypted using mpki to obtain the encrypted symmetric key cki.
[0078] S330: Rewrite the code sections in the original bytecode to generate rewritten bytecode.
[0079] In some examples, for each Open Function (OW), the function section remains unchanged, while the code section is rewritten without restriction. The function parameter and return value declarations in the final rewritten bytecode are identical to the original bytecode. This way, by modifying only the function code logic without changing the function declarations, the resulting rewritten bytecode can serve an obfuscating purpose. Therefore, in the event of a program leak, anyone who obtains the leaked program will mistake the rewritten bytecode, including the function section and the rewritten code section, for the original bytecode included in the application. Consequently, they will be unable to discern the logic of the source code, and thus cannot tamper with it or perform decompilation or other actions detrimental to source code security. For ease of description later, the rewritten bytecode can be referred to as ORW.
[0080] S340, Generate a custom section name based on the public key.
[0081] In some examples, the process of generating a custom section name based on a public key includes the following steps: First, a hash value is calculated for the public key, and then the preset bytes of the hash value are converted into a string to obtain the name of the custom section.
[0082] The preset byte can be a subset of bytes in the hash value. For example, it can be the first n characters in the hash value, the last m characters in the hash value, or the characters from the 0th character to the pth character in the hash value.
[0083] For example, let's take the case where the default byte is the last 40 characters (5 bytes) of the hash value. After calculating the hash value for the public key mpki, the last 40 characters (5 bytes) of the resulting hash value are converted into a 10-digit hexadecimal string. This string can be used as the name of a custom section, which can be denoted as cname.
[0084] The string format can be binary, decimal, hexadecimal, etc. When the string format is the same as the hash value format, the preset bytes in the hash value can be directly extracted as the name of the custom section.
[0085] S350. Write the encrypted bytecode, the decryption logic of the encrypted bytecode, and the encrypted symmetric key in the custom section of the rewritten bytecode.
[0086] The decryption logic here describes the process of decrypting the encrypted bytecode and the encryption key pair. Thus, after the second device obtains the decryption logic, it can decrypt the encrypted bytecode and the encryption key pair based on the decryption logic, ultimately obtaining the original bytecode.
[0087] In some embodiments, the decryption logic includes: reading the encrypted bytecode CW, decrypting the encrypted symmetric key using the private key mski from the key pair to obtain the symmetric key, and finally decrypting the encrypted bytecode using the symmetric key to obtain the original bytecode.
[0088] The aforementioned private key, mski, needs to be obtained by the second device itself. In some examples, the second device is... Figure 1 In the case of a deployment node in the program encryption system shown, the second device can be obtained from the key management center.
[0089] In some embodiments, the first device may also write additional code logic in a custom section of the rewritten bytecode.
[0090] The additional code logic includes at least one of the following:
[0091] If the system time of the second device is within a preset time window, the decryption logic is executed.
[0092] If the address of the second device is a preset address, the decryption logic is executed.
[0093] In the case where the additional code logic includes executing decryption logic when the system time of the second device is within a preset time window, for example, the first device can read the current system time of the local machine, match the current system time of the local machine with the preset time window, and if the current system time of the local machine is within the preset time window, the verification is successful and the decryption logic is executed; otherwise, the decryption logic is not executed.
[0094] The additional code logic includes executing decryption logic when the address of the second device is a preset address. For example, the first device can read the local physical (Media Access Control, MAC) address, match the read local MAC address with the preset address, and if the read local MAC address matches the preset address, the verification is successful and the decryption logic is executed; otherwise, the decryption logic is not executed.
[0095] The additional code logic can be configured by the user.
[0096] In some embodiments, the first device can compile the decryption logic and the append code logic into a dynamic link library supported by the second device. Then, the dynamic link library is written into a custom section of the rewritten bytecode, thus enabling the decryption logic and the append code logic to be written into a custom section of the rewritten bytecode.
[0097] In some examples, the decryption logic and additional code logic can be compiled into a dynamic link library (DLL) supported by the second device using the appropriate toolchain.
[0098] In some examples, the encrypted bytecode (CW) and the decryption logic dynamic link library (DLL) are written into a custom section named cname within the rewritten bytecode (ORW), also known as an extended Custom section. The specific format of the extended Custom section includes the domain name, type, and description, as detailed in Table 1 below.
[0099] Table 1
[0100]
[0101] The payload structure is shown in Table 2 below:
[0102] Table 2
[0103]
[0104] Where varuintN represents LEB128 unsigned integer encoding, N represents the number of bits, and the value ranges from [0, 2]. N -1 ]).
[0105] S360: Obtain the encrypted package based on the rewritten bytecode.
[0106] In some embodiments, the raw bytecode obtained by S310 may include one wasm file or multiple wasm files.
[0107] In the case of a wasm file, the wasm file can be encrypted using the steps S320-S350 described above to obtain an encrypted package containing rewritten bytecode.
[0108] When multiple wasm files are involved, all files in the multiple wasm files can be encrypted using the steps S320-S350 described above to obtain an encrypted package containing multiple rewritten bytecodes. Alternatively, only some files in the multiple wasm files can be encrypted using the steps S320-S350 described above to ultimately obtain an encrypted package containing some rewritten bytecodes.
[0109] At this point, the encrypted program package can be obtained.
[0110] S370, Distribute the obtained encryption package to the second device.
[0111] This application also provides a program execution method applied to a second device.
[0112] In some embodiments, the second device may be a deployment node in a program encryption system, such as Figure 1 As shown. In some examples, the deployment node can be a physical machine, a virtual machine, or a container.
[0113] like Figure 4 As shown, the program execution method includes the following steps:
[0114] S410, Obtain the encrypted package.
[0115] The encryption package includes rewritten bytecode, and the custom section of the rewritten bytecode includes encrypted bytecode, decryption logic for the encrypted bytecode, and an encrypted symmetric key.
[0116] The encryption package may contain one or more Wasm files. Each encrypted Wasm file is processed using the following steps to execute the program.
[0117] S420: Obtain the private key from the key pair of the second device.
[0118] S430. Following the decryption logic, use the private key and the encryption symmetric key to decrypt the encrypted bytecode and obtain the original bytecode.
[0119] S440, Execute raw bytecode.
[0120] The encryption package is implemented as described in the above embodiments. Figure 2 The encryption method shown is used to obtain the encryption.
[0121] The execution engine that executes the raw bytecode can be any type of execution engine, such as an interpreter-based execution engine, or an execution engine based on JIT or AOT modes.
[0122] In some embodiments, the encryption package includes at least one bytecode file, and one bytecode file includes rewritten bytecode or unencrypted raw bytecode. In this case, this application also provides a program execution method, as shown below. Before decrypting the encrypted bytecode using a private key and an encryption symmetric key according to decryption logic to obtain the raw bytecode, the first device may further perform the following steps:
[0123] Step a: Obtain the encrypted package.
[0124] Step b: Obtain the private key from the key pair of the second device.
[0125] Step c: Obtain the public key from the key pair, and generate a second name for the custom section based on the public key.
[0126] Step d: For each bytecode file, check whether the bytecode file contains a custom section with a second name.
[0127] If the bytecode file includes a custom section with a second name, perform the following step e:
[0128] Step e: Following the decryption logic, use the private key and the encryption symmetric key to decrypt the encrypted bytecode and obtain the original bytecode.
[0129] In some examples, the Custom section named cname is read from the bytecode file, the encrypted bytecode CW and the decryption logic dynamic link library binary representation DLL are read, the DLL is run to decrypt CW, and the original bytecode OW is obtained.
[0130] If the bytecode file does not include a custom section with a second name, perform the following step f:
[0131] Step f: Execute the raw bytecode.
[0132] Figure 5 This is a schematic diagram of a program encryption device provided in an embodiment of this application. Figure 4 As shown, the program encryption device includes a key module 501, a program generation module 502, and a distribution module 503.
[0133] Key module 501 is used to obtain the public key and application source code from the key pair.
[0134] The program generation module 502 is used to compile the source code using WebAssembly to obtain raw bytecode; the key pair is the same as the key pair in the second device, which is the device for deploying the application; a symmetric key is generated based on the public key, and the raw bytecode is encrypted using the symmetric key to obtain encrypted bytecode; and the symmetric key is encrypted using the public key to obtain an encrypted symmetric key; code sections in the raw bytecode are rewritten to generate rewritten bytecode; the function sections of the rewritten bytecode are the same as the function sections of the raw bytecode, and the function code logic in the code sections of the rewritten bytecode satisfies the function declarations in the function sections; and it is used to write the encrypted bytecode, the decryption logic of the encrypted bytecode, and the encrypted symmetric key into the custom section of the rewritten bytecode to obtain an encrypted program package.
[0135] Distribution module 503 is used to distribute the encrypted package to the second device.
[0136] In some embodiments, the program encryption device further includes:
[0137] The name generation module is used to generate names for custom sections based on public keys.
[0138] In some embodiments, the name generation module includes:
[0139] A hash calculation unit is used to calculate a hash value for a public key;
[0140] The byte conversion unit is used to convert the preset bytes of a hash value into a string to obtain the name of a custom section.
[0141] In some embodiments, the decryption logic includes:
[0142] Read encrypted bytecode;
[0143] Use the private key from the key pair to decrypt the encrypted symmetric key to obtain the symmetric key;
[0144] The encrypted bytecode is decrypted using a symmetric key to obtain the original bytecode.
[0145] In some embodiments, the program generation module 502 in the program encryption device is further configured to write additional code logic in a custom section of the rewritten bytecode.
[0146] The additional code logic includes at least one of the following:
[0147] If the system time of the second device is within a preset time window, the decryption logic is executed.
[0148] If the address of the second device is a preset address, the decryption logic is executed.
[0149] In some embodiments, the program generation module 502 is further configured to compile the decryption logic and the additional code logic into a dynamic link library supported by the second device;
[0150] In this case, the program generation module 502 in the program encryption device is also used to write dynamic link libraries in a custom section of the rewritten bytecode.
[0151] The program encryption device provided in this application embodiment can execute the method shown in the above method embodiment. Its implementation principle and beneficial effects can be referred to the relevant description in the method embodiment, and will not be repeated here.
[0152] This application also provides a program execution device, such as... Figure 6 As shown, the program execution device, applied to the second device, includes an execution module 601 and a key module 602.
[0153] The execution module is used to obtain the encrypted package, which includes rewritten bytecode. The custom section of the rewritten bytecode includes encrypted bytecode, decryption logic of the encrypted bytecode, and encrypted symmetric key.
[0154] The key module is used to obtain the private key from the key pair of the second device;
[0155] The execution module is also used to decrypt the encrypted bytecode using the private key and the encryption symmetric key according to the decryption logic, to obtain the original bytecode; and to execute the original bytecode.
[0156] The encrypted package is obtained through the encryption method described above.
[0157] In some embodiments, the encryption package includes at least one bytecode file, and one bytecode file includes rewritten bytecode or unencrypted raw bytecode;
[0158] The key module is used to retrieve the public key from the key pair.
[0159] The execution module is used to generate a second name for a custom section based on the public key; for each bytecode file, it checks whether the bytecode file contains a custom section with the second name; if the bytecode file contains a custom section with the second name, it decrypts the encrypted bytecode using the private key and the encryption symmetric key according to the decryption logic in the custom section to obtain the original bytecode.
[0160] In some embodiments, the execution module is further configured to execute the raw bytecode in the bytecode file that has not been encrypted, in the absence of a custom section with a second name in the bytecode file.
[0161] Figure 7 This is a schematic diagram of the structure of a first device provided in an embodiment of this application. Figure 7 As shown, the first device includes: a memory 701, a transceiver 702, and at least one processor 703.
[0162] The transceiver 702 is used to interact with other devices to send and receive data. For example, in this embodiment, the transceiver 702 can be used to obtain the key and the source code of the application, and can also be used to send encrypted packages.
[0163] The memory 701 is used to store computer program code, which includes computer instructions. These computer instructions run in the first apparatus described above to implement the method shown in the above method embodiments. For example, the memory may include high-speed random access memory (RAM), and may also include non-volatile memory (NVM), such as at least one disk storage device, or a USB flash drive, portable hard drive, read-only memory, magnetic disk, or optical disk, etc.
[0164] Processor 703 can be a general-purpose processor, including a Central Processing Unit (CPU), a network processor (NP), etc.; it can also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. Processor 703 can also be other general-purpose processors. The general-purpose processor can be a microprocessor or any conventional processor.
[0165] The memory 701, transceiver 702, and processor 703 are communicatively connected. For example, the memory 701 and transceiver 702 can be connected to the processor 703 via a system bus to complete mutual communication. The system bus can be a peripheral component interconnect (PCI) bus, an extended industry standard architecture (EISA) bus, an industry standard architecture (ISA) bus, etc. The system bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the figure, but this does not mean that there is only one bus or one type of bus.
[0166] Optionally, the memory 701 can be either independent or integrated with the processor 703. When the memory 701 is configured independently, it is connected to the processor 703 via a system bus.
[0167] This application also provides a chip for executing instructions, which is used to execute the technical solution of the program encryption method in the above embodiments.
[0168] This application also provides a computer-readable storage medium storing computer instructions. When these computer instructions are executed by a processor, they are used to implement the technical solution of the program encryption method in the above embodiments. Specifically, when the computer instructions are executed by a processor, the first device can execute the technical solution of the program encryption method in the above embodiments.
[0169] This application also provides a computer program product, which includes a computer program stored in a computer-readable storage medium. At least one processor can read the computer program from the computer-readable storage medium, and when the at least one processor executes the computer program, it can implement the technical solution of the program encryption method in the above embodiments.
[0170] The aforementioned computer-readable storage media can be implemented from any type of volatile or non-volatile storage device or a combination thereof, such as Static Random-Access Memory (SRAM), Electrically Erasable Programmable Read-Only Memory (EEPROM), Erasable Programmable Read-Only Memory (EPROM), Programmable Read-Only Memory (PROM), Read-Only Memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk. The computer-readable storage medium can be any available medium accessible to a general-purpose or special-purpose computer.
[0171] An exemplary computer-readable storage medium is coupled to a processor, enabling the processor to read information from and write information to the storage medium. Of course, the computer-readable storage medium can also be a component of the processor. The processor and the computer-readable storage medium can reside in an application-specific integrated circuit (ASIC). Alternatively, the processor and the computer-readable storage medium can exist as discrete components in an electronic control unit or main control device; this application does not limit this.
[0172] In the several embodiments provided in this application, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative; for instance, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple modules may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be indirect coupling or communication connection through some interfaces, devices, or modules, and may be electrical, mechanical, or other forms.
[0173] The modules described as separate components may or may not be physically separate. The components shown as modules may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to implement the solution of this embodiment according to actual needs.
[0174] Furthermore, the functional modules in the various embodiments of this application can be integrated into one processing unit, or each module can exist physically separately, or two or more modules can be integrated into one unit. The unit composed of the above modules can be implemented in hardware or in the form of hardware plus software functional units.
[0175] The integrated modules described above, implemented as software functional modules, can be stored in a computer-readable storage medium. These software functional modules, stored in a storage medium, include several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) or processor to execute some steps of the methods of the various embodiments of this application.
[0176] It should be understood that the steps of the method disclosed in the embodiments of this application can be directly implemented by a hardware processor, or implemented by a combination of hardware and software modules in the processor.
[0177] Those skilled in the art will understand that all or part of the steps of the above-described method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When executed, the program performs the steps of the above-described method embodiments; and the aforementioned storage medium includes various media capable of storing program code, such as ROM, RAM, magnetic disks, or optical disks.
[0178] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application.
Claims
1. A program encryption method, characterized in that, Applied to a first device, the method includes: Obtain the public key from the key pair and the source code of the application, and compile the source code using WebAssembly to obtain the raw bytecode; the key pair is the same as the key pair in the second device, which is the device for deploying the application; A symmetric key is generated based on the public key, and the original bytecode is encrypted using the symmetric key to obtain encrypted bytecode. The symmetric key is then encrypted using the public key to obtain an encrypted symmetric key. The code sections in the original bytecode are rewritten to generate rewritten bytecode; the function sections of the rewritten bytecode are the same as the function sections of the original bytecode, and the function code logic in the code sections of the rewritten bytecode satisfies the function declarations in the function sections. The encrypted bytecode, the decryption logic of the encrypted bytecode, and the encryption symmetric key are written into a custom section of the rewritten bytecode to obtain an encrypted package.
2. The method according to claim 1, characterized in that, Before writing the encrypted bytecode in a custom section of the rewritten bytecode, the method further includes: The name of the custom section is generated based on the public key.
3. The method according to claim 2, characterized in that, The process of generating the name of the custom section based on the public key includes: Calculate the hash value of the public key; The preset bytes of the hash value are converted into a string to obtain the name of the custom section.
4. The method according to any one of claims 1-3, characterized in that, The decryption logic includes: Read the encrypted bytecode; The encrypted symmetric key is decrypted using the private key in the key pair to obtain the symmetric key; The encrypted bytecode is decrypted using the symmetric key to obtain the original bytecode.
5. The method according to claim 4, characterized in that, Before distributing the encryption package to the second device, the method further includes: Write additional code logic in the custom section of the rewritten bytecode; The additional code logic includes at least one of the following: The decryption logic is executed when the system time of the second device is within a preset time window; If the address of the second device is a preset address, the decryption logic is executed.
6. The method according to claim 5, characterized in that, The method further includes: The decryption logic and the additional code logic are compiled into a dynamic link library supported by the second device; The decryption logic and the additional code logic are written in a custom section of the rewritten bytecode, including: The dynamic link library is written in a custom section of the rewritten bytecode.
7. A program execution method, characterized in that, Applied to a second device, the method includes: Obtain an encrypted package, the encrypted package including rewritten bytecode, the custom section of the rewritten bytecode including encrypted bytecode, the decryption logic of the encrypted bytecode and an encrypted symmetric key; Obtain the private key from the key pair of the second device; According to the decryption logic, the encrypted bytecode is decrypted using the private key and the encryption symmetric key to obtain the original bytecode. Execute the raw bytecode; The encrypted program package is obtained by the program encryption method according to any one of claims 1-6.
8. The method according to claim 7, characterized in that, The encryption package includes at least one bytecode file, and one bytecode file includes rewritten bytecode or unencrypted raw bytecode; Before decrypting the encrypted bytecode using the private key and the encryption symmetric key according to the decryption logic to obtain the original bytecode, the method further includes: Obtain the public key from the key pair, and generate a second name for the custom section based on the public key; For each bytecode file, check whether the bytecode file contains a custom section with a second name; The process involves decrypting the encrypted bytecode using the private key and the encryption symmetric key according to the decryption logic to obtain the original bytecode, including: If the bytecode file includes a custom section with the second name, the encrypted bytecode is decrypted using the private key and the encryption symmetric key according to the decryption logic in the custom section to obtain the original bytecode.
9. The method according to claim 8, characterized in that, The method further includes: If the bytecode file does not include a custom section with the second name, execute the raw bytecode in the bytecode file that has not been encrypted.
10. A program encryption device, characterized in that, Applied to the first device, the program encryption device includes: The key module is used to obtain the public key of the key pair and the application's source code; A program generation module is used to compile the source code using WebAssembly to obtain raw bytecode; the key pair is the same as the key pair in the second device, which is a device for deploying the application; a symmetric key is generated based on the public key, the raw bytecode is encrypted using the symmetric key to obtain encrypted bytecode, and the symmetric key is encrypted using the public key to obtain an encrypted symmetric key; a module is used to rewrite code sections in the raw bytecode to generate rewritten bytecode; the function sections of the rewritten bytecode are the same as the function sections of the raw bytecode, and the function code logic in the code sections of the rewritten bytecode satisfies the function declarations in the function sections; a module is used to write the encrypted bytecode, the decryption logic of the encrypted bytecode, and the encrypted symmetric key into a custom section of the rewritten bytecode to obtain an encrypted package.
11. A program execution device, characterized in that, Applied to a second device, the device comprising: An execution module is used to obtain an encrypted package, the encrypted package including rewritten bytecode, and the custom section of the rewritten bytecode includes encrypted bytecode, decryption logic of the encrypted bytecode, and an encrypted symmetric key; The key module is used to obtain the private key from the key pair of the second device; The execution module is used to decrypt the encrypted bytecode using the private key and the encryption symmetric key according to the decryption logic to obtain the original bytecode; An execution module is used to execute the original bytecode; The encrypted program package is obtained by the program encryption method according to any one of claims 1-6.
12. A first device, characterized in that, include: A memory and at least one processor; the memory is communicatively connected to the processor; the memory is used to store computer program code, the computer program code including computer instructions; when the processor executes the computer instructions, it causes the first device to perform the method as described in any one of claims 1-6.
13. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that, when executed by a processor, are used to implement the method as described in any one of claims 1-6.
14. A computer program product, characterized in that, When the computer program product is run on a computer / executed by the computer's processor, it implements the method as described in any one of claims 1-6.
