Image algorithm model encryption method and device
By converting the model dictionary of the image algorithm model by byte streaming and encrypting it using AES encryption algorithm and Fernet model, the problem of low security in industrial applications is solved, and the security encryption and confidentiality of the model is achieved.
Patent Information
- Application Number
- CN202411764919.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-04
- Publication Date
- 2025-03-25
AI Technical Summary
The lack of effective model encryption methods in the prior art has led to security and confidentiality problems in deep learning models in industrial applications.
By obtaining the model dictionary of the image algorithm model to be encrypted, byte stream conversion is performed, and a key is generated based on the AES encryption algorithm, the byte stream is encrypted using the Fernet encryption model to obtain encrypted data.
The encryption of the image algorithm model is realized to prevent the model from being tampered with or attacked, and the security of the image algorithm model is provided, ensuring the confidentiality of the data.
Smart Images

Figure CN119696766A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of model encryption technology, and in particular to an image algorithm model encryption method and device. Background Art
[0002] In the industrial field, the application of deep learning has become an important means to improve production efficiency and product quality. However, the large amount of data and the complex and diverse characteristics of industrial scenarios make the model training process challenging, requiring an efficient, stable and easy-to-operate training platform. At the same time, with the popularity of deep learning models in industrial applications, the security and confidentiality of the models have become increasingly prominent. Unauthorized access, theft or tampering of models may lead to serious economic losses and technical leaks. Currently, there is no technology for encrypting models. Therefore, an effective model encryption method is needed to protect the security of deep learning models.
[0003] Therefore, there is an urgent need for an image algorithm model encryption strategy to solve the problem of low security of image models. Summary of the invention
[0004] The embodiment of the present invention provides an image algorithm model encryption method and device to solve the problem of low security of the image model.
[0005] In order to solve the above problem, an embodiment of the present invention provides an image algorithm model encryption method, including:
[0006] Obtain the model dictionary of the image algorithm model to be encrypted;
[0007] Convert the model dictionary to obtain a byte stream;
[0008] Generate AES key based on AES encryption algorithm;
[0009] Based on the AES key, a Fernet encryption model is determined, so that the Fernet encryption model encrypts the byte stream to obtain encrypted data.
[0010] As an improvement of the above solution, the step of obtaining a model dictionary of the algorithm model of the image to be encrypted includes:
[0011] Retrieve model data of the image algorithm model to be encrypted;
[0012] The data type, data content and metadata in the model data are identified to obtain a model dictionary of the image algorithm model to be encrypted.
[0013] As an improvement of the above solution, the converting the model dictionary to obtain a byte stream includes: serializing the model dictionary through a preset function of Python software to obtain a byte stream.
[0014] As an improvement of the above solution, the Fernet encryption model encrypts the byte stream to obtain encrypted data, including:
[0015] The Fernet encryption model divides the byte stream into blocks to obtain a plurality of byte stream sub-regions;
[0016] The Fernet encryption model fills each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region;
[0017] The Fernet encryption model performs encryption operation on each byte stream sub-filling area based on the AES key to obtain encrypted data.
[0018] As an improvement of the above solution, the method of filling each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region includes:
[0019] Based on the PKCS7 algorithm, determine the padding data corresponding to each byte stream sub-area;
[0020] In each byte stream sub-region, padding data is added to the end of the data included in the byte stream sub-region to obtain a byte stream sub-padding region.
[0021] Accordingly, an embodiment of the present invention further provides an image algorithm model encryption device, comprising: a data acquisition module, a data conversion module, an encryption module and a result generation module;
[0022] The data acquisition module is used to obtain a model dictionary of the image algorithm model to be encrypted;
[0023] The data conversion module is used to convert the model dictionary to obtain a byte stream;
[0024] The encryption module is used to generate an AES key based on the AES encryption algorithm;
[0025] The result generating module is used to determine the Fernet encryption model based on the AES key, so that the Fernet encryption model encrypts the byte stream to obtain encrypted data.
[0026] As an improvement of the above solution, the data acquisition module includes: a retrieval unit and a recognition unit;
[0027] The retrieving unit is used to retrieve the model data of the image algorithm model to be encrypted;
[0028] The identification unit is used to identify the data type, data content and metadata in the model data to obtain the model dictionary of the image algorithm model to be encrypted.
[0029] As an improvement of the above solution, the data conversion module includes: serializing the model dictionary through a preset function of Python software to obtain a byte stream.
[0030] As an improvement of the above solution, the Fernet encryption model encrypts the byte stream to obtain encrypted data, including:
[0031] The Fernet encryption model divides the byte stream into blocks to obtain a plurality of byte stream sub-regions;
[0032] The Fernet encryption model fills each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region;
[0033] The Fernet encryption model performs encryption operation on each byte stream sub-filling area based on the AES key to obtain encrypted data.
[0034] As an improvement of the above solution, the method of filling each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region includes:
[0035] Based on the PKCS7 algorithm, determine the padding data corresponding to each byte stream sub-area;
[0036] In each byte stream sub-region, padding data is added to the end of the data included in the byte stream sub-region to obtain a byte stream sub-padding region.
[0037] As can be seen from the above, the present invention has the following beneficial effects:
[0038] The present invention provides an image algorithm model encryption method, including: obtaining a model dictionary of an image algorithm model to be encrypted; converting the model dictionary to obtain a byte stream; generating an AES key based on an AES encryption algorithm; determining a Fernet encryption model based on the AES key, so that the Fernet encryption model encrypts the byte stream to obtain encrypted data, and by converting the model dictionary into a byte stream, encryption and secure transmission can be performed more easily. By encrypting the byte stream, encryption of the image algorithm model is achieved, so that the user needs to decrypt the image algorithm model before using the image algorithm model, thereby preventing the image algorithm model from being tampered with or attacked, and providing security for the image algorithm model. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 It is a flowchart of an image algorithm model encryption method provided by an embodiment of the present invention;
[0040] Figure 2 It is a structural schematic diagram of an image algorithm model encryption device provided by an embodiment of the present invention;
[0041] Figure 3 It is a schematic diagram of the structure of a terminal device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0042] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0043] Embodiment 1
[0044] See also Figure 1 , Figure 1 FIG. 1 is a flow chart of an image algorithm model encryption method provided by an embodiment of the present invention. Figure 1 As shown, this embodiment includes steps 101 to 104, and each step is specifically as follows:
[0045] Step 101: Obtain a model dictionary of the image algorithm model to be encrypted.
[0046] In this embodiment, the step of obtaining a model dictionary of the algorithm model of the image to be encrypted includes:
[0047] Retrieve model data of the image algorithm model to be encrypted;
[0048] The data type, data content and metadata in the model data are identified to obtain a model dictionary of the image algorithm model to be encrypted.
[0049] In a specific embodiment, the deep learning image algorithm model dictionary is a data structure for storing model parameters and structures, usually in the form of a Python dictionary. The model dictionary has a specific key-value pair structure, where the key is the parameter name and the value is the corresponding parameter data. The computer can automatically identify the deep learning image algorithm model dictionary through the following features: Data type: The key of the model dictionary is usually a string, and the value is a numerical array, tensor or other nested dictionary. Data content: The content of the model dictionary conforms to the model format standard of a specific deep learning framework (such as PyTorch or TensorFlow). Metadata: The model dictionary contains specific metadata, such as model structure information, training configuration, etc.
[0050] Step 102: transform the model dictionary to obtain a byte stream.
[0051] In this embodiment, converting the model dictionary to obtain a byte stream includes: serializing the model dictionary through a preset function of Python software to obtain a byte stream.
[0052] In a specific embodiment, the model dictionary is saved into a byte stream in memory using Python's torch.save function. The torch.save function is an existing technology for serializing and saving PyTorch models, and can be replaced by other functions that can save model data as a byte stream, such as pickle.dump.
[0053] Step 103: Generate an AES key based on the AES encryption algorithm.
[0054] In a specific embodiment, the AES algorithm is specifically:
[0055] Generate an AES key and use it to initialize the Fernet object;
[0056] Use the encrypt method of the Fernet object to encrypt the byte stream. The encryption process includes dividing the byte stream into blocks and performing encryption operations on each block of data with the key to finally obtain the encrypted byte stream.
[0057] In a specific embodiment, AES (Advanced Encryption Standard) is a symmetric encryption algorithm widely used for data encryption and protection. The security of the AES algorithm comes from its complex key scheduling and encryption process. Fernet is an advanced symmetric encryption scheme based on the AES algorithm and provides an easy-to-use interface for data encryption and decryption.
[0058] It should be noted that the key is a key parameter in the encryption and decryption process. AES keys can be generated using a cryptographically secure random number generator and stored and transmitted in a secure manner. In practical applications, a key management system (KMS) can be used to manage key generation, distribution, and storage.
[0059] Step 104: Based on the AES key, determine a Fernet encryption model so that the Fernet encryption model encrypts the byte stream to obtain encrypted data.
[0060] In this embodiment, the Fernet encryption model encrypts the byte stream to obtain encrypted data, including:
[0061] The Fernet encryption model divides the byte stream into blocks to obtain a plurality of byte stream sub-regions;
[0062] The Fernet encryption model fills each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region;
[0063] The Fernet encryption model performs encryption operation on each byte stream sub-filling area based on the AES key to obtain encrypted data.
[0064] In this embodiment, filling each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region includes:
[0065] Based on the PKCS7 algorithm, determine the padding data corresponding to each byte stream sub-area;
[0066] In each byte stream sub-region, padding data is added to the end of the data included in the byte stream sub-region to obtain a byte stream sub-padding region.
[0067] In a specific embodiment, in order to make the length of the plaintext data meet the block size requirement of AES encryption, the data needs to be padded. PKCS7 (i.e., the preset padding scheme described in the present invention) is a commonly used padding scheme that can ensure that the data length is an integer multiple of the block size. During the encryption process, the padded data will be attached to the end of the plaintext data, and the padded data will be automatically removed during decryption to restore the original plaintext.
[0068] In a specific embodiment, the encrypted byte stream is saved to a specified path. The byte stream can be written into a file using a file I / O operation.
[0069] For better explanation, the following code is provided for image algorithm model encryption:
[0070] def encrypt_models_dict(dict,save_dir,key):
[0071] b = io.BytesIO()
[0072] torch.save(dict,b)
[0073] data_bytes = b.getvalue()
[0074] encrypted_data=Fernet(key).encrypt(data_bytes)
[0075] b=io.BytesIO(encrypted_data)
[0076] torch.save(b,save_dir)
[0077] In a specific embodiment, the steps of decrypting the image algorithm model are specifically as follows:
[0078] (1) Use Python's torch.load function to load the encrypted model file. The torch.load function is an existing technology for deserializing and loading PyTorch models and can be replaced by other functions that can load byte streams, such as pickle.load;
[0079] (2) Use the Fernet object in the AES algorithm and the provided key to decrypt the encrypted byte stream. The specific steps include: using the AES key to initialize the Fernet object; using the decrypt method of the Fernet object to decrypt the encrypted byte stream. The decryption process includes dividing the byte stream into blocks and performing a decryption operation on each block of data with the key to finally obtain the decrypted byte stream;
[0080] (3) Convert byte stream to model dictionary: Convert the decrypted byte stream to model dictionary and return it. You can use file I / O operations to read the byte stream into memory and use the torch.load function to deserialize it.
[0081] For better explanation, the following code is provided to illustrate the decryption of the image algorithm model:
[0082] def decrypt_models(model_path,key):
[0083] b=torch.load(model_path,map_location=torch.device('cpu'))
[0084] b.seek(0)
[0085] decrypted_data = b.getvalue()
[0086] decrypted_data=Fernet(key).decrypt(decrypted_data)
[0087] b=io.BytesIO(decrypted_data)
[0088] b.seek(0)
[0089] x=torch.load(b,map_location=torch.device('cpu'))
[0090] return x
[0091] See also Figure 2 , Figure 2 It is a structural diagram of an image algorithm model encryption device provided by an embodiment of the present invention, comprising: a data acquisition module 201, a data conversion module 202, an encryption module 203 and a result generation module 204;
[0092] The data acquisition module is used to obtain a model dictionary of the image algorithm model to be encrypted;
[0093] The data conversion module is used to convert the model dictionary to obtain a byte stream;
[0094] The encryption module is used to generate an AES key based on the AES encryption algorithm;
[0095] The result generating module is used to determine the Fernet encryption model based on the AES key, so that the Fernet encryption model encrypts the byte stream to obtain encrypted data.
[0096] As an improvement of the above solution, the data acquisition module includes: a retrieval unit and a recognition unit;
[0097] The retrieving unit is used to retrieve the model data of the image algorithm model to be encrypted;
[0098] The identification unit is used to identify the data type, data content and metadata in the model data to obtain the model dictionary of the image algorithm model to be encrypted.
[0099] As an improvement of the above solution, the data conversion module includes: serializing the model dictionary through a preset function of Python software to obtain a byte stream.
[0100] As an improvement of the above solution, the Fernet encryption model encrypts the byte stream to obtain encrypted data, including:
[0101] The Fernet encryption model divides the byte stream into blocks to obtain a plurality of byte stream sub-regions;
[0102] The Fernet encryption model fills each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region;
[0103] The Fernet encryption model performs encryption operation on each byte stream sub-filling area based on the AES key to obtain encrypted data.
[0104] As an improvement of the above solution, the method of filling each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region includes:
[0105] Based on the PKCS7 algorithm, determine the padding data corresponding to each byte stream sub-area;
[0106] In each byte stream sub-region, padding data is added to the end of the data included in the byte stream sub-region to obtain a byte stream sub-padding region.
[0107] This embodiment obtains the model dictionary of the image algorithm model to be encrypted; converts the model dictionary to obtain a byte stream; generates an AES key based on the AES encryption algorithm; determines the Fernet encryption model based on the AES key, so that the Fernet encryption model encrypts the byte stream to obtain encrypted data, and by converting the model dictionary into a byte stream, encryption and secure transmission can be performed more easily. By encrypting the byte stream, the encryption of the image algorithm model is achieved, so that the user needs to decrypt the image algorithm model before using the image algorithm model, thereby preventing the image algorithm model from being tampered with or attacked, and providing the security of the image algorithm model.
[0108] Embodiment 2
[0109] See also Figure 3 , Figure 3 It is a schematic diagram of the structure of a terminal device provided in one embodiment of the present invention.
[0110] A terminal device of this embodiment includes: a processor 301, a memory 302, and a computer program stored in the memory 302 and executable on the processor 301. When the processor 301 executes the computer program, the steps of the above-mentioned image algorithm model encryption method in the embodiment are implemented, for example: Figure 1 Alternatively, when the processor executes the computer program, the functions of each module in the above-mentioned device embodiments are realized, for example: Figure 2 The image algorithm model shown encrypts all modules of the device.
[0111] In addition, an embodiment of the present invention also provides a computer-readable storage medium, which includes a stored computer program, wherein when the computer program is running, the device where the computer-readable storage medium is located is controlled to execute the image algorithm model encryption method described in any of the above embodiments.
[0112] Those skilled in the art will understand that the schematic diagram is merely an example of a terminal device and does not constitute a limitation on the terminal device. The terminal device may include more or fewer components than shown in the diagram, or a combination of certain components, or different components. For example, the terminal device may also include input and output devices, network access devices, buses, etc.
[0113] The processor 301 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc. The processor 301 is the control center of the terminal device, and uses various interfaces and lines to connect various parts of the entire terminal device.
[0114] The memory 302 can be used to store the computer program and / or module. The processor 301 implements various functions of the terminal device by running or executing the computer program and / or module stored in the memory and calling the data stored in the memory 302. The memory 302 can mainly include a program storage area and a data storage area, wherein the program storage area can store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area can store data created according to the use of the mobile phone (such as audio data, a phone book, etc.), etc. In addition, the memory can include a high-speed random access memory, and can also include a non-volatile memory, such as a hard disk, a memory, a plug-in hard disk, a smart memory card (Smart Media Card, SMC), a secure digital (Secure Digital, SD) card, a flash card (Flash Card), at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0115] Wherein, if the module / unit integrated in the terminal device is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on such an understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Wherein, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form, etc. The computer-readable medium may include: any entity or device capable of carrying the computer program code, recording medium, U disk, mobile hard disk, magnetic disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium, etc.
[0116] It should be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the accompanying drawings of the device embodiments provided by the present invention, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines. A person of ordinary skill in the art may understand and implement it without paying any creative effort.
[0117] The above is a preferred embodiment of the present invention. It should be pointed out that a person skilled in the art can make several improvements and modifications without departing from the principle of the present invention. These improvements and modifications are also considered to be within the scope of protection of the present invention.
Claims
1. An image algorithm model encryption method, characterized in that: include: Obtain the model dictionary of the image algorithm model to be encrypted; Convert the model dictionary to obtain a byte stream; Generate AES key based on AES encryption algorithm; Based on the AES key, a Fernet encryption model is determined, so that the Fernet encryption model encrypts the byte stream to obtain encrypted data.
2. The image algorithm model encryption method according to claim 1, characterized in that: The step of obtaining a model dictionary of an algorithm model of an image to be encrypted includes: Retrieve model data of the image algorithm model to be encrypted; The data type, data content and metadata in the model data are identified to obtain a model dictionary of the image algorithm model to be encrypted.
3. The image algorithm model encryption method according to claim 2, characterized in that: The converting the model dictionary to obtain a byte stream includes: serializing the model dictionary through a preset function of Python software to obtain a byte stream.
4. The image algorithm model encryption method according to claim 3, characterized in that: The Fernet encryption model encrypts the byte stream to obtain encrypted data, including: The Fernet encryption model divides the byte stream into blocks to obtain a plurality of byte stream sub-regions; The Fernet encryption model fills each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region; The Fernet encryption model performs encryption operation on each byte stream sub-filling area based on the AES key to obtain encrypted data.
5. The image algorithm model encryption method according to claim 4, characterized in that: The step of filling each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region includes: Based on the PKCS7 algorithm, determine the padding data corresponding to each byte stream sub-area; In each byte stream sub-region, padding data is added to the end of the data included in the byte stream sub-region to obtain a byte stream sub-padding region.
6. An image algorithm model encryption device, characterized in that: include: Data acquisition module, data conversion module, encryption module and result generation module; The data acquisition module is used to obtain a model dictionary of the image algorithm model to be encrypted; The data conversion module is used to convert the model dictionary to obtain a byte stream; The encryption module is used to generate an AES key based on the AES encryption algorithm; The result generating module is used to determine the Fernet encryption model based on the AES key, so that the Fernet encryption model encrypts the byte stream to obtain encrypted data.
7. The image algorithm model encryption device according to claim 6, characterized in that: The data acquisition module includes: a retrieval unit and an identification unit; The retrieving unit is used to retrieve the model data of the image algorithm model to be encrypted; The identification unit is used to identify the data type, data content and metadata in the model data to obtain the model dictionary of the image algorithm model to be encrypted.
8. The image algorithm model encryption device according to claim 7, characterized in that: The data conversion module includes: serializing the model dictionary through a preset function of Python software to obtain a byte stream.
9. The image algorithm model encryption device according to claim 8, characterized in that: The Fernet encryption model encrypts the byte stream to obtain encrypted data, including: The Fernet encryption model divides the byte stream into blocks to obtain a plurality of byte stream sub-regions; The Fernet encryption model fills each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region; The Fernet encryption model performs encryption operation on each byte stream sub-filling area based on the AES key to obtain encrypted data.
10. The image algorithm model encryption device according to claim 9, characterized in that: The step of filling each of the byte stream sub-regions based on a preset filling scheme to obtain a byte stream sub-filling region corresponding to each byte stream sub-region includes: Based on the PKCS7 algorithm, determine the padding data corresponding to each byte stream sub-area; In each byte stream sub-region, padding data is added to the end of the data included in the byte stream sub-region to obtain a byte stream sub-padding region.
Citation Information
Cited By
Model encryption method and device and model decryption method and device
CN120602154A