A method and device for encrypting shader files
By converting the shader file from binary stream to utf-8 code and encrypting it, the problem of poor security of shader files in the prior art is solved, and the encryption protection and normal use of the file is realized.
Patent Information
- Application Number
- CN202010724619.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-07-24
- Publication Date
- 2024-12-10
- Estimated Expiration
- 2040-07-24
AI Technical Summary
Existing shader files are easily tampered with due to their editability, poor security, lack encryption protection.
By converting the shader file to be encrypted from a binary stream to a text stream, encoded as utf-8 code, and encrypted using a predetermined encryption algorithm, the encrypted shader file is generated, and a new file name is stored and set, and subsequently decrypted by decryption The algorithm decrypts to ensure the normal use of the file.
Effectively prevent shader files from being used directly, reduce their editability, improve file security, and ensure that shader files can be decrypted and used normally when needed.
Smart Images

Figure CN111931205B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a method and device for encrypting a shader file. Background Art
[0002] Shader is an editable program used to implement image rendering and replace the fixed rendering pipeline. In the existing virtual scene, the shader replaces the traditional fixed rendering pipeline and can realize the relevant calculations in 3D graphics. And because of its editability, it can realize a variety of image effects without being restricted by the fixed rendering pipeline of the graphics card, which greatly improves the image quality.
[0003] In the process of implementing the present invention, the inventors found that there are at least the following problems in the prior art:
[0004] The existing shader files can be used directly. However, due to their editability, they are easy to be tampered with and have poor security. However, no related method for encrypting the shader code has been proposed. Summary of the invention
[0005] In view of this, an embodiment of the present invention provides a method and apparatus for encrypting a shader file, which can at least solve the problem of unencrypted shader code in the prior art.
[0006] To achieve the above object, according to one aspect of an embodiment of the present invention, a method for encrypting a shader file is provided, comprising:
[0007] Run the encryption script to convert the first shader file to be encrypted from a binary stream to a text stream;
[0008] Traversing the text stream, encoding the characters in the text stream into UTF-8 codes;
[0009] The UTF-8 code is encrypted using a predetermined encryption algorithm, and a second shader file is generated based on the encrypted character string.
[0010] Optionally, before converting the first shader file to be encrypted from a binary stream to a text stream, the method further includes:
[0011] Reading a first file name and a storage path of the first shader file, so as to read a first shader file corresponding to the first file name from a project file corresponding to the storage path;
[0012] After generating the second shader file based on the encrypted string, the method further includes:
[0013] The second shader file is stored in a project file corresponding to the storage path, and a second file name of the second shader file is set to a combination of the first file name and predetermined characters.
[0014] Optionally, encoding the characters in the text stream into UTF-8 codes includes:
[0015] Determine the hexadecimal ASCII code corresponding to the character in the text stream, and use the hexadecimal ASCII code as a unicode number;
[0016] Convert the unicode number to a binary number, and fill the binary number values into the spaces in the predetermined binary format from right to left to get the UTF-8 code.
[0017] Optionally, before determining the ASCII code corresponding to the character in the text stream, the method further includes: uniformly replacing line breaks in the text stream with line breaks in a predetermined format.
[0018] Optionally, the encrypting the UTF-8 code by using a predetermined encryption algorithm includes:
[0019] According to the order of the UTF-8 codes, extract three UTF-8 codes, shift the binary number in the first UTF-8 code right by two bits, and get the first encrypted bit;
[0020] Perform a bitwise AND operation on the binary number in the first UTF-8 code and the first predetermined binary number and shift them left by four bits, shift the binary number in the second UTF-8 code right by four bits, and perform an OR operation on the two to obtain the second encrypted bit;
[0021] Perform a bitwise AND operation on the binary number in the second UTF-8 code and the second predetermined binary number and shift them left by two bits, shift the binary number in the third UTF-8 code right by six bits, perform an OR operation on the two, and obtain the third encrypted bit;
[0022] Perform a bitwise AND operation on the binary number in the third UTF-8 code and the third predetermined binary number to obtain a fourth encrypted bit;
[0023] The first encryption bit, the second encryption bit, the third encryption bit and the fourth encryption bit are converted into ASCII codes respectively, and the characters corresponding to the ASCII codes are searched and combined to obtain a four-bit encrypted string.
[0024] Optionally, the method further comprises: when the second UTF-8 code and the third UTF-8 code do not exist, setting the third encryption bit and the fourth encryption bit to a fourth predetermined binary number; or
[0025] When the third UTF-8 code does not exist, the fourth encryption bit is set to a fourth predetermined binary number.
[0026] Optionally, after generating the second shader file based on the encrypted string, the method further includes:
[0027] In response to a load operation on the second shader file, reading an encrypted string in the second shader file;
[0028] Decrypt the encrypted string according to a predetermined decryption algorithm to obtain a UTF-8 code;
[0029] The UTF-8 code is decoded to obtain characters and combined into a text stream, and the text stream is converted into a binary stream to obtain the first shader file.
[0030] Optionally, decrypting the encrypted string according to a predetermined decryption algorithm to obtain a UTF-8 code includes:
[0031] According to the arrangement order of the encrypted characters, four encrypted characters are extracted, the ASCII codes corresponding to the encrypted characters are searched and converted into binary numbers, and the first encrypted bit, the second encrypted bit, the third encrypted bit and the fourth encrypted bit are obtained;
[0032] Shift the binary number in the first encrypted bit left by two bits, shift the binary number in the second encrypted bit right by four bits, and perform an OR operation on the two to get the first UTF-8 code;
[0033] Perform a bitwise AND operation on the binary number in the second encrypted bit and the second predetermined binary number and shift them left by four bits, shift the binary number in the third encrypted bit right by two bits, perform an OR operation on the two, and obtain a second UTF-8 code;
[0034] The binary number in the third encryption position is bitwise ANDed with the first predetermined binary number and shifted left by six positions, and then ORed with the binary number in the fourth encryption position to obtain a third UTF-8 code.
[0035] Optionally, the method further includes:
[0036] Determine whether the third encryption bit is a fourth predetermined binary number, and if the determination result is yes, set the second UTF-8 code and the third UTF-8 code to empty;
[0037] If the judgment result is no, then determine whether the fourth encryption bit is the fourth predetermined binary number. If the judgment result is yes, set the third UTF-8 code to empty.
[0038] Optionally, decoding the UTF-8 code to obtain characters and assembling them into a text stream includes: dividing a UTF-8 code into two groups of binary numbers with four binary numbers as a group from left to right, converting each group of binary numbers into hexadecimal numbers respectively, to obtain two hexadecimal numbers;
[0039] Look up the character corresponding to the combined two hexadecimal numbers from the ASCII code table;
[0040] Arrange and combine the queried characters according to the order of the UTF-8 code to obtain a text stream.
[0041] To achieve the above object, according to another aspect of an embodiment of the present invention, a device for encrypting a shader file is provided, comprising:
[0042] A conversion module, used for running an encryption script to convert the first shader file to be encrypted from a binary stream to a text stream;
[0043] An encoding module, used for traversing the text stream and encoding characters in the text stream into UTF-8 codes;
[0044] The encryption module is used to encrypt the UTF-8 code using a predetermined encryption algorithm and generate a second shader file based on the encrypted character string.
[0045] Optionally, it further includes a reading module, which is used to: read the first file name and storage path of the first shader file, so as to read the first shader file corresponding to the first file name from the project file corresponding to the storage path;
[0046] and a storage module, for storing the second shader file in a project file corresponding to the storage path, and setting a second file name of the second shader file to a combination of the first file name and predetermined characters.
[0047] Optionally, the encoding module is used to: determine the hexadecimal ASCII code corresponding to the character in the text stream, and use the hexadecimal ASCII code as the unicode number;
[0048] Convert the unicode number to a binary number, and fill the binary number values into the spaces in the predetermined binary format from right to left to get the UTF-8 code.
[0049] Optionally, the encoding module is further used to: uniformly replace line breaks in the text stream with line breaks in a predetermined format.
[0050] Optionally, the encryption module is used to: extract three UTF-8 codes according to the order of the UTF-8 codes, shift the binary number in the first UTF-8 code right by two bits to obtain a first encryption bit;
[0051] Perform a bitwise AND operation on the binary number in the first UTF-8 code and the first predetermined binary number and shift them left by four bits, shift the binary number in the second UTF-8 code right by four bits, and perform an OR operation on the two to obtain the second encrypted bit;
[0052] Perform a bitwise AND operation on the binary number in the second UTF-8 code and the second predetermined binary number and shift them left by two bits, shift the binary number in the third UTF-8 code right by six bits, perform an OR operation on the two, and obtain the third encrypted bit;
[0053] Perform a bitwise AND operation on the binary number in the third UTF-8 code and the third predetermined binary number to obtain a fourth encrypted bit;
[0054] The first encryption bit, the second encryption bit, the third encryption bit and the fourth encryption bit are converted into ASCII codes respectively, and the characters corresponding to the ASCII codes are searched and combined to obtain a four-bit encrypted string.
[0055] Optionally, the encryption module is also used to: when the second UTF-8 code and the third UTF-8 code do not exist, set the third encryption bit and the fourth encryption bit to a fourth predetermined binary number; or when the third UTF-8 code does not exist, set the fourth encryption bit to a fourth predetermined binary number.
[0056] Optionally, a decryption module is further included, which is used to: read the encrypted string in the second shader file in response to a loading operation on the second shader file;
[0057] Decrypt the encrypted string according to a predetermined decryption algorithm to obtain a UTF-8 code;
[0058] The UTF-8 code is decoded to obtain characters and combined into a text stream, and the text stream is converted into a binary stream to obtain the first shader file.
[0059] Optionally, the decryption module is used to: extract four encrypted characters according to the order of arrangement of the encrypted characters, query the ASCII code corresponding to each encrypted character and convert it into a binary number to obtain a first encrypted bit, a second encrypted bit, a third encrypted bit and a fourth encrypted bit;
[0060] Shift the binary number in the first encrypted bit left by two bits, shift the binary number in the second encrypted bit right by four bits, and perform an OR operation on the two to get the first UTF-8 code;
[0061] Perform a bitwise AND operation on the binary number in the second encrypted bit and the second predetermined binary number and shift them left by four bits, shift the binary number in the third encrypted bit right by two bits, perform an OR operation on the two, and obtain a second UTF-8 code;
[0062] The binary number in the third encryption position is bitwise ANDed with the first predetermined binary number and shifted left by six positions, and then ORed with the binary number in the fourth encryption position to obtain a third UTF-8 code.
[0063] Optionally, the decryption module is further used to: determine whether the third encrypted bit is a fourth predetermined binary number, and if the determination result is yes, set the second UTF-8 code and the third UTF-8 code to empty;
[0064] If the judgment result is no, then determine whether the fourth encryption bit is the fourth predetermined binary number. If the judgment result is yes, set the third UTF-8 code to empty.
[0065] Optionally, the decryption module is used to: divide a UTF-8 code into two groups of binary numbers with four binary numbers as a group from left to right, and convert each group of binary numbers into hexadecimal to obtain two hexadecimal numbers;
[0066] Look up the character corresponding to the combined two hexadecimal numbers from the ASCII code table;
[0067] Arrange and combine the queried characters according to the order of the UTF-8 code to obtain a text stream.
[0068] To achieve the above objective, according to yet another aspect of an embodiment of the present invention, an electronic device for encrypting a shader file is provided.
[0069] The electronic device of an embodiment of the present invention includes: one or more processors; a storage device for storing one or more programs, when the one or more programs are executed by the one or more processors, the one or more processors implement any of the above-mentioned methods for encrypting shader files.
[0070] To achieve the above objective, according to another aspect of an embodiment of the present invention, a computer-readable medium is provided, on which a computer program is stored, and when the program is executed by a processor, any of the above methods for encrypting a shader file is implemented.
[0071] According to the solution provided by the present invention, one embodiment of the above invention has the following advantages or beneficial effects: to prevent the shader from being used directly, a self-developed encryption algorithm is used to perform bit operations on the shader file, so that the plaintext shader code cannot be directly run, reducing its editability and ensuring the security of the file; before subsequent use, it needs to be decrypted using the reverse of the encryption algorithm, that is, the decryption algorithm, to ensure the normal use of the shader file.
[0072] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with specific implementation examples. BRIEF DESCRIPTION OF THE DRAWINGS
[0073] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0074] Figure 1 is a schematic diagram of the main flow of a method for encrypting a shader file according to an embodiment of the present invention;
[0075] Figure 2 is a flowchart of an optional method for encrypting a shader file according to an embodiment of the present invention;
[0076] Figure 3 is a flowchart of another optional method for encrypting a shader file according to an embodiment of the present invention;
[0077] Figure 4 is a flowchart of another optional method for encrypting a shader file according to an embodiment of the present invention;
[0078] Figure 5 It is a flowchart of a method for decoding UTF-8 code according to an embodiment of the present invention;
[0079] Figure 6 is a schematic diagram of main modules of a device for encrypting a shader file according to an embodiment of the present invention;
[0080] Figure 7 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0081] Figure 8 It is a schematic diagram of the structure of a computer system of a mobile device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION
[0082] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0083] See also Figure 1 , which shows a main flow chart of a method for encrypting a shader file provided by an embodiment of the present invention, comprising the following steps:
[0084] S101: Run the encryption script to convert the first shader file to be encrypted from a binary stream to a text stream;
[0085] S102: traverse the text stream and encode the characters in the text stream into UTF-8 codes;
[0086] S103: Encrypt the UTF-8 code using a predetermined encryption algorithm, and generate a second shader file based on the encrypted string.
[0087] In the above implementation, for step S101, when there is only one shader file to be encrypted (i.e., the first shader file), the encryption script can be directly run to perform the encryption operation. Alternatively, regardless of the number, the information of all first shader files, such as the file name and storage path (preliminary manual configuration), can be written into the configuration file in advance. The shader file can be stored locally on the computer or on the Internet, but is preferably stored locally on the computer.
[0088] The configuration file is usually a javascript file with a ".js" suffix, which records the file name and storage path of each first shader file, as well as the encryption method and other configuration information in a fixed format. When reading the shader file later, it is necessary to read the corresponding shader file according to the storage path recorded in the configuration file. Furthermore, there may be multiple shader files stored under this path, so it is necessary to filter by file name to extract the correct shader file to be encrypted.
[0089] During the development process, we often encounter the need to convert binary data into text. Common application scenarios include printing, cross-application interaction, and unsupported transmission protocols. Since this solution operates on the utf-8 (Unicode Transformation Forma, 8 bits) encoding, after obtaining the first shader file, it is also necessary to convert it from a binary stream to a text stream. And the text has an encoding format. According to different encoding formats, the binary stream can be converted into characters of different formats.
[0090] For step S102, the subsequent encryption operation is performed on utf-8 code, so the characters in the text stream need to be converted into utf-8 code format, but there is currently no method to directly convert characters into utf-8 code. This solution uses Base64 to first convert the characters into unicode numbers, and then convert the unicode numbers into utf-8 codes. For details, see the subsequent Figure 2 Description shown.
[0091] Base64 is usually used for data exchange between programs, not for human recognition. Sometimes the converted data may be less recognizable than the original data, so it can also be called "encryption". The basic principle of its implementation is: uppercase and lowercase letters, 10 Arabic numerals (0-9) and "+" and " / " a total of 64 printable characters to convert and express any binary number.
[0092] The utf-8 code is an 8-bit binary number. Base64 is one of the most common encoding methods for transmitting 8-bit byte codes on the Internet. It can be used to transmit longer identification information in an http environment. In other applications, binary data also needs to be encoded in a form suitable for placing in a URL (Uniform Resource Locator) (including hidden form fields).
[0093] For step S103, use the self-developed encryption algorithm (i.e., the predetermined encryption algorithm) to encrypt the UTF-8 code to obtain the encrypted second shader file. For the specific encryption method, see the subsequent Figure 3 According to the storage path of the first shader file, the encrypted second shader file is saved in the same path, and its file name is set to the format of "first file name + _encode".
[0094] The project folder is the folder of the entire project. In addition to shader files, a project also has js files, html files, etc. Through the storage path of the first shader file stored in the configuration file, the first shader file located in the project folder is read, encrypted, and the encrypted second shader file is also stored in the project folder.
[0095] The method provided in the above embodiment uses a self-developed encryption algorithm to perform bit operations on the shader file to achieve file encryption, reduce its editability, and ensure the security of the file.
[0096] See also Figure 2 , shows a schematic flow chart of an optional method for encrypting a shader file according to an embodiment of the present invention, comprising the following steps:
[0097] S201: Run the encryption script to convert the first shader file to be encrypted from a binary stream to a text stream;
[0098] S202: traverse the text stream, determine the hexadecimal ASCII code corresponding to the character in the text stream, and use the hexadecimal ASCII code as a unicode number;
[0099] S203: Convert the unicode number into a binary number, and fill the values in the binary number into the spaces in a predetermined binary format from right to left to obtain a UTF-8 code;
[0100] S204: Encrypt the UTF-8 code using a predetermined encryption algorithm, and generate a second shader file based on the encrypted string.
[0101] In the above embodiment, for steps S201 and S204, see Figure 1 The description of steps S101 and S103 is not repeated here.
[0102] In the above implementation, for steps S202 and S203, there are many libraries that can support the implementation of base64 in Java. If it is Java 1.6 / 1.7, apache common-codec can be used. If it is Java 1.8, java.util.Base64 in the standard library can be used. If it is an Android system, android.util.base64 can be used directly.
[0103] The Base64 encoding format is as follows:
[0104]
[0105]
[0106] Currently, there is no direct way to convert characters into UTF-8 codes, so they must first be converted into Unicode codes. The specific implementation process is as follows:
[0107] 1. ASCII codes are divided into binary, decimal and hexadecimal, and unicode codes are hexadecimal. Therefore, according to the ASCII code table, it is directly determined to convert the characters in the text stream into hexadecimal ASCII codes; for example, the hexadecimal ASCII code of the character "H" is 48.
[0108] Furthermore, the decimal ASCII code may be considered first. For example, the decimal ASCII code of the character "H" is 72, which needs to be converted to hexadecimal 48 later.
[0109] 2. Convert the unicode number to binary and remove the high-order 0, such as the first 0 in 0100.
[0110] 3. From right to left, fill the values in the binary number into the x (i.e., space) in the predetermined binary format. If there are any xs that are not filled in, fill them with 0 to get the UTF-8 code.
[0111] Unicode assigns a unique digital number to all characters in the world, ranging from 0x000000 to 0x10FFFF (hexadecimal), with a total of more than 1.1 million. The first 128 bits of the unicode number are the same as the utf-8 code. This solution mainly considers unicode numbers in the range of 0 to 65535, and the corresponding binary formats are:
[0112] unicode number (hexadecimal) UTF-8 code format (binary) 0000 0000~0000 007F 0xxxxxxx 0000 0080~0000 07FF 110xxxxx 10xxxxxx 0000 0800~0000 FFFF 1110xxxx 10xxxxxx 10xxxxxx
[0113] In order to facilitate the subsequent unified management, this solution sets the UTF-8 code to 24 bits, and fills in 0s where the bits are less than 24, so one character corresponds to three UTF-8 codes. For example, 48 is in the first number range, and the corresponding binary format is 0000000000000000 0xxxxxxx. The binary number 1001000 converted from 48 is filled into the binary format in sequence, and a total of three UTF-8 codes are obtained, namely 00000000 00000000 01001000.
[0114] 4. Finally, each character is converted into 3 UTF-8 codes; for example, the character "H" corresponds to 0000 0000 0000000001001000, a total of 3 UTF-8 codes.
[0115] Furthermore, before converting characters into unicode numbers, the text stream may be preprocessed, for example, the \r and \n line break characters may be uniformly replaced with \n to ensure the correctness of the line break format and reduce the subsequent conversion error rate.
[0116] The method provided in the above embodiment uses the unreadable feature of Base64 encoding, that is, the encoded data cannot be directly seen by human eyes, to encrypt the characters in the text stream and convert them into 8-bit byte UTF-8 code.
[0117] See also Figure 3 , shows a schematic flow chart of another optional method for encrypting a shader file according to an embodiment of the present invention, comprising the following steps:
[0118] S301: Run the encryption script to convert the first shader file to be encrypted from a binary stream to a text stream;
[0119] S302: traverse the text stream and encode the characters in the text stream into UTF-8 codes;.
[0120] S303: extract three UTF-8 codes according to the order of the UTF-8 codes, shift the binary number in the first UTF-8 code right by two bits, and obtain the first encrypted bit;
[0121] S304: performing a bitwise AND operation on the binary number in the first UTF-8 code and the first predetermined binary number and shifting the binary number to the left by four bits, shifting the binary number in the second UTF-8 code to the right by four bits, and performing an OR operation on the two to obtain a second encrypted bit;
[0122] S305: performing a bitwise AND operation on the binary number in the second UTF-8 code and the second predetermined binary number and shifting the binary number to the left by two bits, shifting the binary number in the third UTF-8 code to the right by six bits, and performing an OR operation on the two to obtain a third encrypted bit;
[0123] S306: performing a bitwise AND operation on the binary number in the third UTF-8 code and the third predetermined binary number to obtain a fourth encrypted bit;
[0124] S307: Convert the first encryption bit, the second encryption bit, the third encryption bit and the fourth encryption bit into ASCII codes respectively, search for characters corresponding to the ASCII codes and combine them to obtain a four-bit encrypted string;
[0125] S308: Generate a second shader file based on the encrypted character string.
[0126] In the above implementation, for steps S301, S302 and S308, see Figure 1 The description of steps S101 to S103 is not repeated here.
[0127] In the above implementation, for steps S303 to S307, this solution develops a self-developed encryption algorithm, and the relevant code is as follows:
[0128]
[0129]
[0130] The specific implementation process is as follows:
[0131] 1. To ensure that all characters in the text stream are converted to UTF-8 code, first use Base64 to process again;
[0132] 2. Since this scheme sets one character to correspond to three UTF-8 codes, three UTF-8 codes are also used as a group for encryption (corresponding to chr1, chr2, chr3), and 4 encrypted bits are obtained (corresponding to enc1, enc2, enc3, enc4):
[0133] ①enc1 is obtained by shifting chr1 right by two bits;
[0134] ② enc2 is obtained by performing a bitwise AND operation on chr1 and 011 (i.e., the first predetermined binary number), shifting the result of the operation left by 4 bits, and performing an OR operation with chr2 shifted right by 4 bits;
[0135] ③enc3 performs a bitwise AND operation on chr2 and 1111 (i.e., the second predetermined binary number), shifts the operation result left by 2 bits, and then performs an OR operation with chr3 shifted right by 6 bits to obtain the result.
[0136] ④enc4 is the bitwise AND result of chr3 and 111111 (i.e., the third predetermined binary number).
[0137] 3. First, it can be determined whether chr2 or chr3 does not exist. If both chr2 and chr3 do not exist, enc3 and enc4 are set to the binary number of 64 (i.e., the fourth predetermined binary number). 64 corresponds to @ in the ASCII table. Since @ does not exist in the shader file, @ is used to represent a non-existent null character.
[0138] 4. Convert enc1, enc2, enc3, and enc4 into hexadecimal numbers, find the corresponding characters in the ASCII table, combine them into an encrypted string, and output it.
[0139] The method provided in the above embodiment uses three UTF-8 codes as a group and encrypts them by bit operation to obtain four encryption bits, thereby ensuring the security of the characters in the shader file.
[0140] See also Figure 4 , shows a schematic flow chart of another optional method for encrypting a shader file according to an embodiment of the present invention, comprising the following steps:
[0141] S401: Run the encryption script to convert the first shader file to be encrypted from a binary stream to a text stream;
[0142] S402: traverse the text stream and encode the characters in the text stream into UTF-8 codes;
[0143] S403: Encrypt the UTF-8 code using a predetermined encryption algorithm, and generate a second shader file based on the encrypted string;
[0144] S404: In response to the loading operation of the second shader file, read the encrypted string in the second shader file;
[0145] S405: decrypting the encrypted string according to a predetermined decryption algorithm to obtain a UTF-8 code; wherein the predetermined decryption algorithm is the reverse of the predetermined encryption algorithm;
[0146] S406: Decode the UTF-8 code to obtain characters and combine them into a text stream, convert the text stream into a binary stream, and obtain the first shader file.
[0147] In the above embodiment, for steps S401 to S403, see Figure 1 The description of steps S101 to S103 is not repeated here.
[0148] In the above implementation, for steps S404 to S405, the encrypted second shader file cannot be used directly and needs to be decrypted before use. The code of the self-developed decryption algorithm is:
[0149]
[0150]
[0151] The encryption algorithm encrypts 3 utf-8 codes (corresponding to chr1, chr2, chr3) into 4 encrypted bits (corresponding to enc1, enc2, enc3, enc4), corresponding to 4 encrypted characters. The decryption process is the reverse of the encryption method, and the specific implementation process is as follows:
[0152] 1. Extract 4 encrypted characters in the order of encrypted characters, and query the hexadecimal number of each encrypted character from the ASCII code table. Since the utf-8 code is binary, the hexadecimal number needs to be converted into a binary number to obtain four binary numbers, that is, four encrypted bits, including the first encryption bit, the second encryption bit, the third encryption bit and the fourth encryption bit;
[0153] 2. First, determine whether enc3 is a binary number of 64 (i.e., the fourth predetermined binary number). If so, since 64 corresponds to the character @, and the character @ does not appear in the code, chr2 and chr3 are set to empty; but if not, continue to determine whether enc4 is a binary number of 64. If so, chr3 is set to empty, otherwise the normal decryption process is performed;
[0154] 3. Normal decryption process:
[0155] ①chr1 is obtained by shifting the binary number in enc1 left by 2 bits and the binary number in enc2 right by 4 bits, and then performing an OR operation on the two;
[0156] ②chr2 performs a bitwise AND operation on the binary number in enc2 and 1111 (i.e., the second predetermined binary number), shifts the operation result left by four bits, shifts the binary number in enc3 right by two bits, and performs an OR operation on the two to obtain the result;
[0157] ③chr3 is obtained by performing a bitwise AND operation on the binary number in enc3 and 011 (i.e., the first predetermined binary number), shifting the operation result left by 6 bits, and performing an OR operation with the binary number in enc4.
[0158] For step S406, the decryption result of the decryption algorithm is a UTF-8 code, and the UTF-8 code needs to be decoded to obtain characters to be combined into the original first shader file. Figure 5 The description shown is not repeated here.
[0159] In the method provided in the above embodiment, the encrypted second shader file cannot be used directly, and needs to be decrypted by the reverse of the encryption algorithm, namely the decryption algorithm, before use, thereby ensuring the normal use of the shader file.
[0160] See also Figure 5 , shows a schematic flow chart of a method for decoding UTF-8 code according to an embodiment of the present invention, comprising the following steps:
[0161] S501: Divide a UTF-8 code into two groups of binary numbers with four binary numbers as a group from left to right, and convert each group of binary numbers into hexadecimal numbers to obtain two hexadecimal numbers;
[0162] S502: Searching for a character corresponding to the combined two hexadecimal numbers from an ASCII code table;
[0163] S503: Arrange and combine the queried characters according to the arrangement order of the UTF-8 code to obtain a text stream.
[0164] In the above implementation, for steps S501 to S503, this implementation describes the decoding process of the UTF-8 code, and the decoding code is as follows:
[0165]
[0166]
[0167] The specific implementation process is as follows (here is a specific example):
[0168] 1. Binary numbers in computers are used to store and represent information, such as the sequence 0100100001100101. In a UTF-8 code, the sequence is split into several groups with 4 binary digits as a group. For example, the result of splitting the above sequence is 01001000 0110 0101.
[0169] 2. Treat these binary sequences as 4 binary numbers, each with 4 bits. Then convert these 4 binary numbers to hexadecimal, and you get: 4 8 6 5. Each UTF-8 code corresponds to a 2-bit hexadecimal number.
[0170] Because the largest hexadecimal number that can be represented by 4 binary digits is f, through the above conversion, the number of bits is converted from the original 4 bits to 1 bit, so as to achieve the purpose of shortening the sequence length.
[0171] 3. Forcibly combine the above 4-digit hexadecimal number into a 2-digit hexadecimal number, such as: 48 65. The resulting 2-digit hexadecimal number is the unicode number.
[0172] 4. By comparing the hexadecimal ASCII code table, we can find the characters "H" and "e" corresponding to the above 48 and 65, which are combined into "He".
[0173] If the ASCII code table is in decimal, you must first convert the unicode number to decimal: 72 101, and then look up the corresponding character in the decimal ASCII code table.
[0174] The above translation from 4-bit binary numbers directly to the characters corresponding to the ASCII code table can be regarded as the principle of computer reading text files. In fact, since the characters that can be represented by the ASCII character set are far from meeting people's needs, large character sets such as Unicode are often used now, but the basic principle is no different from using the ASCII character set, except that more characters can be represented and the binary bits required are longer.
[0175] It should be noted that although the \r and \n line break characters need to be uniformly replaced with \n in advance during the encoding process, the reverse conversion will be directly performed during the decoding process to generate \r and \n instead of just \n.
[0176] The method provided in the above embodiment reversely parses according to the process of UTF-8 code, reversely decodes UTF-8 into Unicode number, thereby reading the corresponding characters and completing the generation of text stream.
[0177] See also Figure 6 , showing a schematic diagram of main modules of a device 600 for encrypting a shader file provided by an embodiment of the present invention, including:
[0178] The conversion module 601 is used to run the encryption script to convert the first shader file to be encrypted from a binary stream to a text stream;
[0179] An encoding module 602 is used to traverse the text stream and encode characters in the text stream into UTF-8 codes;
[0180] The encryption module 603 is used to encrypt the UTF-8 code using a predetermined encryption algorithm and generate a second shader file based on the encrypted string.
[0181] The implementation device of the present invention further includes a reading module 604 (not shown in the figure), which is used to: read the first file name and storage path of the first shader file, so as to read the first shader file corresponding to the first file name from the project file corresponding to the storage path;
[0182] And a storage module 605 (not shown in the figure), which is used to: store the second shader file in the project file corresponding to the storage path, and set the second file name of the second shader file to a combination of the first file name and predetermined characters.
[0183] In the implementation device of the present invention, the encoding module 602 is used to: determine the hexadecimal ASCII code corresponding to the character in the text stream, and use the hexadecimal ASCII code as the unicode number;
[0184] Convert the unicode number to a binary number, and fill the binary number values into the spaces in the predetermined binary format from right to left to get the UTF-8 code.
[0185] In the implementation device of the present invention, the encoding module 602 is further used to: uniformly replace the line breaks in the text stream with line breaks in a predetermined format.
[0186] In the implementation device of the present invention, the encryption module 603 is used to: extract three UTF-8 codes according to the order of the UTF-8 codes, shift the binary number in the first UTF-8 code right by two bits, and obtain the first encryption bit;
[0187] Perform a bitwise AND operation on the binary number in the first UTF-8 code and the first predetermined binary number and shift them left by four bits, shift the binary number in the second UTF-8 code right by four bits, and perform an OR operation on the two to obtain the second encrypted bit;
[0188] Perform a bitwise AND operation on the binary number in the second UTF-8 code and the second predetermined binary number and shift them left by two bits, shift the binary number in the third UTF-8 code right by six bits, perform an OR operation on the two, and obtain the third encrypted bit;
[0189] Perform a bitwise AND operation on the binary number in the third UTF-8 code and the third predetermined binary number to obtain a fourth encrypted bit;
[0190] The first encryption bit, the second encryption bit, the third encryption bit and the fourth encryption bit are converted into ASCII codes respectively, and the characters corresponding to the ASCII codes are searched and combined to obtain a four-bit encrypted string.
[0191] In the implementation device of the present invention, the encryption module 603 is also used for:
[0192] When the second UTF-8 code and the third UTF-8 code do not exist, setting the third encryption bit and the fourth encryption bit to a fourth predetermined binary number; or
[0193] When the third UTF-8 code does not exist, the fourth encryption bit is set to a fourth predetermined binary number.
[0194] The implementation device of the present invention further includes a decryption module 606 (not shown in the figure), which is used to:
[0195] In response to a load operation on the second shader file, reading an encrypted string in the second shader file;
[0196] Decrypt the encrypted string according to a predetermined decryption algorithm to obtain a UTF-8 code;
[0197] The UTF-8 code is decoded to obtain characters and combined into a text stream, and the text stream is converted into a binary stream to obtain the first shader file.
[0198] In the implementation device of the present invention, the decryption module 606 is used to:
[0199] According to the arrangement order of the encrypted characters, four encrypted characters are extracted, the ASCII codes corresponding to the encrypted characters are searched and converted into binary numbers, and the first encrypted bit, the second encrypted bit, the third encrypted bit and the fourth encrypted bit are obtained;
[0200] Shift the binary number in the first encrypted bit left by two bits, shift the binary number in the second encrypted bit right by four bits, and perform an OR operation on the two to get the first UTF-8 code;
[0201] Perform a bitwise AND operation on the binary number in the second encrypted bit and the second predetermined binary number and shift them left by four bits, shift the binary number in the third encrypted bit right by two bits, perform an OR operation on the two, and obtain a second UTF-8 code;
[0202] The binary number in the third encryption position is bitwise ANDed with the first predetermined binary number and shifted left by six positions, and then ORed with the binary number in the fourth encryption position to obtain a third UTF-8 code.
[0203] In the implementation device of the present invention, the decryption module 606 is also used for:
[0204] Determine whether the third encryption bit is a fourth predetermined binary number, and if the determination result is yes, set the second UTF-8 code and the third UTF-8 code to empty;
[0205] If the judgment result is no, then determine whether the fourth encryption bit is the fourth predetermined binary number. If the judgment result is yes, set the third UTF-8 code to empty.
[0206] In the implementation device of the present invention, the decryption module 606 is used to:
[0207] From left to right, divide a UTF-8 code into two groups of binary numbers with four binary digits as a group, and convert each group of binary numbers into hexadecimal to obtain two hexadecimal numbers;
[0208] Look up the character corresponding to the combined two hexadecimal numbers from the ASCII code table;
[0209] Arrange and combine the queried characters according to the order of the UTF-8 code to obtain a text stream.
[0210] In addition, the specific implementation content of the device described in the embodiment of the present invention has been described in detail in the method described above, so the repeated content will not be described again here.
[0211] Figure 7 An exemplary system architecture 700 is shown in which embodiments of the present invention may be applied.
[0212] like Figure 7 As shown, system architecture 700 may include terminal devices 701, 702, 703, network 704 and server 705 (just an example). Network 704 is used to provide a medium for communication links between terminal devices 701, 702, 703 and server 705. Network 704 may include various connection types, such as wired, wireless communication links or optical fiber cables, etc.
[0213] The user can use the terminal devices 701, 702, 703 to interact with the server 705 through the network 704 to receive or send messages, etc. Various communication client applications can be installed on the terminal devices 701, 702, 703.
[0214] The terminal devices 701, 702, and 703 may be various electronic devices having display screens and supporting web browsing, and the server 705 may be a server providing various services.
[0215] It should be noted that the method provided in the embodiment of the present invention is generally executed by the server 705 , and accordingly, the device is generally set in the server 705 .
[0216] It should be understood that Figure 7 The number of terminal devices, networks and servers in the embodiment is only for illustration. Any number of terminal devices, networks and servers may be provided according to implementation requirements.
[0217] Reference below Figure 8 , which shows a schematic diagram of the structure of a computer system 800 of a terminal device suitable for implementing an embodiment of the present invention. Figure 8 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0218] like Figure 8 As shown, the computer system 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage part 808 into a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the system 800 are also stored. The CPU 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.
[0219] The following components are connected to the I / O interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the I / O interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed, so that a computer program read therefrom is installed into the storage section 808 as needed.
[0220] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 809, and / or installed from the removable medium 811. When the computer program is executed by the central processing unit (CPU) 801, the above-mentioned functions defined in the system of the present invention are executed.
[0221] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0222] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0223] The modules involved in the embodiments of the present invention may be implemented by software or hardware. The modules described may also be set in a processor, for example, it may be described as: a processor includes a conversion module, an encoding module, and an encryption module. The names of these modules do not constitute a limitation on the modules themselves in some cases, for example, the encryption module may also be described as an "encryption character module".
[0224] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes:
[0225] Run the encryption script to convert the first shader file to be encrypted from a binary stream to a text stream;
[0226] Traversing the text stream, encoding the characters in the text stream into UTF-8 codes;
[0227] The UTF-8 code is encrypted using a predetermined encryption algorithm, and a second shader file is generated based on the encrypted character string.
[0228] According to the technical solution of the embodiment of the present invention, in order to prevent the shader from being used directly, a self-developed encryption algorithm is used to perform bit operations on the shader file, so that the plaintext shader code cannot be run directly, reducing its editability and ensuring the security of the file; before subsequent use, it needs to be decrypted using the reverse of the encryption algorithm, that is, the decryption algorithm, to ensure the normal use of the shader file.
[0229] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A method for encrypting a shader file, characterized in that: include: Run the encryption script to convert the first shader file to be encrypted from a binary stream to a text stream; Traversing the text stream, encoding the characters in the text stream into UTF-8 codes; Encrypting the UTF-8 code using a predetermined encryption algorithm, and generating a second shader file based on the encrypted string; wherein, encrypting the UTF-8 code using a predetermined encryption algorithm includes: According to the order of the UTF-8 codes, extract three UTF-8 codes, shift the binary number in the first UTF-8 code right by two bits, and get the first encrypted bit; Perform a bitwise AND operation on the binary number in the first UTF-8 code and the first predetermined binary number and shift them left by four bits, shift the binary number in the second UTF-8 code right by four bits, and perform an OR operation on the two to obtain the second encrypted bit; Perform a bitwise AND operation on the binary number in the second UTF-8 code and the second predetermined binary number and shift them left by two bits, shift the binary number in the third UTF-8 code right by six bits, perform an OR operation on the two, and obtain the third encrypted bit; Perform a bitwise AND operation on the binary number in the third UTF-8 code and the third predetermined binary number to obtain a fourth encrypted bit; The first encryption bit, the second encryption bit, the third encryption bit and the fourth encryption bit are converted into ASCII codes respectively, and the characters corresponding to the ASCII codes are searched and combined to obtain a four-bit encrypted string.
2. The method according to claim 1, characterized in that: Before converting the first shader file to be encrypted from a binary stream to a text stream, the method further includes: Reading a first file name and a storage path of the first shader file, so as to read a first shader file corresponding to the first file name from a project file corresponding to the storage path; After generating the second shader file based on the encrypted string, the method further includes: The second shader file is stored in a project file corresponding to the storage path, and a second file name of the second shader file is set to a combination of the first file name and predetermined characters.
3. The method according to claim 1, characterized in that The encoding of characters in the text stream into UTF-8 codes includes: Determine the hexadecimal ASCII code corresponding to the character in the text stream, and use the hexadecimal ASCII code as a unicode number; Convert the unicode number to a binary number, and fill the values in the binary number into the spaces in the predetermined binary format from right to left to get the UTF-8 code.
4. The method according to claim 3, characterized in that Before determining the ASCII code corresponding to the character in the text stream, the method further includes: uniformly replacing the line breaks in the text stream with line breaks in a predetermined format.
5. The method according to claim 1, characterized in that The method further comprises: When the second UTF-8 code and the third UTF-8 code do not exist, setting the third encryption bit and the fourth encryption bit to a fourth predetermined binary number; or When the third UTF-8 code does not exist, the fourth encryption bit is set to a fourth predetermined binary number.
6. The method according to claim 5, characterized in that After generating the second shader file based on the encrypted string, the method further includes: In response to a load operation on the second shader file, reading an encrypted string in the second shader file; Decrypt the encrypted string according to a predetermined decryption algorithm to obtain a UTF-8 code; The UTF-8 code is decoded to obtain characters and combined into a text stream, and the text stream is converted into a binary stream to obtain the first shader file.
7. The method according to claim 6, characterized in that Decrypting the encrypted string according to a predetermined decryption algorithm to obtain a UTF-8 code includes: According to the arrangement order of the encrypted characters, four encrypted characters are extracted, the ASCII codes corresponding to the encrypted characters are searched and converted into binary numbers, and the first encrypted bit, the second encrypted bit, the third encrypted bit and the fourth encrypted bit are obtained; Shift the binary number in the first encrypted bit left by two bits, shift the binary number in the second encrypted bit right by four bits, and perform an OR operation on the two to get the first UTF-8 code; Perform a bitwise AND operation on the binary number in the second encrypted bit and the second predetermined binary number and shift them left by four bits, shift the binary number in the third encrypted bit right by two bits, perform an OR operation on the two, and obtain a second UTF-8 code; The binary number in the third encryption position is bitwise ANDed with the first predetermined binary number and shifted left by six positions, and then ORed with the binary number in the fourth encryption position to obtain a third UTF-8 code.
8. The method according to claim 7, characterized in that The method further comprises: Determine whether the third encryption bit is a fourth predetermined binary number, and if the determination result is yes, set the second UTF-8 code and the third UTF-8 code to empty; If the judgment result is no, then determine whether the fourth encryption bit is the fourth predetermined binary number. If the judgment result is yes, set the third UTF-8 code to empty.
9. The method according to claim 6, characterized in that The decoding of the UTF-8 code to obtain characters and combining them into a text stream includes: From left to right, divide a UTF-8 code into two groups of binary numbers with four binary digits as a group, and convert each group of binary numbers into hexadecimal to obtain two hexadecimal numbers; Look up the character corresponding to the combined two hexadecimal numbers from the ASCII code table; Arrange and combine the queried characters according to the order of the UTF-8 code to obtain a text stream.
10. A device for encrypting a shader file, characterized in that: include: A conversion module, used for running an encryption script to convert the first shader file to be encrypted from a binary stream to a text stream; An encoding module, used for traversing the text stream and encoding characters in the text stream into UTF-8 codes; An encryption module is used to encrypt the UTF-8 code using a predetermined encryption algorithm and generate a second shader file based on the encrypted string; wherein, encrypting the UTF-8 code using a predetermined encryption algorithm includes: According to the order of the UTF-8 codes, extract three UTF-8 codes, shift the binary number in the first UTF-8 code right by two bits, and get the first encrypted bit; Perform a bitwise AND operation on the binary number in the first UTF-8 code and the first predetermined binary number and shift them left by four bits, shift the binary number in the second UTF-8 code right by four bits, and perform an OR operation on the two to obtain the second encrypted bit; Perform a bitwise AND operation on the binary number in the second UTF-8 code and the second predetermined binary number and shift them left by two bits, shift the binary number in the third UTF-8 code right by six bits, perform an OR operation on the two, and obtain the third encrypted bit; Perform a bitwise AND operation on the binary number in the third UTF-8 code and the third predetermined binary number to obtain a fourth encrypted bit; The first encryption bit, the second encryption bit, the third encryption bit and the fourth encryption bit are converted into ASCII codes respectively, and the characters corresponding to the ASCII codes are searched and combined to obtain a four-bit encrypted string.
11. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 9.
12. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.
Citation Information
Patent Citations
Encrypting method and decrypting method corresponding to same
CN101996298A
Method, device and system for encryption and decryption of communication
CN103684760A
File decryption and evidence collection method and device in storage medium
CN109710899A
Encoding method, decoding method, related devices and computer equipment
CN110768785A
Method, device and equipment for protecting Shader
CN111228819A
