A code encryption method and device
By performing numerical conversion and offset processing of code files, combining lexical, syntax and semantic analysis to generate encrypted files, the problem of low complexity and security of code encryption in the existing technology is solved, and efficient code protection is achieved.
Patent Information
- Application Number
- CN202010086750.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-02-11
- Publication Date
- 2025-04-15
- Estimated Expiration
- 2040-02-11
AI Technical Summary
The existing code encryption method is too complex and not very secure, making it difficult to effectively protect the code from leakage and churn.
By traversing the code file, obtaining the character stream and performing numerical conversion, offsetting numeric characters with offsets, generating encrypted files, and optionally using lexical analysis, grammatical analysis and semantic analysis to identify and correct error positions, further improving the complexity of encryption.
It effectively reduces the readability of the code file and destroys the lexical structure of the code file, making it impossible to execute even by grabbing the file through packet capture, significantly improving the confidentiality and security of the code file.
Smart Images

Figure CN113254889B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a code encryption method and device. Background Art
[0002] Program software codes are important intellectual assets of software companies. The leakage and loss of codes will bring direct economic losses to software companies and also bring great risks to the data security of product users. How to strengthen the security of codes and prevent them from being easily obtained is an aspect that software companies are currently paying more attention to.
[0003] In the process of implementing the present invention, the inventors found that the prior art has at least the following problems:
[0004] The existing code encryption method mainly reduces the readability of the code. It is usually encrypted separately on the front end, and all files need to be downloaded to analyze the encryption method. The encryption method is too fixed and the operation is relatively complicated. Summary of the invention
[0005] In view of this, an embodiment of the present invention provides a code encryption method and device, which can at least solve the problem that code encryption in the prior art is too complicated and has low security.
[0006] To achieve the above object, according to one aspect of an embodiment of the present invention, a code encryption method is provided, comprising:
[0007] Traverse the code file and obtain the character stream in the code file;
[0008] Performing digital conversion on the characters in the character stream to obtain converted digital characters;
[0009] The digital characters are offset by using the offset to obtain the offset digital characters, and the code file composed of the offset digital characters is used as an encrypted file.
[0010] Optionally, after obtaining the character stream in the code file, the method further includes:
[0011] Scanning the character stream using lexical analysis to identify word sequences in the character stream according to word formation rules;
[0012] Performing grammatical analysis on the word sequence to determine whether there is a grammatical error in the word sequence, and if so, determining a first error position;
[0013] Performing a type inspection on the character stream by using semantic analysis to determine whether there is a semantic error in the structure of the character stream, and if so, determining a second error position;
[0014] Correction information for the characters in the first error position and the second error position is received to obtain a corrected character stream.
[0015] Optionally, before performing digital conversion on the characters in the character stream, the method further includes:
[0016] Using lexical analysis to identify numbers in the word sequence, and then in the syntax tree of the code, using the syntax analysis to remove numbers in variable positions;
[0017] Perform base conversion on the filtered numbers to obtain the first base sequence.
[0018] Optionally, the step of offsetting the numeric character by using the offset to obtain the offset numeric character further includes:
[0019] Performing base conversion on the offset and the digital character respectively to obtain a first binary sequence and a third base sequence;
[0020] The third binary sequence is shifted using the second binary sequence to obtain a shifted third binary sequence.
[0021] Optionally, the step of using the code file composed of the offset numeric characters as an encrypted file further includes:
[0022] The offset digital characters are converted into a base-4 sequence, and a code file composed of the base-4 sequence is used as an encrypted file.
[0023] Optionally, after using the code file composed of the offset numeric characters as an encrypted file, the method further includes:
[0024] Obtaining a current timestamp, encrypting the offset using the current timestamp to obtain an encrypted offset, and using the encrypted offset as a key;
[0025] The storage location of the code file is determined, so as to store the current timestamp, the key and the encrypted file together in the storage location.
[0026] Optionally, the using the current timestamp to encrypt the offset to obtain the encrypted offset includes:
[0027] Performing base conversion on the current timestamp to obtain a fifth base sequence;
[0028] The fifth base-1 sequence is used to encrypt the first binary sequence to obtain an encrypted first binary sequence.
[0029] Optionally, the encrypting the offset by using the current timestamp includes:
[0030] Perform a logical operation on the offset and the current timestamp; wherein the logical operation is AND logic or DIF logic.
[0031] Optionally, the storage location is a server;
[0032] After storing the current timestamp, the key and the encrypted file together in the storage location, the method further includes:
[0033] In response to a decryption operation on the encrypted file, the key is sent to a server, so that the key is decrypted on the server based on the current timestamp to obtain the offset;
[0034] Receiving the offset fed back by the server, inputting the offset and the encrypted file into a restoration function, and obtaining the converted digital character;
[0035] The converted numeric characters are subjected to reverse base conversion and combined to obtain the code file.
[0036] To achieve the above object, according to another aspect of an embodiment of the present invention, a code encryption device is provided, comprising:
[0037] A traversal module, used for traversing a code file and obtaining a character stream in the code file;
[0038] A conversion module, used for performing digital conversion on the characters in the character stream to obtain converted digital characters;
[0039] The offset module is used to offset the digital characters using the offset to obtain the offset digital characters, and use the code file composed of the offset digital characters as the encrypted file.
[0040] Optionally, the traversal module is further used to:
[0041] Scanning the character stream using lexical analysis to identify word sequences in the character stream according to word formation rules;
[0042] Performing grammatical analysis on the word sequence to determine whether there is a grammatical error in the word sequence, and if so, determining a first error position;
[0043] Performing a type inspection on the character stream by using semantic analysis to determine whether there is a semantic error in the structure of the character stream, and if so, determining a second error position;
[0044] Correction information for the characters in the first error position and the second error position is received to obtain a corrected character stream.
[0045] Optionally, the conversion module is further used to:
[0046] Using lexical analysis to identify numbers in the word sequence, and then in the syntax tree of the code, using the syntax analysis to remove numbers in variable positions;
[0047] Perform base conversion on the filtered numbers to obtain the first base sequence.
[0048] Optionally, the offset module is further used for:
[0049] Performing base conversion on the offset and the digital character respectively to obtain a first binary sequence and a third base sequence;
[0050] The third binary sequence is shifted using the second binary sequence to obtain a shifted third binary sequence.
[0051] Optionally, the offset module is further used for:
[0052] The offset digital characters are converted into a base-4 sequence, and a code file composed of the base-4 sequence is used as an encrypted file.
[0053] Optionally, an encryption module is also included for:
[0054] Obtaining a current timestamp, encrypting the offset using the current timestamp to obtain an encrypted offset, and using the encrypted offset as a key;
[0055] The storage location of the code file is determined, so as to store the current timestamp, the key and the encrypted file together in the storage location.
[0056] Optionally, the encryption module is used to:
[0057] Performing base conversion on the current timestamp to obtain a fifth base sequence;
[0058] The fifth base-1 sequence is used to encrypt the first binary sequence to obtain an encrypted first binary sequence.
[0059] Optionally, the encryption module is used to:
[0060] Perform a logical operation on the offset and the current timestamp; wherein the logical operation is AND logic or DIF logic.
[0061] Optionally, the storage location is a server;
[0062] The device also includes a decryption module, which is used to:
[0063] In response to a decryption operation on the encrypted file, the key is sent to a server, so that the key is decrypted on the server based on the current timestamp to obtain the offset;
[0064] Receiving the offset fed back by the server, inputting the offset and the encrypted file into a restoration function, and obtaining the converted digital character;
[0065] The converted numeric characters are subjected to reverse base conversion and combined to obtain the code file.
[0066] To achieve the above objective, according to another aspect of an embodiment of the present invention, a code encryption electronic device is provided.
[0067] The electronic device of the 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 code encryption methods.
[0068] To achieve the above object, 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-mentioned code encryption methods is implemented.
[0069] According to the solution provided by the present invention, an embodiment of the above invention has the following advantages or beneficial effects: through ASCII conversion, the characters in the original code file are converted into digital form, reducing the readability of the code file; the variable value is transformed according to the offset, destroying the lexical structure of the code file, resulting in the inability to execute the file even if the file is obtained by packet capture later, effectively improving the confidentiality and security of the code file.
[0070] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS
[0071] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0072] Figure 1 This is a schematic diagram of the main flow of a code encryption method according to an embodiment of the present invention;
[0073] Figure 2 is a schematic diagram of converting variable values in a syntax tree according to an embodiment of the present invention;
[0074] Figure 3is a flow chart of an optional code encryption method according to an embodiment of the present invention;
[0075] Figure 4 is a flow chart of another optional code encryption method according to an embodiment of the present invention;
[0076] Figure 5 is a flowchart of another optional code encryption method according to an embodiment of the present invention;
[0077] Figure 6 is a schematic diagram of main modules of a code encryption device according to an embodiment of the present invention;
[0078] Figure 7 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0079] 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
[0080] 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.
[0081] It should be noted that there are many types of languages used in code files, such as JS (short for JavaScript), C, Java, Python and other programming languages. Considering the versatility of JS language, the present invention is mainly explained using JS language as an example.
[0082] In addition, the base conversion involved in the present invention is mainly the conversion between decimal and hexadecimal. In addition to this conversion form, other conversion methods can also be used, but it is necessary to ensure that the same conversion form is used as a whole.
[0083] See also Figure 1 , which shows a main flow chart of a code encryption method provided by an embodiment of the present invention, comprising the following steps:
[0084] S101: traverse the code file to obtain the character stream in the code file;
[0085] S102: performing digital conversion on the characters in the character stream to obtain converted digital characters;
[0086] S103: offset the numeric characters using the offset to obtain the offset numeric characters, and use the code file composed of the offset numeric characters as the encrypted file.
[0087] In the above implementation, for step S101, the present invention is mainly described based on the webpack plug-in; wherein, webpack is a module bundler, which analyzes the project structure, finds out JS modules and other extended languages that cannot be directly run by other browsers (such as Scss, TypeScript, etc.), and packages them into a suitable format for browser use.
[0088] It should be noted that the present invention is a plug-in developed based on webpack, and the execution subject is webpack, which cannot be separated. In the front-end development process, webpack is more in line with the front-end engineering structure.
[0089] Therefore, before executing code encryption, the present invention first needs to download the plug-in. Specifically, the plug-in name is entered in npm (nodepackage manager) to search for the webpack plug-in, and then downloaded to the local and introduced in webpack.config.js to add it to the plugin.
[0090] Npm plug-in libraries are divided into public libraries and private libraries. When an encryption plug-in is published to a public library, all users can download and install it; but if it is published to a private library, only internal personnel of the enterprise or some designated internal personnel can download and install it.
[0091] As a js plug-in, js encryption plug-in can receive js files from webpack. Traverse the js file and obtain the character stream in the js file; a character stream can contain one or more strings, a character is a subset of a string, and a string contains one or more characters.
[0092] For step S102, there are several ways to obfuscate the code: splitting the string, splitting the array, and adding waste code. Considering that there are many shader languages and the project is large, the present invention mainly adopts the method of splitting the string.
[0093] See also Figure 2 As shown, a syntax tree AST (Abstract Syntax Tree) is constructed. According to the syntax tree rules, lexical analysis is used to determine the numbers in the code, and then syntax analysis is used to determine the numbers at the variable value positions, where the variables are at the left leaf nodes and the variable values are at the right leaf nodes, and only the numbers in the right leaf nodes are converted from decimal to hexadecimal.
[0094] In addition to alphabetic characters, the character stream may also contain digital characters. To unify the format, these digital characters can be converted into hexadecimal format in advance and then converted together with the alphabetic characters to reduce errors and improve the conversion success rate.
[0095] By using the ASCII (American Standard Code for Information Interchange) comparison table (here only as an example), characters in the character stream are converted into digits to obtain a character stream in ASCII digital form.
[0096] For step S103, a random number between 0 and 10 is generated (for ASCII, it can take values of 0 to 127, which is only an example here) and saved as the offset devation. Assuming the offset is 3, the original digital character 57 is offset to obtain 60. It should be noted that the character stream converted by the ASCII comparison table is still executable, but the character stream offset by the offset has destroyed its structure and cannot be executed.
[0097] Since strings are usually in alphabetical form, the characters that make up the code file should be in hexadecimal form. The conversion process can occur before or after the offset:
[0098] 1) Before offset: Convert the offset and numeric characters from decimal to hexadecimal to obtain the binary sequence (devationHEX) and the third sequence (strHEX), then sum devationHEX and strHEX to obtain the new strHEX.
[0099] 2) After the offset: the numeric character is offset by using the offset to obtain the numeric character after the offset, and the numeric character after the offset is converted from decimal to hexadecimal to obtain the fourth base sequence.
[0100] The method provided in the above embodiment converts the characters in the original code file into digital form through ASCII conversion, thereby reducing the readability of the code file; transforms the variable value according to the offset, destroying the lexical structure of the code file, resulting in the inability to execute the file even if the file is subsequently obtained through packet capture, thereby effectively improving the confidentiality and security of the code file.
[0101] See also Figure 3 , shows a schematic flow chart of an optional code encryption method according to an embodiment of the present invention, comprising the following steps:
[0102] S301: traverse the code file to obtain the character stream in the code file;
[0103] S302: Scanning the character stream by using lexical analysis to identify a word sequence in the character stream according to a word formation rule;
[0104] S303: Performing grammatical analysis on the word sequence to determine whether there is a grammatical error in the word sequence, and if so, determining a first error position;
[0105] S304: Performing type inspection on the character stream by using semantic analysis to determine whether there is a semantic error in the structure of the character stream, and if so, determining a second error position;
[0106] S305: receiving correction information of the characters in the first error position and the second error position to obtain a corrected character stream;
[0107] S306: Perform digital conversion on the characters in the character stream to obtain converted digital characters;
[0108] S307: offset the numeric characters using the offset to obtain the offset numeric characters, and use the code file composed of the offset numeric characters as the encrypted file.
[0109] In the above embodiment, for steps S301, S306 and S307, see Figure 1 The description of steps S101 to S103 is not repeated here.
[0110] In the above implementation, for step S302, usually there may be errors in the code writing process, and if it is directly encrypted, the errors will be encrypted together. When the code file obtained by subsequent decryption runs incorrectly, it is impossible to determine whether it is a code error or a decryption error, and it is impossible to track the location of the problem. Therefore, it is necessary to find the error in the code before encryption to facilitate modification.
[0111] Specifically, the character stream is subjected to lexical analysis, grammatical analysis, and semantic analysis to check whether there are non-logical errors in the written js code, mainly including grammatical errors, spelling errors, etc., to ensure that the js file can be compiled correctly.
[0112] Lexical analysis is the first stage of the compilation process and the basis of compilation. The task of this stage is to read the source program character by character from left to right, that is, to scan the character stream that constitutes the source program, and then recognize the word sequence (also called word symbol or symbol) according to the word formation rules, and give qualitative and fixed-length processing to the recognized words.
[0113] For step S303, syntax analysis is a logical stage of the compilation process. The task of syntax analysis is to combine word sequences into various grammatical phrases, such as "program", "statement", "expression", etc., based on lexical analysis. Syntax analysis determines whether the source program is structurally correct, and the structure of the source program is described by a context-free grammar.
[0114] According to the grammatical rules of the source language, the corresponding grammatical categories are identified from the results of lexical analysis, and grammar checking, reporting and processing of grammatical errors are performed at the same time.
[0115] Regarding step S304, semantic analysis is a logical stage in the programming process, which is used to examine the context-related properties of the structurally correct source program and perform type examination.
[0116] Semantic analysis is to check whether there are semantic errors in the source program and collect type information for the code generation stage. For example, one of the tasks of semantic analysis is to perform type review to check whether each operator has operands allowed by the language specification. When it does not meet the language specification, the compiler should report an error. For example, some compilers will report errors when real numbers are used as array subscripts. For example, some programs stipulate that operands can be forced. Then when a binary operation is applied to an integer and a real object, the compiler should convert the integer to a real type and cannot consider it an error in the source program.
[0117] For step S305, the above three analyses are indispensable and the order is fixed, which is the compilation order of the compiler. And this plug-in only provides the error location and error cause, and the subsequent correction is mainly done manually to obtain the corrected character stream.
[0118] In addition, comments are usually marked in js files, and the comment format is / / and / ** / . Considering that comments are not necessary for js file encryption, they can be deleted to increase the encryption speed and reduce the file size. Specifically, regular expressions are used to judge the comments in js files, determine the comment locations, and delete them.
[0119] The method provided in the above embodiment performs error checking on the character stream through lexical analysis, grammatical analysis and semantic analysis, and the subsequently encrypted code is the corrected code, thereby avoiding the problem that the encrypted code fails to run after decryption and the problem cannot be tracked.
[0120] See also Figure 4 , shows a schematic flow chart of another optional code encryption method according to an embodiment of the present invention, comprising the following steps:
[0121] S401: traverse the code file to obtain the character stream in the code file;
[0122] S402: Perform digital conversion on the characters in the character stream to obtain converted digital characters;
[0123] S403: offset the numeric characters using the offset to obtain the offset numeric characters, and use the code file composed of the offset numeric characters as the encrypted file;
[0124] S404: Obtain a current timestamp, use the current timestamp to encrypt the offset to obtain an encrypted offset, and use the encrypted offset as a key;
[0125] S405: Determine the storage location of the code file, so as to store the current timestamp, the key and the encrypted file together in the storage location.
[0126] In the above implementation, for steps S401 to S403, see Figure 1 The description of steps S101 to S103 is not repeated here.
[0127] In the above implementation, for step S404, after the encrypted file is obtained, it needs to be transferred to the location where the original code file is stored for continued storage.
[0128] pass Figure 1 From the description, it can be seen that for the encryption process of the code file, in addition to considering digital conversion, the most critical part is the offset, and the offset depends on the determination of the offset (random number). The subsequent file decryption is the reverse decryption through the offset.
[0129] Get the system time timestamp of the current computer (accurate to milliseconds), and use the logical "and" or "difference" method to calculate the current timestamp and offset to generate the key secret.
[0130] Furthermore, assuming that the offset is in hexadecimal form at this time, the logical operation also needs to convert the current timestamp into hexadecimal to generate timeStampHex (ie, the fifth hexadecimal sequence), and perform logical operations (including sum logic or difference logic) on devationHex and timeStampHex to generate the key secret.
[0131] For step S405, the generated key and current timestamp will be sent together with the encrypted file to the storage location of the code file (usually the server), where the current timestamp is used to decrypt the key to obtain the offset, and the obtained offset is used to decrypt the encrypted file.
[0132] When a subsequent file is requested, the server will send the key to the front-end page, and the front-end page will decrypt the file according to the offset in the sent key.
[0133] It should be noted that the current timestamp transmitted to the server may be in decimal form, such as timestamp, or in hexadecimal form, such as timeStampHex, and the present invention does not limit the specific form.
[0134] The method provided in the above embodiment, even if the repetition probability of the generated random number is high, the result obtained after performing logical operation on it using the current timestamp is different, thereby ensuring the uniqueness of the key and effectively ensuring the encryption uniqueness of the encrypted file.
[0135] See also Figure 5 , shows a schematic flow chart of another optional code encryption method according to an embodiment of the present invention, comprising the following steps:
[0136] S501: traverse the code file to obtain the character stream in the code file;
[0137] S502: Perform digital conversion on the characters in the character stream to obtain converted digital characters;
[0138] S503: offset the numeric characters using the offset to obtain the offset numeric characters, and use the code file composed of the offset numeric characters as the encrypted file;
[0139] S504: Obtain a current timestamp, use the current timestamp to encrypt the offset to obtain an encrypted offset, and use the encrypted offset as a key;
[0140] S505: Determine the storage location of the code file, so as to store the current timestamp, the key and the encrypted file together in the storage location;
[0141] S506: In response to the decryption operation on the encrypted file, the key is sent to the server, so that the key is decrypted on the server based on the current timestamp to obtain the offset;
[0142] S507: receiving the offset fed back by the server, inputting the offset and the encrypted file into a restoration function, and obtaining the converted digital character;
[0143] S508: Perform reverse base conversion on the converted numeric characters and combine them to obtain the code file.
[0144] In the above embodiment, steps S501 to S503 can be referred to Figure 1 For the description of steps S101 to S103, steps S504 and S505 can be found in Figure 4 The description of steps S404 and S405 is not repeated here.
[0145] In the above implementation, for step S506, the secret decryption method is stored in the server, and the secret is sent to the server using the https protocol, and the server returns the offset. Taking the AND logic operation as an example, secret = devation + timestamp, then the offset devotion = secret - timestamp can be obtained.
[0146] It should be noted that after the key is generated by performing AND operation based on the current timestamp and offset, the current timestamp and key will be uploaded to the backend along with the encrypted file, and the frontend will only retain the key. When the frontend needs to obtain the offset to decrypt the file, it needs to send the key to the backend. After the backend queries the corresponding timestamp, it can calculate the offset and feed it back to the frontend.
[0147] Furthermore, before the front-end obtains the encrypted js file, it can be based on the user account login and the back-end verification of the account HTTP request cookie to determine whether the user has permission to access. When the front-end requests, the js file can be sent first, and if the identity authentication is passed, the key can be uploaded to the back-end to obtain the offset.
[0148] For step S507, the encrypted js file cannot be directly executed and must be decrypted with the offset in the key to be converted into an executable js file.
[0149] After receiving the offset feedback from the backend, the frontend will reverse the offset of the encrypted file based on the offset. For example, if the previous operation was an "addition" offset operation, such as 57+3=60, then the decryption process is a "difference" offset operation, that is, 60-3=57; but if the previous encryption was a "difference" offset operation, the decryption process should be an "addition" offset operation, and the two are inversely related. Therefore, the encryption method and / or decryption method can also be uploaded to the server.
[0150] In addition, the encrypted file can also be restored based on the restore function; wherein the restore function is a function that converts the encrypted file into an executable obfuscated file, the input value is the encrypted file and the offset, and the output value is the executable file, that is, the obfuscated code file.
[0151] For step S508, the numeric characters in the obfuscated code file are further reversely converted using the ASCII comparison table to obtain a code file containing characters such as letters and numbers.
[0152] If the numbers at the variable value positions in the code have been converted from decimal to hexadecimal, then the characters at the variable value positions also need to be reversely converted to finally obtain the original code file.
[0153] In the method provided in the above embodiment, the key and the obfuscated code are paired, and each encrypted file can only be decrypted with the key generated at that time. In addition, user information can be mixed into the code file for authority verification, thereby improving the encryption security of the code.
[0154] See also Figure 6 , shows a schematic diagram of the main modules of a code encryption device 600 provided in an embodiment of the present invention, including:
[0155] A traversal module 601 is used to traverse a code file and obtain a character stream in the code file;
[0156] The conversion module 602 is used to perform digital conversion on the characters in the character stream to obtain converted digital characters;
[0157] The offset module 603 is used to offset the numeric characters using the offset to obtain the offset numeric characters, and use the code file composed of the offset numeric characters as the encrypted file.
[0158] In the implementation device of the present invention, the traversal module 601 is also used for:
[0159] Scanning the character stream using lexical analysis to identify word sequences in the character stream according to word formation rules;
[0160] Performing grammatical analysis on the word sequence to determine whether there is a grammatical error in the word sequence, and if so, determining a first error position;
[0161] Performing a type inspection on the character stream by using semantic analysis to determine whether there is a semantic error in the structure of the character stream, and if so, determining a second error position;
[0162] Correction information for the characters in the first error position and the second error position is received to obtain a corrected character stream.
[0163] In the implementation device of the present invention, the conversion module 602 is also used for:
[0164] Using lexical analysis to identify numbers in the word sequence, and then in the syntax tree of the code, using the syntax analysis to remove numbers in variable positions;
[0165] Perform base conversion on the filtered numbers to obtain the first base sequence.
[0166] In the implementation device of the present invention, the offset module 603 is also used for:
[0167] Performing base conversion on the offset and the digital character respectively to obtain a first binary sequence and a third base sequence;
[0168] The third binary sequence is shifted using the second binary sequence to obtain a shifted third binary sequence.
[0169] In the implementation device of the present invention, the offset module 603 is also used for:
[0170] The offset digital characters are converted into a base-4 sequence, and a code file composed of the base-4 sequence is used as an encrypted file.
[0171] The implementation device of the present invention further includes an encryption module 604 (not shown in the figure), which is used to:
[0172] Obtaining a current timestamp, encrypting the offset using the current timestamp to obtain an encrypted offset, and using the encrypted offset as a key;
[0173] The storage location of the code file is determined, so as to store the current timestamp, the key and the encrypted file together in the storage location.
[0174] In the implementation device of the present invention, the encryption module 604 is used to:
[0175] Performing base conversion on the current timestamp to obtain a fifth base sequence;
[0176] The fifth base-1 sequence is used to encrypt the first binary sequence to obtain an encrypted first binary sequence.
[0177] In the implementation device of the present invention, the encryption module 604 is used to:
[0178] Perform a logical operation on the offset and the current timestamp; wherein the logical operation is AND logic or DIF logic.
[0179] In the implementation device of the present invention, the storage location is a server;
[0180] The device further includes a decryption module 605 (not shown in the figure), which is used to:
[0181] In response to a decryption operation on the encrypted file, the key is sent to a server, so that the key is decrypted on the server based on the current timestamp to obtain the offset;
[0182] Receiving the offset fed back by the server, inputting the offset and the encrypted file into a restoration function, and obtaining the converted digital character;
[0183] The converted numeric characters are subjected to reverse base conversion and combined to obtain the code file.
[0184] 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.
[0185] Figure 7 An exemplary system architecture 700 is shown in which embodiments of the present invention may be applied.
[0186] 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.
[0187] 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.
[0188] The terminal devices 701 , 702 , and 703 may be various electronic devices having a display screen and supporting web browsing, including but not limited to smart phones, tablet computers, laptop computers, and desktop computers.
[0189] The server 705 may be a server that provides various services, such as a background management server that provides support for shopping websites browsed by users using the terminal devices 701 , 702 , and 703 (only as an example).
[0190] 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 .
[0191] 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.
[0192] 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 8The 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.
[0193] 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.
[0194] 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.
[0195] 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.
[0196] 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.
[0197] 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.
[0198] 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 traversal module, a conversion module, and an offset module. The names of these modules do not constitute a limitation on the modules themselves in some cases, for example, the offset module may also be described as a "code offset module".
[0199] 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:
[0200] Traverse the code file and obtain the character stream in the code file;
[0201] Performing digital conversion on the characters in the character stream to obtain converted digital characters;
[0202] The digital characters are offset by using the offset to obtain the offset digital characters, and the code file composed of the offset digital characters is used as an encrypted file.
[0203] According to the technical solution of the embodiment of the present invention, the characters in the original code file are converted into digital form through ASCII conversion, which reduces the readability of the code file; the variable value is transformed according to the offset, which destroys the lexical structure of the code file, resulting in the inability to execute the file even if the file is obtained through packet capture later, thereby effectively improving the confidentiality and security of the code file.
[0204] 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 code encryption method, characterized in that: include: Traverse the code file and obtain the character stream in the code file; Performing digital conversion on the characters in the character stream to obtain converted digital characters; wherein, when the character stream includes characters in alphabetical form and characters in digital form, converting the characters in digital form into hexadecimal form, and then performing digital conversion together with the characters in alphabetical form; The digital characters are offset by using the offset to obtain the offset digital characters, and the code file composed of the offset digital characters is used as the encrypted file; Obtain a current timestamp, use the current timestamp to encrypt the offset to obtain an encrypted offset, and use the encrypted offset as a key; wherein a logical operation is performed on the offset and the current timestamp, and the logical operation is a sum logic or a difference logic; Determine the storage location of the code file so that the current timestamp, the key and the encrypted file are stored in the storage location together; wherein, the front end only retains the key, and when the front end needs to obtain the offset to decrypt the file, it needs to send the key to the back end, and the back end can calculate the offset after querying the corresponding timestamp and feed it back to the front end.
2. The method according to claim 1, characterized in that After obtaining the character stream in the code file, the method further includes: Scanning the character stream using lexical analysis to identify word sequences in the character stream according to word formation rules; Performing grammatical analysis on the word sequence to determine whether there is a grammatical error in the word sequence, and if so, determining a first error position; Performing a type inspection on the character stream by using semantic analysis to determine whether there is a semantic error in the structure of the character stream, and if so, determining a second error position; Correction information for the characters in the first error position and the second error position is received to obtain a corrected character stream.
3. The method according to claim 2, characterized in that Before the digital conversion of the characters in the character stream, the method further comprises: Using lexical analysis to identify numbers in the word sequence, and then in the syntax tree of the code, using the syntax analysis to remove numbers in variable positions; Perform base conversion on the filtered numbers to obtain the first base sequence.
4. The method according to claim 1, characterized in that: The method of using the offset to offset the numeric character to obtain the offset numeric character also includes: Performing base conversion on the offset and the digital character respectively to obtain a first binary sequence and a third base sequence; The third binary sequence is shifted using the second binary sequence to obtain a shifted third binary sequence.
5. The method according to claim 1, characterized in that The code file composed of the offset digital characters is used as an encrypted file, and further includes: The offset digital characters are converted into a base-4 sequence, and a code file composed of the base-4 sequence is used as an encrypted file.
6. The method according to claim 4, characterized in that The step of encrypting the offset by using the current timestamp to obtain the encrypted offset includes: Performing base conversion on the current timestamp to obtain a fifth base sequence; The fifth base-1 sequence is used to encrypt the first binary sequence to obtain an encrypted first binary sequence.
7. The method according to claim 1, characterized in that The storage location is a server; After storing the current timestamp, the key and the encrypted file together in the storage location, the method further includes: In response to a decryption operation on the encrypted file, the key is sent to a server, so that the key is decrypted on the server based on the current timestamp to obtain the offset; Receiving the offset fed back by the server, inputting the offset and the encrypted file into a restoration function, and obtaining the converted digital character; The converted numeric characters are subjected to reverse base conversion and combined to obtain the code file.
8. A code encryption device, characterized in that: include: A traversal module, used for traversing a code file and obtaining a character stream in the code file; a conversion module, configured to perform digital conversion on the characters in the character stream to obtain converted digital characters; wherein, when the character stream includes characters in alphabetical form and characters in digital form, the characters in digital form are converted into hexadecimal form, and then the characters in digital form are converted together with the characters in alphabetical form; An offset module is used to offset the numeric characters using an offset to obtain the offset numeric characters, and use a code file composed of the offset numeric characters as an encrypted file; An encryption module, used to obtain a current timestamp, encrypt the offset using the current timestamp to obtain an encrypted offset, and use the encrypted offset as a key; wherein a logical operation is performed on the offset and the current timestamp, and the logical operation is a sum logic or a difference logic; Determine the storage location of the code file so that the current timestamp, the key and the encrypted file are stored in the storage location together; wherein, the front end only retains the key, and when the front end needs to obtain the offset to decrypt the file, it needs to send the key to the back end, and the back end can calculate the offset after querying the corresponding timestamp and feed it back to the front end.
9. 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 7.
10. 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 7 is implemented.
Citation Information
Patent Citations
Method and device for protecting JavaScript codes
CN105205359A