Specific embodiment
The present invention is described in further detail with reference to the accompanying drawings and examples.It is understood that this place is retouchedThe specific embodiment stated is used only for explaining the present invention rather than limiting the invention.It also should be noted that in order to justIn description, only some but not all contents related to the present invention are shown in the drawings.
The embodiment of the invention provides a kind of program code guard methods, as shown in Figure 1, this method comprises:
101, obtaining needs program code to be protected and the key journey for encrypting the need program code to be protectedSequence code.
Wherein, the secret cipher key code program be used to carry out Hash calculation obtain encryption need program code to be protected encryption,Decruption key can may be the program code for not encrypting and (not needing to protect), this hair for the program code of encryptionBright embodiment is not limited this.The cipher key procedures code can be a program code, be also possible to multiple program codes,It is specific that it is not limited by the embodiments of the present invention.
102, Hash calculation is carried out to the cipher key procedures code according to pre-defined algorithm, obtains encryption key.
Wherein, the cipher key procedures code carry out Hash calculation pre-defined algorithm, can for algorithm include MD2, MD4, MD5 andAny number of in SHA-1, it is not limited by the embodiments of the present invention, and any existing Hash calculation method can answerWith in an embodiment of the present invention.And when being encrypted and decrypted, what is used can be the same algorithm, the encryption obtainedKey and decruption key be it is identical, that is, use symmetric encipherment algorithm;Rivest, shamir, adelman can certainly be used, is obtainedAlthough encryption key and decruption key are different, its pairing is used.
103, the need program code to be protected is encrypted to obtain encipheror code using the encryption key.
Program code guard method is stated below in conjunction with specific example, as shown in Fig. 2, one section of program code includes threeA function, the corresponding one section of program code block of each function, which is respectively A function, B function, C function, original programProgram code to be protected is B function in code block, and cipher key procedures code is unprotected A function and C function.According to predetermined calculationMethod carries out Hash calculation to A function and C function, obtains encryption key;B function is encrypted to obtain using the encryption keyEncipheror code.
Further, it after to needing program code to be protected to encrypt, in program code execution, needs to encryptionProgram code be first decrypted, obtain Original program code, execute again later.But the key journey due to calculating encryption keySequence code is likely located at before the encipheror code, it is also possible to below, so, in order to what is executed in program codeWhen, the decruption key of encipheror code is smoothly obtained, is needed to be protected to the need using the encryption keyWhile program code is encrypted to obtain encipheror code, setting obtains the address link of decruption key, to execute journeyWhen sequence code, the decruption key of the encipheror code is obtained.
Further, the setting based on address above mentioned link, when executing program code, when going to encipheror codeWhen, the present invention provides program code guard method, to realize the decryption of encipheror code, as shown in figure 3, this methodInclude:
201, the execution address chain, which is obtained, takes the cipher key procedures code.
202, Hash calculation is carried out to the cipher key procedures code according to pre-defined algorithm, obtains decruption key.
203, the encipheror code is decrypted using the decruption key;If successful decryption executes 204;If decryption is unsuccessful, 205 are executed.
Wherein, if successful decryption, illustrate that obtaining the relevant cipher key procedures code of decruption key was not tampered with;If decryptionIt is unsuccessful, illustrate that obtaining the relevant cipher key procedures code of decruption key had been tampered with.
204, the program code after decryption is executed.
205, the information alert that output program code is tampered.
Wherein, the embodiment of the present invention carry out program code be tampered information alert when, can by but be not limited toMode below realizes which is one kind or any number of combination of following form, which includes: text, sound, figureShape, video.
Based on the ciphering process of Fig. 2 description, when program code goes to encrypted B function, execute pre-setAddress chain, which is obtained, takes A function and C function, carries out Hash calculation to A function and C function according to pre-defined algorithm, obtains decruption key;It is decrypted using B function of the decruption key to encryption;If rogue program has distorted the code of A function or C function, pressingWhen carrying out Hash calculation to A function and C function according to pre-defined algorithm, obtained decruption key will be changed, then cannot normally decrypt BFunction ciphertext causes program that can not execute.So only when A and C code block number evidence are complete, when not distorted by rogue program,It just can normally execute B-code block.For example, in android system, DEX file will be obtained with the Hash calculation of SO fileEncryption key is encrypted, and is existed with ciphertext form, so DEX data can not distort;The Hash of SO file can be as decryption DEXThe key of file.So DEX cannot be decrypted normally if distorting SO file content, cause DEX that can not execute.
Based on the above method, the embodiment of the present invention also provides a kind of program code protective device, as shown in figure 4, the deviceInclude:
Module 31 is obtained, needs program code to be protected and for encrypting the need program generation to be protected for obtainingThe cipher key procedures code of code.Wherein, which needs program generation to be protected for carrying out Hash calculation acquisition encryptionThe encryption of code, decruption key can may be the program for not encrypting and (not needing to protect) for the program code of encryptionCode, it is not limited by the embodiments of the present invention.The cipher key procedures code can be a program code, be also possible to multipleProgram code, specific it is not limited by the embodiments of the present invention.
Hash calculation module 32 is added for carrying out Hash calculation to the cipher key procedures code according to pre-defined algorithmKey.Wherein, the pre-defined algorithm of Hash calculation is carried out to the cipher key procedures code, the embodiment of the present invention is to this without limitSystem, any existing Hash calculation method can be using in an embodiment of the present invention.And carrying out encryption reconciliationWhen close, what is used can be the same algorithm, the encryption key and decruption key obtained be it is identical, i.e., using symmetric cryptography calculateMethod;Rivest, shamir, adelman can certainly be used, although the encryption key and decruption key obtained is different, it is pairingIt uses.
Encrypting module 33 is added for being encrypted using the encryption key to the need program code to be protectedClose program code.
Further, it after to needing program code to be protected to encrypt, in program code execution, needs to encryptionProgram code be first decrypted, obtain Original program code, execute again later.But the key journey due to calculating encryption keySequence code is likely located at before the encipheror code, it is also possible to below, so, in order to what is executed in program codeWhen, the decruption key of encipheror code is smoothly obtained, is needed to be protected to the need using the encryption keyWhile program code is encrypted to obtain encipheror code, it is also necessary to which setting obtains the instruction of cipher key procedures code, such as Fig. 5It is shown, the device further include:
Setup module 34, for utilizing the encryption key to the need program generation to be protected in the encrypting module 33While code is encrypted to obtain encipheror code, setting obtains the address link of decruption key, to execute program codeWhen, obtain the decruption key of the encipheror code.
Further, as shown in fig. 6, the device further include:
Instruction execution module 35, for executing the address when program code is gone at the encipheror codeLink obtains the cipher key procedures code.
The Hash calculation module 32 is also used to, and carries out Hash calculation to the cipher key procedures code according to pre-defined algorithm,Obtain decruption key.
Deciphering module 36, for the encipheror code to be decrypted using the decruption key.
Program code execution module 37 is used for when the deciphering module 36 is to encipheror code decryption success,Program code after executing decryption.
Further, as shown in fig. 7, the device further include:
Message output module 38 is used for when the deciphering module 36 is unsuccessful to the encipheror code decryption, defeatedThe information alert that program code is tampered out.Wherein, the embodiment of the present invention carry out program code be tampered information alert when,Can by but be not limited to mode below and realize, which is one kind or any number of combination of following form, the formIt include: text, sound, figure, video.
The program code guard method and device that the embodiment of the present invention proposes are protected to need program code to be protectedWhen shield, encryption, decruption key are obtained by carrying out Hash calculation to other program codes, that is, devise a kind of pair of objectDependence as key encrypted object, then any one link is tampered in dependence, can not all be decrypted originalObject needs program code to be protected not to be tampered so can be good at protection.
Also, the embodiment of the present invention can also detect well whether program code is tampered, can be defeated if having been tampered withPrompt information out informs that user knows that plant maintenance person is timely handled.
The above description is merely a specific embodiment, but scope of protection of the present invention is not limited thereto, anyThose familiar with the art in the technical scope disclosed by the present invention, can easily think of the change or the replacement, and should all containLid is within protection scope of the present invention.Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.