A method and apparatus for protecting R language source code
By encoding the input parameters and each line of code of the R language source code and encapsulating them into R6 class objects, the problem of exposure of the R language source code is solved and the security during transmission and operation is improved.
Patent Information
- Application Number
- CN202310467101.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-04-26
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2043-04-26
AI Technical Summary
Due to its interpreted nature, R language source code is easily exposed during use, making it difficult for developers to effectively protect the source code.
By encoding the input parameters and each line of code in the R language source code into multiple variables, and storing them in the encoding file, using R6 class objects for encapsulation, it avoids directly exposing the source code content and improves the transmission and operation process. security.
It effectively avoids the exposure of source code content during transmission and operation, improves security, and at the same time retains the operating logic and method names of the source code to prevent others from stealing the code logic.
Smart Images

Figure CN116522295B
Abstract
Description
Technical Field
[0001] The present application belongs to the field of computer technology, and specifically relates to a method and device for protecting R language source code. Background Art
[0002] R is a computer language. R programs written in R are usually used as desktop programs by data analysts to complete data processing, data analysis, data visualization, etc. R itself is a complete software system, including both an R language interpreter and a complete graphical user interface.
[0003] Since R is an interpreted programming language, the R interpreter requires the R source code to execute. Therefore, when using R programs, the program source code will inevitably be exposed, making it impossible for developers to effectively protect the source code.
[0004] Application Contents
[0005] The purpose of the embodiments of the present application is to provide a method and device for protecting R language source code, so as to solve the defect that the R language source code in the prior art is easily exposed.
[0006] In order to solve the above technical problems, this application is implemented as follows:
[0007] In a first aspect, a method for protecting R language source code is provided, comprising the following steps:
[0008] Encoding the input parameters in the R language source code into multiple variables, and storing the multiple variables in an input parameter encoding file;
[0009] Encode each line of code in the R language source code into parameters, code symbols and expressions respectively, and store the encoded multiple variables without context into a source code encoding file;
[0010] The input parameter encoding file and the source code encoding file are transmitted or executed.
[0011] In a second aspect, a device for protecting R language source code is provided, comprising:
[0012] A first encoding module, used for encoding the input parameters in the R language source code into a plurality of variables, and storing the plurality of variables in an input parameter encoding file;
[0013] A second encoding module is used to encode each line of code in the R language source code into parameters, code symbols and expressions, and store the encoded multiple variables without context into a source code encoding file;
[0014] A processing module is used to transmit or run the input parameter encoding file and the source code encoding file.
[0015] The embodiment of the present application can prevent the content of the source code from being exposed and improve the security of the source code during transmission or operation by encoding the input parameters and each line of code in the R language source code respectively and transmitting or running the encoded file. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 This is a flow chart of a method for protecting R language source code provided by an embodiment of the present application;
[0017] Figure 2 It is a specific implementation diagram of the method for protecting R language source code provided in an embodiment of the present application;
[0018] Figure 3 is a schematic diagram of the source code provided in the embodiment of the present application;
[0019] Figure 4 is a schematic diagram of the encoding result corresponding to the source code provided in the embodiment of the present application;
[0020] Figure 5 It is a structural diagram of a device for protecting R language source code provided in an embodiment of the present application. DETAILED DESCRIPTION
[0021] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.
[0022] The present application embodiment provides a method for protecting the R program source code from being exposed during transmission and operation, which mainly includes the following three aspects:
[0023] 1. Input parameter encoding: The input parameters required for the source code to run are encoded into multiple variables and stored in the input parameter encoding file;
[0024] 2. Source code encoding: read the source code, and according to certain encoding rules, encode each line of source code into three parts: parameter name, code symbol, and expression. After processing, multiple simple variables will be obtained, which contain all the information of the encoded source code, and then the variables will be stored in the source code encoding file;
[0025] 3. Decoding of the encoded file: Read the input parameter encoding file and decode it to get the input parameters. Read the source code encoding file, decode and encapsulate it into an R6 class (R6 is a data structure of object-oriented programming in R language. It is a static class that needs to be instantiated before it can be loaded and run by memory; an R6 class object consists of a class name, public methods and attributes, private methods and attributes, where public methods and attributes are also called public members and can be called outside the R6 class; private methods and attributes are called private members and can only be called within the R6 class) object, then instantiate the R6 object and run the program. The decoding process does not restore the original code, but generates a new encapsulated R6 object that can execute the same functions as the original code.
[0026] In order to achieve the above purpose, the present application provides the following solutions:
[0027] 1. This method provides basic coding rules, matching with clear and unique definition symbols in the R language writing process to identify the components of the line of statements, and thus divide it into three parts: parameter name, code symbol, and expression. In the coding process, the code symbols can be replaced with some keywords, such as the definition symbol '<-' can be written as 'assignment_symbol' in the coding file. Secondly, because the context of the code is related, there will also be a subordinate relationship in the expression part of the coding file (for example, when defining a function, 'tmp<-function(){}', the processing logic in the function will be defined in '{}', then the statements in '{}' are subordinate to the tmp function, so when coding, the variables encoded by the statements in '{}' will be subordinate to the 'tmp' variable).
[0028] 2. The decoding rules of this method provide encapsulation function. If the source code is run directly, the variables and functions in the code will be displayed in the running environment. Therefore, in order to solve this problem, during the decoding process, the decoding result is directly encapsulated into an R6 class.
[0029] 3. This method provides basic decoding rules, and decodes according to the decoding rules in the method and generates an R6 class structure. When generating the R6 class structure, all functions are written into the methods of the private members of the R6 class, and the rest are written into the methods of the public members of the R6 class (the specific distinction method is described in 6.3.1 in the detailed processing of Part 8 below).
[0030] 4. The above steps are the core process of this method. By calling step 1, the source code is encoded into a source code encoding file, and the input parameters are encoded into an input parameter encoding file, and then two encoding files are obtained, and then the two encoding files can be transmitted or run. When running, by calling step 3, the encoding file is decoded into the R6 class, and then the R6 class is instantiated and the R6 class object is called to run the source code.
[0031] In the following, in conjunction with the accompanying drawings, a method for protecting R language source code provided by an embodiment of the present application is described in detail through specific embodiments and application scenarios.
[0032] like Figure 1 As shown, it is a flow chart of a method for protecting R language source code provided by an embodiment of the present application, and the method comprises the following steps:
[0033] Step 101: Encode the input parameters in the R language source code into multiple variables, and store the multiple variables in an input parameter encoding file.
[0034] Step 102, encode each line of code in the R language source code into parameters, code symbols and expressions respectively, and store the encoded multiple variables without context into a source code encoding file.
[0035] Specifically, it is possible to match the clear and unique definition symbols in the R language writing process, identify the components of each line of code, and divide each line of code into three parts: parameters, code symbols, and expressions.
[0036] Step 103: Transmit or run the input parameter encoding file and the source code encoding file.
[0037] Specifically, the input parameter encoding file may be decoded to obtain input parameters; the source code encoding file may be decoded and encapsulated into an R6 class object; the R6 class object may be instantiated, and the R6 class object may be loaded and run.
[0038] In this embodiment, an R6 class object may be generated, the input parameter encoding file may be decoded, the decoded input parameters may be used as attributes of public members in the R6 class object, and an initialization function may be written.
[0039] Furthermore, the source code encoding file is read, all variables in the source code encoding file are traversed, all input parameters and function names are recorded, and the source code encoding file is decoded according to the encoding rules of parameters, code symbols and expressions; the public member part obtained by decoding is written into the public member of the R6 class object as the processing logic and startup entry of the entire source code; the private member part obtained by decoding is written into the private member of the R6 class object as the core processing function, and the functions and input variables of the private member part involved in the public member part are processed into the calling format of the R6 class.
[0040] The embodiment of the present application can prevent the content of the source code from being exposed and improve the security of the source code during transmission or operation by encoding the input parameters and each line of code in the R language source code respectively and transmitting or running the encoded file.
[0041] In the embodiment of the present application, after the source code is encoded and before it is decoded, the result generated needs to be stored and transmitted in the form of an RDS file (RDS file is a file format that uses binary to store and read in R language, and its main advantage is that it can store variables in any form in R, and its storage and reading speed is faster than other files). At the same time, the RDS file needs to be read into the memory to view the content in the file, which more effectively protects the encoding result.
[0042] The present application embodiment mainly includes two data processing and analysis methods, such as Figure 2 As shown, one is an encoding method, whose purpose is to encode the source code, that is, to scramble the source code according to certain rules; the other is a decoding method, whose purpose is to decode the source code encoding file and encapsulate it into an R6 class object.
[0043] Before coding, you need to know what the input parameters are in the entire source code and what the source code body is, such as Figure 3 The coded method function is stored in the .RData file (RData is a file exclusive to the R language that can save massive data and is used to store environment variables in this method) in the form of variables, so the environment needs to be loaded when used.
[0044] Specifically, the encoding process includes: calling the parameter encoding function, passing the sorted input parameter names in the form of vectors, and after processing, multiple simple variables will be obtained, which will then be stored in the input parameter encoding file, and the input parameter part will be processed; then, calling the code encoding function, passing the file path as a parameter to the function, and the function will encode according to the encoding rules. During the encoding process, each line of code is parsed into three variables: parameters, code symbols, and expressions, such as Figure 4As shown in the figure, the encoding process will adjust the variable storage level according to the actual situation of the code. After the encoding is completed, the corresponding source code encoding file will be generated in the working directory, which can be used as the source code encoding file. The entire encoding process is to parse the source code into multiple variables without context through encoding rules, disrupt the source code, and make it impossible for users to see the writing logic of the source code. In order to make the encoded source code run, a decoding function is also required.
[0045] Correspondingly, the decoding process includes: calling the decoding function, loading the encoded source code encoding file and the input parameter encoding file, then generating the R6 class structure, decoding the input parameter encoding file, decoding the input parameters into the attributes (parameters) of the public members in the R6 class, and writing the initialization function at the same time. At this point, the input parameter part is decoded.
[0046] In the source code decoding part, first read the source code encoding file to get all the variables in the encoding file, then traverse all the variables, record all the input parameters and function names, and then start decoding according to the parameters, code symbols, and expression encoding rules.
[0047] During the decoding process, in order to encapsulate the result into R6 class while decoding, the following processing needs to be performed:
[0048] First, in order to encapsulate the code into an R6 class object, it is necessary to clearly define which members are public and which are private. Figure 3 In the source code, lines 1, 11, and 12 of the source code represent the code running order and process, so they can be used as public member content; lines 2-10 of the source code are a function definition, in which the processing logic written belongs to the core part, and the function will only be executed when it is called. Its position has little effect on the overall code running, so it can be used as private member content.
[0049] After the distinction is completed, the functions of the private member part involved in the public member part and the input variables need to be processed. All input parameters and function names recorded in the process of traversing all variables in the public member part need to be processed into the calling format of the R6 class (when the R6 class calls the private member method, it is necessary to add 'private$' before the function name). After the processing is completed, the public member part is written into the public member of the R6 class as the processing logic and startup entry of the entire source code, and the private member part is written into the private member of the R6 class as the core processing function, and external calls are not allowed. Then load the R6 class, then instantiate it, run it, and finally get the result.
[0050] At this point, the encoding and decoding process is complete. During the entire processing process, all the intermediate data generated are stored in the memory in the form of variables. And when decoding, decoding and running are continuous. After decoding is completed, the encapsulated instantiation object will be run to obtain the running result, and then the program will end. At the end, the R6 class objects generated during the program running process will also be deleted.
[0051] The encoding function provided in the embodiment of the present application can encode the source code, and a coded file can be obtained after encoding. The coded file can retain the operating logic and method name of the source code, and can also prevent others from seeing the content of the source code. Even if a user has viewed the content in the coded file through certain technical means, he will only see multiple variables composed of parameter names, symbols and expressions, and it is also impossible to obtain the operating logic of the entire code. Further, the decoding function provided in the embodiment of the present application can decode the code in the coded file, and does not restore the original code, but encapsulates it into an R6 class object, which can also hide the function function well during operation. At the same time, after instantiation, the R6 object will only be generated in the operating environment and will be automatically destroyed after the operation ends.
[0052] like Figure 5 As shown, it is a schematic diagram of the structure of a device for protecting R language source code provided in an embodiment of the present application, including:
[0053] The first encoding module 510 is used to encode the input parameters in the R language source code into multiple variables, and store the multiple variables in an input parameter encoding file.
[0054] The second encoding module 520 is used to encode each line of code in the R language source code into parameters, code symbols and expressions, and store the encoded multiple variables without context into the source code encoding file.
[0055] Specifically, the second encoding module 520 is specifically used to match the clear and unique definition symbols in the R language writing process, identify the components of each line of code, and divide each line of code into three parts: parameters, code symbols and expressions.
[0056] The processing module 530 is used to transmit or run the input parameter encoding file and the source code encoding file.
[0057] The processing module 530 specifically includes:
[0058] A first decoding submodule, used for decoding the input parameter encoding file to obtain input parameters;
[0059] Specifically, the first decoding submodule is specifically used to generate an R6 class object, decode the input parameter encoding file, use the decoded input parameters as attributes of public members in the R6 class object, and write an initialization function.
[0060] A second decoding submodule is used to decode the source code file and encapsulate it into an R6 class object;
[0061] Specifically, the second decoding submodule is specifically used to read the source code encoding file, traverse all variables in the source code encoding file, record all input parameters and function names, and decode the source code encoding file according to the encoding rules of parameters, code symbols and expressions; write the decoded public member part into the public member of the R6 class object as the processing logic and startup entry of the entire source code; write the decoded private member part into the private member of the R6 class object as the core processing function, and process the functions and input variables of the private member part involved in the public member part into the calling format of the R6 class.
[0062] The running submodule 533 is used to instantiate the R6 class object and load and run the R6 class object.
[0063] The embodiment of the present application can prevent the content of the source code from being exposed and improve the security of the source code during transmission or operation by encoding the input parameters and each line of code in the R language source code respectively and transmitting or running the encoded file.
[0064] The embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, each process of the above-mentioned method embodiment for protecting R language source code is implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. The computer-readable storage medium is, for example, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.
[0065] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0066] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0067] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.
Claims
1. A method for protecting R language source code, characterized in that: The following steps are involved: Encoding the input parameters in the R language source code into multiple variables, and storing the multiple variables in an input parameter encoding file; Encode each line of code in the R language source code into parameters, code symbols and expressions respectively, and store the encoded multiple variables without context into a source code encoding file; Transmitting or running the input parameter encoding file and the source code encoding file; Running the input parameter encoding file and the source code encoding file specifically includes: Decoding the input parameter encoding file to obtain input parameters; Decode the source code file and encapsulate it into an R6 class object; Instantiate the R6 class object, and load and run the R6 class object; Decoding and encapsulating the source code file into an R6 class object specifically includes: Read the source code encoding file, traverse all variables in the source code encoding file, record all input parameters and function names, and decode the source code encoding file according to the encoding rules of parameters, code symbols and expressions; The public member part obtained by decoding is written into the public member of the R6 class object as the processing logic and startup entry of the entire source code; the private member part obtained by decoding is written into the private member of the R6 class object as the core processing function, and the functions and input variables of the private member part involved in the public member part are processed into the calling format of the R6 class.
2. The method according to claim 1, characterized in that Decoding the input parameter encoding file to obtain the input parameters specifically includes: Generate an R6 class object, decode the input parameter encoding file, use the decoded input parameters as attributes of public members in the R6 class object, and write an initialization function.
3. The method according to claim 1, characterized in that The encoding of each line of code in the R language source code into parameters, code symbols and expressions respectively includes: Match the clear and unique definition symbols in the R language writing process, identify the components of each line of code, and divide each line of code into three parts: parameters, code symbols and expressions.
4. A device for protecting R language source code, characterized in that: include: A first encoding module, used for encoding the input parameters in the R language source code into a plurality of variables, and storing the plurality of variables in an input parameter encoding file; A second encoding module is used to encode each line of code in the R language source code into parameters, code symbols and expressions, and store the encoded multiple variables without context into a source code encoding file; A processing module, used for transmitting or running the input parameter encoding file and the source code encoding file; The processing module specifically includes: A first decoding submodule, used for decoding the input parameter encoding file to obtain input parameters; A second decoding submodule is used to decode the source code file and encapsulate it into an R6 class object; A running submodule is used to instantiate the R6 class object and load and run the R6 class object; The second decoding submodule is specifically used to read the source code encoding file, traverse all variables in the source code encoding file, record all input parameters and function names, and decode the source code encoding file according to the encoding rules of parameters, code symbols and expressions; write the decoded public member part into the public member of the R6 class object as the processing logic and startup entry of the entire source code; write the decoded private member part into the private member of the R6 class object as the core processing function, and process the functions and input variables of the private member part involved in the public member part into the calling format of the R6 class.
5. The device according to claim 4, characterized in that The first decoding submodule is specifically used to generate an R6 class object, decode the input parameter encoding file, use the decoded input parameters as attributes of public members in the R6 class object, and write an initialization function.
6. The device according to claim 4, characterized in that The second encoding module is specifically used to match the clear and unique definition symbols in the R language writing process, identify the components of each line of code, and divide each line of code into three parts: parameters, code symbols and expressions.
