File encryption and decryption method and device, electronic equipment and storage medium

By combining initialization vectors, file hash values, and encryption counts in a block encryption method within CBC mode, the initialization vector attack problem in CBC mode is solved, improving the security and performance of file encryption algorithms, especially the encryption and decryption efficiency of large files.

CN121508799APending Publication Date: 2026-02-10CHINA MOBILE COMM GRP CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511495497.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-10-20
Publication Date
2026-02-10

AI Technical Summary

Technical Problem

Existing file encryption methods based on CBC mode suffer from initial vector attack problems, especially when encrypting large files, resulting in poor performance and insufficient security.

Method used

By combining randomly generated initialization vectors, file hash values, and encryption counts, files are encrypted and decrypted in groups. The initialization vector attack problem is solved by using file hash values ​​in XOR operations and counter theory, thereby improving the security strength of the encryption algorithm.

Benefits of technology

It improves the security and performance of file encryption algorithms, especially when encrypting large files, reducing encryption and decryption time and improving file security and encryption efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121508799A_ABST
    Figure CN121508799A_ABST
Patent Text Reader

Abstract

The invention provides a file encryption and decryption method and device, electronic equipment and a storage medium, and belongs to the technical field of data security, and the method comprises the steps: carrying out the grouping encryption of a to-be-encrypted file based on a randomly generated initialization vector, a file hash value and the number of encryption times of the grouping encryption of the to-be-encrypted file; the file hash value is determined based on the file to be encrypted. According to the invention, on the basis of the file grouping encryption, the file grouping encryption is carried out not only based on the initialization vector, but also based on the file hash value and the encryption times of the to-be-encrypted file, so that the problem of initial vector attack existing in a standard grouping encryption mode can be solved, and the security strength of an encryption algorithm is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data security technology, and in particular to a method, apparatus, electronic device and storage medium for encrypting and decrypting files. Background Technology

[0002] Encrypting file data is an effective way to ensure the security of file data during storage and network transmission.

[0003] Encrypting file data in blocks using the Cipher Block Chaining (CBC) method is a common approach to file encryption. This method divides the file into multiple plaintext blocks and uses the ciphertext block encrypted from the previous plaintext block to encrypt the next plaintext block.

[0004] However, CBC mode requires a pre-determined initialization vector when encrypting the first plaintext block of a file, which poses a problem of initialization vector attack. Summary of the Invention

[0005] This application provides a file encryption and decryption method, apparatus, electronic device, and storage medium to address the shortcomings of existing technologies that suffer from initial vector attack problems when performing block encryption based on CBC mode, and to implement a file encryption and decryption scheme that solves the initial vector attack problem.

[0006] This application provides a file encryption method, including: Based on the randomly generated initialization vector, the file hash value, and the number of encryption attempts for the file to be encrypted, the file to be encrypted is encrypted in groups. The file hash value is determined based on the file to be encrypted.

[0007] According to a file encryption method provided in this application, for each current plaintext block of the file to be encrypted, the step of encrypting the file by blocks based on a randomly generated initialization vector, a file hash value, and the number of encryption attempts for the file to be encrypted includes: determining a first intermediate value based on the file hash value and the current number of encryption attempts corresponding to the current plaintext block; determining a second intermediate value based on the first intermediate value, the current plaintext block, and the fused encryption data corresponding to the current plaintext block; encrypting the second intermediate value based on a preset key to obtain a ciphertext block corresponding to the current plaintext block; when the current plaintext block is the first plaintext block to be encrypted in the file to be encrypted, the fused encryption data is the initialization vector; when the current plaintext block is not the first plaintext block to be encrypted in the file to be encrypted, the fused encryption data is the ciphertext block corresponding to the previous plaintext block of the current plaintext block.

[0008] According to a file encryption method provided in this application, before performing group encryption on the file to be encrypted based on a randomly generated initialization vector, a file hash value, and the number of encryption attempts, the method further includes: performing a hash operation on the file to be encrypted and / or a preset item of the file to be encrypted to obtain the file hash value; the preset item includes at least one of a file header, a cross-reference table, a file endnote, the main text, the first block of the main text, and the last block of the main text.

[0009] According to a file encryption method provided in this application, the step of encrypting the file to be encrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of encryption attempts for the file to be encrypted includes: if the file size of the file to be encrypted is greater than or equal to a first preset threshold, then, based on the initialization vector, the file hash value, and the number of encryption attempts, grouping and encrypting a preset encryption item of the file to be encrypted; the preset encryption item includes at least one of the following: the first block of the text, the last block of the text, and a cross-reference table.

[0010] This application also provides a file decryption method, including: Based on the randomly generated initialization vector, file hash value, and the number of decryption attempts for the file to be decrypted, the file to be decrypted is decrypted in groups. The file hash value is determined based on the file to be encrypted before it was encrypted.

[0011] According to a file decryption method provided in this application, for each current ciphertext block of the file to be decrypted, the step of decrypting the file in blocks based on a randomly generated initialization vector, a file hash value, and the number of decryption attempts for the file to be decrypted includes: determining a third intermediate value based on the file hash value and the current number of decryption attempts corresponding to the current ciphertext block; determining a fourth intermediate value based on the third intermediate value, the current ciphertext block, and the fused decryption data corresponding to the current ciphertext block; decrypting the fourth intermediate value based on a preset key to obtain the plaintext block corresponding to the current ciphertext block; when the current ciphertext block is the first ciphertext block to be decrypted in the file to be decrypted, the fused decryption data is the initialization vector; when the current ciphertext block is not the first ciphertext block to be decrypted in the file to be decrypted, the fused decryption data is the plaintext block corresponding to the previous ciphertext block of the current ciphertext block.

[0012] According to a file decryption method provided in this application, the file hash value is obtained by hashing the file to be encrypted and / or preset items of the file to be encrypted; the preset items include at least one of the following: file header, cross-reference table, file endnote, body, first block of body, and last block of body.

[0013] According to a file decryption method provided in this application, the step of decrypting the file to be decrypted in groups based on a randomly generated initialization vector, a file hash value, and a number of decryption attempts includes: if the file size of the file to be decrypted is greater than or equal to a second preset threshold, then based on the initialization vector, the file hash value, and the number of decryption attempts, decrypting a preset decryption item of the file to be decrypted in groups; the preset decryption item includes at least one of the following: the first block of the text, the last block of the text, and a cross-reference table.

[0014] This application also provides a file encryption device, comprising: The encryption module is used to perform block encryption on the file to be encrypted based on a randomly generated initialization vector, a file hash value, and a number of encryption attempts for the block encryption. The file hash value is determined based on the file to be encrypted.

[0015] This application also provides a file decryption device, comprising: The decryption module is used to decrypt the file to be decrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of decryption attempts. The file hash value is determined based on the file to be encrypted before it was encrypted.

[0016] This application also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and running on the processor, wherein the processor executes the computer program to implement any of the file encryption methods or file decryption methods described above.

[0017] This application also provides a non-transitory computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements any of the file encryption methods or file decryption methods described above.

[0018] This application also provides a computer program product, including a computer program that, when executed by a processor, implements any of the file encryption methods or file decryption methods described above.

[0019] The file encryption and decryption method, apparatus, electronic device, and storage medium provided in this application, by combining file block encryption with initialization vector encryption, and also combining file block encryption based on the file hash value and encryption count of the file to be encrypted, can solve the initialization vector attack problem existing in the standard block encryption mode and improve the security strength of the encryption algorithm. Attached Figure Description

[0020] To more clearly illustrate the technical solutions in this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0021] Figure 1 This is one of the flowcharts illustrating the file encryption method provided in this application.

[0022] Figure 2 This is the second flowchart illustrating the file encryption method provided in this application.

[0023] Figure 3 This is a schematic diagram of the structure of the PDF file provided in this application.

[0024] Figure 4 This is one of the flowcharts illustrating the file decryption method provided in this application.

[0025] Figure 5 This is the second flowchart illustrating the file decryption method provided in this application.

[0026] Figure 6 This is a schematic diagram of the file encryption device provided in this application.

[0027] Figure 7 This is a schematic diagram of the file decryption device provided in this application.

[0028] Figure 8 This is a schematic diagram of the structure of the electronic device provided in this application. Detailed Implementation

[0029] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0030] It should be noted that, in the description of this application, the term "comprising" or any other variation thereof is intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Those skilled in the art will understand the specific meaning of the above terms in this application according to the specific circumstances.

[0031] The terms "first," "second," etc., used in this application are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that embodiments of this application can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class, without limiting the number of objects; for example, a first object can be one or more. Furthermore, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects have an "or" relationship.

[0032] The following is combined Figures 1-8 This application describes the file encryption and decryption methods, apparatuses, electronic devices, and storage media provided.

[0033] With the rapid development of mobile internet and information compression technology, file transmission and communication have become extremely convenient. A large amount of PDF file data related to individuals, enterprises, and governments needs to be generated, processed, and transmitted and accessed via the internet. Due to the openness and sharing nature of the internet, people can access file resources anytime, anywhere using multiple devices. However, during the transmission of file data, there may be security risks such as privacy leaks, file theft, and malicious tampering with file content.

[0034] Encrypting file data is an effective data protection method to ensure the security of file data during storage and network transmission. By using keys and encryption algorithms to transform plaintext file data into random and irregular ciphertext data, the purpose of protecting the file is achieved.

[0035] Currently, two common methods for encrypting file data include file encryption methods and storage path encryption methods.

[0036] The file encryption method directly encrypts all the contents of the file, converts the file into hexadecimal bytecode, and then uses a certain cryptographic algorithm and key to encrypt the bytecode, obtaining ciphertext bytecode, which is then stored in a server database. When reading or using the file, the same cryptographic algorithm and key are used to decrypt the ciphertext bytecode to obtain plaintext bytecode, which is then converted back into a file to obtain the plaintext file.

[0037] The storage path encryption method stores files on the server's hard drive as folders and files, and determines its storage path in the file system. Files can be easily read, written, and deleted through the storage path in the file system. At the same time, in order to protect the confidentiality of files, the storage path of the files is encrypted. That is, the files are still stored in plaintext, but the storage path is encrypted.

[0038] For file encryption methods, when the file is small, such as only a few hundred KB or a few MB, direct file encryption is better. However, when the file is large, such as tens of MB, hundreds of MB or even larger, the file encryption will take a long time, and the decryption time when using the file will also be long, resulting in lower performance.

[0039] In file encryption methods, block cipher based on the CBC (Conditional Block Cycle) model is a common approach. This involves dividing the file into multiple plaintext blocks and using the ciphertext from the previous block to encrypt the next plaintext block. However, the CBC model requires a pre-determined initialization vector when encrypting the first plaintext block, which exposes it to initialization vector attacks.

[0040] Although the storage path encryption method encrypts the file's storage path, the server operator can still find the file by searching for keywords in the filename or file content. Since only the storage path is encrypted and the file content is not, the server operator can still see the plaintext of the file after finding it, which does not completely protect the confidentiality of the file.

[0041] Figure 1 This is one of the flowcharts illustrating the file encryption method provided in this application, such as... Figure 1 As shown, the file encryption method includes, but is not limited to, step 101.

[0042] It should be noted that the execution subject of the file encryption method provided in this application is the corresponding file encryption device, which can be a server, computer equipment, such as mobile phone, tablet computer, laptop computer, handheld computer, vehicle electronic equipment, wearable device, ultra-mobile personal computer (UMPC), netbook or personal digital assistant (PDA), etc.

[0043] Step 101: Encrypt the file to be encrypted by grouping it based on the initialization vector (IV), the file hash value, and the number of times the file to be encrypted is grouped.

[0044] The file hash value is determined based on the file to be encrypted.

[0045] It should be noted that the file encryption method provided in this application is applicable to encryption algorithms that rely on initialization vectors and are based on CBC mode, including but not limited to any one of the following encryption algorithms: the Chinese national cryptographic algorithm SM4, the Advanced Encryption Standard (AES), and the Data Encryption Standard (DES). The following embodiments all use the Chinese national cryptographic algorithm SM4 as an example to illustrate the file encryption method provided in this application. The block length and key length of the SM4 algorithm are both 128 bits.

[0046] Optionally, the type of the file to be encrypted includes, but is not limited to, any of the common document formats such as Portable Document Format (PDF), Plain Text File (TXT), OfficeOpen XML Document (DOCX), Excel Spreadsheet, and PowerPoint Presentation (PPT).

[0047] An initialization vector is a fixed-length input value used in cryptography to enhance encryption security; it is also known as an initial variable. Its core function is to ensure that different ciphertexts are generated when the same key is used for encryption.

[0048] Specifically, before encrypting the file to be encrypted in groups, the file hash value is determined based on the file to be encrypted. The hash value is obtained by performing a hash operation on the file to be encrypted. This hash value is the file hash value.

[0049] Furthermore, the file to be encrypted is divided into groups according to the block length of the encryption algorithm, resulting in multiple plaintext blocks, and a unique initialization vector with the same length as the plaintext blocks is randomly generated.

[0050] It is understood that the initialization vector is random and unique, and the length of the initialization vector is equal to the length of the plaintext block. Its generation method can be determined according to the specific encryption mode used for block encryption, and this application does not impose any restrictions on this.

[0051] Taking the SM4 algorithm as an example, the file to be encrypted is divided into 128-bit groups, and a 128-bit initialization vector is randomly generated.

[0052] When encrypting a file into blocks, the encryption process does not rely solely on the initialization vector specified in the CBC mode. Instead, it simultaneously encrypts the file into blocks based on the initialization vector, the file hash value, and the number of times the file is encrypted, in order to obtain the encrypted file.

[0053] The file encryption method provided in this application, by combining file block encryption with initialization vector encryption, and also with file hash value and encryption count of the file to be encrypted, can solve the initialization vector attack problem in the standard block encryption mode and improve the security strength of the encryption algorithm.

[0054] Based on the above embodiments, as an optional embodiment, for each current plaintext block of the file to be encrypted, the step of encrypting the file by blocks based on a randomly generated initialization vector, a file hash value, and the number of times the file to be encrypted is to be encrypted includes: Based on the file hash value and the current encryption count corresponding to the current plaintext block, a first intermediate value is determined; A second intermediate value is determined based on the first intermediate value, the current plaintext packet, and the fused encrypted data corresponding to the current plaintext packet; The second intermediate value is encrypted based on a preset key to obtain the ciphertext block corresponding to the current plaintext block; When the current plaintext block is the first plaintext block to be encrypted in the file to be encrypted, the fused encryption data is the initialization vector; If the current plaintext packet is not the first plaintext packet to be encrypted in the file to be encrypted, the fused encrypted data is the ciphertext packet corresponding to the previous plaintext packet of the current plaintext packet.

[0055] Specifically, Figure 2 This is the second flowchart illustrating the file encryption method provided in this application, as shown below. Figure 2 As shown, when encrypting a file, the file hash value is determined in advance based on the file to be encrypted. The plaintext content of the file to be encrypted is divided into several plaintext groups in advance, and several plaintext groups are determined as the current plaintext group in turn.

[0056] If the current plaintext block is the first plaintext block to be encrypted in the file to be encrypted, then the current plaintext block is plaintext block 1, the current encryption count of the current plaintext block is 1, and the merged encrypted data is used as the initialization vector. The first intermediate value is determined based on the results of addition, multiplication, subtraction, etc., between the file hash value and the current encryption count "1". For example, the first intermediate value is "file hash value + 1".

[0057] Furthermore, related operations are performed on the first intermediate value, the current plaintext block (plaintext block 1), and the initialization vector to obtain the second intermediate value. Then, the second intermediate value is encrypted using a preset key to obtain the ciphertext block corresponding to the current plaintext block (plaintext block 1), i.e., ciphertext block 1.

[0058] After completing the encryption of plaintext block 1, plaintext block 2 is used as the current plaintext block. If the current plaintext block is not the first plaintext block to be encrypted in the file to be encrypted, the current encryption count of the current plaintext block is 2. The merged encrypted data is the ciphertext block (ciphertext block 1) corresponding to the previous plaintext block (plaintext block 1) of the current plaintext block (plaintext block 2).

[0059] The first intermediate value is determined based on the result of addition, multiplication, subtraction, or other operations between the file hash value and the current encryption count "2". For example, the first intermediate value is "file hash value + 2".

[0060] Furthermore, related operations are performed on the first intermediate value, the current plaintext block (plaintext block 2), and ciphertext block 1 to obtain the second intermediate value. Then, the second intermediate value is encrypted using a preset key to obtain the ciphertext block corresponding to the current plaintext block (plaintext block 2), i.e., ciphertext block 2.

[0061] By analogy, the plaintext content of the file to be encrypted is divided into several plaintext groups in sequence. The steps of determining the first intermediate value, determining the second intermediate value, and encrypting the second intermediate value based on the preset key are repeated until the encryption of the last plaintext group of the file to be encrypted is completed, and the final ciphertext of the file to be encrypted is obtained.

[0062] Optionally, determining the second intermediate value based on the first intermediate value, the current plaintext packet, and the fused encrypted data corresponding to the current plaintext packet includes: Perform an XOR operation on the first intermediate value and the fused encrypted data to obtain a first XOR result; perform an XOR operation on the first XOR result and the current plaintext block to obtain a second XOR result; determine the second intermediate value based on the second XOR result.

[0063] By using the aforementioned method for determining the second intermediate value, a file group encryption method that integrates the file hash value and encryption count of a file and is compatible with the national cryptographic SM4 algorithm can be implemented.

[0064] The file encryption method provided in this application introduces the file hash value of the file to be encrypted into XOR operations and other block operations on the standard CBC encryption block mode. Furthermore, each time the file hash value is used for encryption, a counter theory is incorporated. The data participating in the block encryption for the current block is determined by combining the file hash value and the encryption count. By using the file hash value and the counter method, the initial vector attack problem existing in the standard CBC mode can be solved, thereby improving the security strength and encryption performance of the encryption algorithm and effectively enhancing file security.

[0065] Based on the above embodiments, as an optional embodiment, before performing block encryption on the file to be encrypted based on the randomly generated initialization vector, file hash value, and the number of times the block encryption is performed, the method further includes: Perform a hash operation on the file to be encrypted and / or the preset items of the file to be encrypted to obtain the file hash value; The preset items include at least one of the following: file header, cross-reference table, file endnote, body, first block of body, and last block of body.

[0066] Preset items are pre-determined file content items in the file to be encrypted that are used for hash calculations to obtain the file hash value.

[0067] Specifically, when the file to be encrypted is a PDF file, Figure 3 This is a structural diagram of the PDF file provided in this application, as shown below. Figure 3 As shown, before performing group encryption on the file to be encrypted, when determining the file hash value based on the file to be encrypted, in addition to directly performing a hash operation on the complete file to be encrypted to obtain the file hash value, at least one of the preset items of the file header, cross-reference table, file endnote, body, first block of the body and last block of the body of the file to be encrypted can also be hashed to obtain the file hash value.

[0068] The first block of the text is a content block of a preset size that starts from the beginning of the text of the file to be encrypted; the last block of the text is a content block of a preset size that starts from the end of the text of the file to be encrypted.

[0069] Optionally, the preset size of the first block and the last block of the text is determined by the business type of the file to be encrypted.

[0070] For example, when the business type of the file to be encrypted is a PDF paper for review, the title page at the end of the PDF paper for review, which includes student information, supervisor information, training institution and other identity information, is designated as the first block of the main text, and the acknowledgments page at the end of the PDF paper for review is designated as the last block of the main text.

[0071] Optionally, the step of performing a hash operation on the file to be encrypted and / or a preset item of the file to be encrypted to obtain the file hash value includes: Determine the list of preset items consisting of preset items and their indices; Perform a modulo operation on the current number of encryption attempts and the preset number of items, and use the resulting modulo as the target index value; Based on the target index value, a target preset item is determined from the preset item list; A hash operation is performed on the target preset item of the file to be encrypted to obtain the file hash value.

[0072] The file encryption method provided in this application, by determining the file hash value not only based on the initialization vector but also based on all or part of the preset items of the file to be encrypted, and combining the number of encryption attempts to perform file group encryption, can solve the initialization vector attack problem existing in the standard block encryption mode and further improve the security strength of the encryption algorithm.

[0073] Based on the above embodiments, as an optional embodiment, the step of performing group encryption on the file to be encrypted based on the randomly generated initialization vector, file hash value, and the number of encryption attempts based on the file to be encrypted includes: If the file size of the file to be encrypted is greater than or equal to a first preset threshold, then the preset encryption items of the file to be encrypted are grouped and encrypted based on the initialization vector, the file hash value, and the number of encryption attempts. The preset encryption items include at least one of the following: the first block of the text, the last block of the text, and the cross-reference table.

[0074] Specifically, a first preset threshold (such as 1024MB, 2048MB, etc.) is set in advance. After receiving the file to be encrypted, the file size is parsed. When the file size of the file to be encrypted is greater than or equal to the first preset threshold, at least one of the preset encryption items, such as the first block of the text, the last block of the text, and the cross-reference table of the file to be encrypted, is grouped and encrypted based on the initialization vector, the file hash value, and the number of encryption times.

[0075] When the file size of the file to be encrypted is less than the first preset threshold, the entire content of the file to be encrypted is grouped and encrypted based on the initialization vector, file hash value and encryption count.

[0076] The file encryption method provided in this application determines the encryption method based on the size of the file by setting a first preset threshold. When the file is small and less than the threshold, all content of the file is directly encrypted. When the file is large and greater than or equal to the threshold, only part of the file is encrypted, such as the first block of the main text, the last block of the main text, and the cross-reference table of the PDF file. This solves the encryption and decryption performance problem of large files.

[0077] Figure 4 This is one of the flowcharts illustrating the file decryption method provided in this application, such as... Figure 4 As shown, the file decryption method includes, but is not limited to, step 401.

[0078] It should be noted that the execution subject of the file decryption method provided in this application is the corresponding file decryption device, which can be a server, computer equipment, such as mobile phones, tablets, laptops, handheld computers, in-vehicle electronic devices, wearable devices, ultra-mobile personal computers (UMPCs), netbooks, or personal digital assistants (PDAs), etc.

[0079] Step 401: Decrypt the file to be decrypted in groups based on the randomly generated initialization vector, file hash value, and the number of decryption attempts for each group.

[0080] The file hash value is determined based on the file to be encrypted before it was encrypted.

[0081] It should be noted that the file decryption method provided in this application is applicable to encryption algorithms that rely on initialization vectors and are based on CBC mode, including but not limited to any one of the following encryption algorithms: the Chinese national cryptographic algorithm SM4, the AES algorithm, and the DES algorithm. The following embodiments all use the Chinese national cryptographic algorithm SM4 as an example to illustrate the file decryption method provided in this application. The block length and key length of the SM4 algorithm are both 128 bits.

[0082] Optionally, the type of the file to be decrypted includes, but is not limited to, any of the common document formats such as PDF, TXT, DOCX, Excel, and PPT.

[0083] Specifically, before decrypting the files to be decrypted in groups, the pre-stored file hash values, determined based on the files to be encrypted before they were encrypted, are extracted from the server database.

[0084] Furthermore, the file to be decrypted is divided into multiple ciphertext blocks according to the block length of the encryption algorithm, and the same initialization vector as the encryption is used.

[0085] When decrypting a file into blocks, the decryption process is not based solely on the initialization vector specified in the CBC mode. Instead, it simultaneously decrypts the file into blocks based on the initialization vector, the file hash value, and the number of decryption attempts performed on each block to obtain the decrypted plaintext file.

[0086] The file decryption method provided in this application, by combining file group decryption not only based on the initialization vector, but also based on the file hash value and decryption count of the file to be decrypted, can solve the initialization vector attack problem existing in the standard block encryption mode and improve the security strength of the encryption algorithm.

[0087] Based on the above embodiments, as an optional embodiment, for each current ciphertext block of the file to be decrypted, the step of decrypting the file by blocks based on the randomly generated initialization vector, file hash value, and the number of times the file to be decrypted is decrypted includes: A third intermediate value is determined based on the file hash value and the current decryption count corresponding to the current ciphertext block; Based on the third intermediate value, the current ciphertext block, and the fused decryption data corresponding to the current ciphertext block, a fourth intermediate value is determined; The fourth intermediate value is decrypted based on a preset key to obtain the plaintext block corresponding to the current ciphertext block; If the current ciphertext block is the first ciphertext block to be decrypted in the file to be decrypted, the fused decryption data is the initialization vector; If the current ciphertext block is not the first ciphertext block to be decrypted in the file to be decrypted, the fused decryption data is the plaintext block corresponding to the previous ciphertext block of the current ciphertext block.

[0088] Specifically, Figure 5 This is the second flowchart illustrating the file decryption method provided in this application, as shown below. Figure 5 As shown, during file decryption, the pre-stored file hash value determined based on the file to be encrypted before encryption is extracted from the server database. The ciphertext content of the file to be decrypted is divided into several ciphertext groups, and several ciphertext groups are determined as the current ciphertext group in turn.

[0089] If the current ciphertext block is the first ciphertext block to be decrypted in the file to be decrypted, then the current ciphertext block is ciphertext block 1, and the current decryption count for the current ciphertext block is 1. The merged decryption data is used as the initialization vector. A third intermediate value is determined based on the results of addition, multiplication, and subtraction operations between the file hash value and the current decryption count "1". For example, the first intermediate value is "file hash value + 1".

[0090] Furthermore, correlation operations are performed on the sum and initialization vector of the first intermediate value, the current ciphertext block (ciphertext block 1), and the fourth intermediate value to obtain the fourth intermediate value. Then, the fourth intermediate value is decrypted using the preset key to obtain the plaintext block corresponding to the current ciphertext block (ciphertext block 1), i.e., plaintext block 1.

[0091] After decrypting ciphertext block 1, ciphertext block 2 is used as the current ciphertext block. If the current ciphertext block is not the first ciphertext block to be decrypted in the file to be decrypted, the current decryption count of the current ciphertext block is 2. The merged encrypted data is the plaintext block (plaintext block 1) corresponding to the previous ciphertext block (ciphertext block 1) of the current ciphertext block (ciphertext block 2).

[0092] The third intermediate value is determined based on the results of addition, multiplication, and subtraction between the file hash value and the current decryption count "2". For example, the third intermediate value is "file hash value + 2".

[0093] Furthermore, related operations are performed on the third intermediate value, the current ciphertext block (ciphertext block 2), and plaintext block 1 to obtain the fourth intermediate value. Then, the fourth intermediate value is decrypted using a preset key to obtain the plaintext block corresponding to the current ciphertext block (ciphertext block 2), i.e., plaintext block 2.

[0094] By analogy, the ciphertext content of the file to be decrypted is divided into several ciphertext groups in sequence. The steps of determining the third intermediate value, determining the fourth intermediate value, and decrypting the fourth intermediate value based on the preset key are repeated until the last ciphertext group of the file to be decrypted is decrypted, and the final plaintext of the file to be decrypted is obtained.

[0095] Optionally, determining the fourth intermediate value based on the third intermediate value, the current ciphertext block, and the fused decryption data corresponding to the current ciphertext block includes: Perform an XOR operation on the third intermediate value and the fused decrypted data to obtain the third XOR operation result; perform an XOR operation on the third XOR operation result and the current ciphertext block to obtain the fourth XOR operation result; determine the fourth intermediate value based on the fourth XOR operation result.

[0096] By using the aforementioned method for determining the fourth intermediate value, a file grouping decryption method that integrates the file hash value and decryption count of a file and is compatible with the national cryptographic SM4 algorithm can be implemented.

[0097] The file decryption method provided in this application introduces the file hash value of the file to be encrypted before encryption into the block operation, such as XOR operation, on the standard CBC encryption block mode. Furthermore, each time the file hash value is used for decryption, the counter theory is incorporated. The data participating in the block decryption for the current block is determined by combining the file hash value and the decryption count. By using the file hash value and the counter method, the initial vector attack problem existing in the standard CBC mode can be solved, which improves the security strength and encryption performance of the encryption algorithm and effectively enhances file security.

[0098] Based on the above embodiments, as an optional embodiment, the file hash value is obtained by performing a hash operation on the file to be encrypted and / or a preset item of the file to be encrypted; The preset items include at least one of the following: file header, cross-reference table, file endnote, body, first block of body, and last block of body.

[0099] Specifically, when the file to be encrypted is a PDF file, the file hash value extracted from the server database is obtained by performing a hash operation on the file to be encrypted before the file is grouped and encrypted. In addition to directly performing a hash operation on the file to be encrypted, it can also be obtained by performing a hash operation on at least one of the preset items of the file header, cross-reference table, file endnote, body, first block of body and last block of body of body.

[0100] It should be noted that the file decryption method provided in this embodiment can be referred to in correspondence with the file encryption method provided in any of the above embodiments in specific implementation, and will not be described again.

[0101] The file decryption method provided in this application, by determining the file hash value not only based on the initialization vector but also based on all or part of the preset items of the file to be encrypted, and combining the number of decryption attempts to perform file group decryption, can solve the initialization vector attack problem existing in the standard block decryption mode and further improve the security strength of the encryption algorithm.

[0102] Based on the above embodiments, as an optional embodiment, the step of performing group decryption on the file to be decrypted based on the randomly generated initialization vector, file hash value, and the number of decryption attempts, includes: If the file size of the file to be decrypted is greater than or equal to the second preset threshold, then the preset decryption items of the file to be decrypted are decrypted in groups based on the initialization vector, the file hash value and the number of decryption attempts; The preset decryption items include at least one of the following: the first block of the text, the last block of the text, and the cross-reference table.

[0103] Specifically, a second preset threshold (such as 1024MB, 2048MB, etc.) is set in advance. Based on the file size of the file to be decrypted, when the file size of the file to be decrypted is greater than or equal to the second preset threshold, at least one of the preset decryption items such as the first block of the text, the last block of the text, and the cross-reference table of the file to be decrypted is decrypted in groups based on the initialization vector, the file hash value, and the number of decryption times.

[0104] When the file size of the file to be decrypted is less than the second preset threshold, the entire content of the file to be decrypted is decrypted in groups based on the initialization vector, file hash value and decryption count.

[0105] It should be noted that the second preset threshold can be determined based on the first preset threshold and the ratio of the volume change of the file before and after encryption.

[0106] It should be noted that the file decryption method provided in this embodiment can be referred to in correspondence with the file encryption method provided in any of the above embodiments in specific implementation, and will not be described again.

[0107] The file decryption method provided in this application determines the decryption method based on the size of the file to be decrypted by setting a second preset threshold. When the file is small and less than the threshold, all content of the file is directly decrypted; when the file is large and greater than or equal to the threshold, only part of the file is decrypted, specifically the first block of the main text, the last block of the main text, and the cross-reference table of the PDF file. This solves the encryption and decryption performance problem of large files.

[0108] In one embodiment, after receiving the file to be encrypted, the file size of the file to be encrypted is parsed, and the relationship between the file size of the file to be encrypted and a first preset threshold (e.g., 1024MB) is determined. When the file size is less than the first preset threshold, the file to be decrypted is decrypted in groups based on the initialization vector, file hash value and decryption count.

[0109] Calculate the file hash value fhash of the file to be encrypted; convert the file to be encrypted into hexadecimal bytecode data; divide all bytecode data into 128-bit groups to obtain several plaintext groups; randomly generate a 128-bit initialization vector iv; and generate a preset key key.

[0110] Taking the standard Chinese cryptographic algorithm SM4 as an example, the encryption process involves multiple encryption steps based on the initialization vector (iv), file hash value (fhash), and the bytecode (data) of the file to be encrypted. A preset key (key) is used to encrypt the file in blocks, and the algorithm calculates encrypt(key, iv, fhash, data) to obtain the final ciphertext (cipher_data) of the bytecode. The final ciphertext (cipher_data) and the file hash value (fhash) are then stored in a server database.

[0111] When a file needs to be read or used, the final ciphertext `cipher_data` and the file hash value `fhash` are retrieved from the server database. The final ciphertext `cipher_data` is then decrypted based on the file hash value `fhash` and the initialization vector `iv`. Using the standard SM4 algorithm, `decrypt(key, iv, fhash, cipher_data)` is calculated to decrypt and obtain the plaintext bytecode `data`. The plaintext bytecode `data` is then converted back into a file to obtain the final plaintext.

[0112] When the file size of the file to be encrypted is greater than or equal to the first preset threshold, the first block of the text, the last block of the text, and the cross-reference table of the file to be encrypted are grouped and encrypted based on the initialization vector, the file hash value, and the number of encryption attempts.

[0113] Calculate the file hash value fhash of the file to be encrypted; extract the first block first_data (e.g., the length of the first block content is defined as 1024 bytes), the last block last_data (e.g., the length of the last block content is 1024 bytes), and the cross-reference table jcyy_data from the file to be encrypted.

[0114] First_data, last_data, and jcyy_data are grouped into 128-bit segments, a 128-bit initialization vector (iv) is randomly generated, and a preset key (key) is also generated.

[0115] Based on the standard SM4 algorithm, encrypt(key, iv, fhash, first_data) is calculated to obtain the ciphertext c_first_data of the first block of the main text; encrypt(key, iv, fhash, last_data) is calculated to obtain the ciphertext c_last_data of the last block of the main text; encrypt(key, iv, fhash, jcyy_data) is calculated to obtain the ciphertext c_jcyy_data of the cross-reference table; and fhash, c_first_data, c_last_data, and c_jcyy_data are stored in the server database.

[0116] When a file needs to be read or used, the following steps are performed: First, the encrypted first block of the text (c_first_data), the encrypted last block of the text (c_last_data), the encrypted cross-reference table (c_jcyy_data), and the file hash (fhash) are extracted from the server database. Based on the standard SM4 algorithm, `decrypt(key, iv, fhash, c_first_data)` is calculated to obtain the plaintext first block of the text (first_data); then, `decrypt(key, iv, fhash, c_last_data)` is calculated to obtain the plaintext last block of the text (last_data); finally, `decrypt(key, iv, fhash, c_jcyy_data)` is calculated to obtain the plaintext jcyy_data of the cross-reference table. The plaintext first_data, last_data, and jcyy_data are then combined to reconstruct the file according to the standard PDF format, resulting in the plaintext PDF file.

[0117] Overall, the file encryption and decryption methods provided in this application improve upon the standard CBC encryption / decryption block mode. They introduce the hash value of the file to be encrypted, such as a PDF file, into the XOR operation of the blocks, and use counter theory to solve the initial vector attack problem present in the standard CBC mode, thus improving the security strength of the encryption algorithm. Furthermore, the method of determining the encryption method based on the size of the file solves the problem of low encryption / decryption performance when the file is large. When the file to be encrypted is large, only the first block of the text, the last block of the text, and the cross-reference table are encrypted, thus solving the encryption / decryption performance problem for large files.

[0118] Figure 6 This is a schematic diagram of the file encryption device provided in this application, such as... Figure 6 As shown, the file encryption device includes, but is not limited to, encryption module 601.

[0119] The encryption module 601 is used to perform group encryption on the file to be encrypted based on a randomly generated initialization vector, a file hash value, and a number of encryption attempts.

[0120] It should be noted that the file encryption device provided in this application can execute the file encryption method described in any of the above embodiments during actual operation, which will not be elaborated in this embodiment.

[0121] The file encryption device provided in this application, by combining file block encryption with initialization vector encryption, and also with file hash value and encryption count of the file to be encrypted, can solve the initialization vector attack problem in standard block encryption mode and improve the security strength of encryption algorithm.

[0122] Figure 7 This is a schematic diagram of the file decryption device provided in this application, such as... Figure 7 As shown, the file decryption device includes, but is not limited to, the decryption module 701.

[0123] The decryption module 701 is used to decrypt the file to be decrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of decryption attempts.

[0124] The file decryption device provided in this application, by combining file group decryption not only based on the initialization vector, but also based on the file hash value and decryption count of the file to be decrypted, can solve the initialization vector attack problem in the standard block encryption mode and improve the security strength of the encryption algorithm.

[0125] Figure 8 This is a schematic diagram of the structure of the electronic device provided in this application, such as... Figure 8 As shown, the electronic device may include: a processor 810, a communications interface 820, a memory 830, and a communication bus 840, wherein the processor 810, the communications interface 820, and the memory 830 communicate with each other through the communication bus 840. The processor 810 can call logical instructions in the memory 830 to execute the file encryption method provided in any of the above embodiments. The file encryption method includes, but is not limited to, the following steps: encrypting the file to be encrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of encryption attempts for the file to be encrypted; the file hash value is determined based on the file to be encrypted. Alternatively, the file decryption method provided in any of the above embodiments can be executed, wherein the file decryption method includes, but is not limited to, the following steps: decrypting the file to be decrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of decryption attempts for the file to be decrypted; wherein the file hash value is determined based on the file to be encrypted before the file to be decrypted is encrypted.

[0126] Furthermore, the logical instructions in the aforementioned memory 830 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0127] On the other hand, this application also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the file encryption method provided in any of the above embodiments. The file encryption method includes, but is not limited to, the following steps: encrypting the file to be encrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of encryption attempts for the file to be encrypted; the file hash value is determined based on the file to be encrypted. Alternatively, the file decryption method provided in any of the above embodiments can be executed, wherein the file decryption method includes, but is not limited to, the following steps: decrypting the file to be decrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of decryption attempts for the file to be decrypted; wherein the file hash value is determined based on the file to be encrypted before the file to be decrypted is encrypted.

[0128] In another aspect, this application also provides a non-transitory computer-readable storage medium storing a computer program thereon. When the computer program is executed by a processor, it implements the file encryption method provided in any of the above embodiments. The file encryption method includes, but is not limited to, the following steps: encrypting the file to be encrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of encryption attempts for the file to be encrypted; the file hash value is determined based on the file to be encrypted. Alternatively, the file decryption method provided in any of the above embodiments can be executed, wherein the file decryption method includes, but is not limited to, the following steps: decrypting the file to be decrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of decryption attempts for the file to be decrypted; wherein the file hash value is determined based on the file to be encrypted before the file to be decrypted is encrypted.

[0129] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.

[0130] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.

[0131] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.

Claims

1. A file encryption method, characterized in that, include: Based on the randomly generated initialization vector, the file hash value, and the number of encryption attempts for the file to be encrypted, the file to be encrypted is encrypted in groups. The file hash value is determined based on the file to be encrypted.

2. The file encryption method according to claim 1, characterized in that, For each current plaintext block of the file to be encrypted, the step of encrypting the file by blocks based on a randomly generated initialization vector, a file hash value, and the number of times the file is encrypted, includes: Based on the file hash value and the current encryption count corresponding to the current plaintext block, a first intermediate value is determined; A second intermediate value is determined based on the first intermediate value, the current plaintext packet, and the fused encrypted data corresponding to the current plaintext packet; The second intermediate value is encrypted based on a preset key to obtain the ciphertext block corresponding to the current plaintext block; When the current plaintext block is the first plaintext block to be encrypted in the file to be encrypted, the fused encryption data is the initialization vector; If the current plaintext packet is not the first plaintext packet to be encrypted in the file to be encrypted, the fused encrypted data is the ciphertext packet corresponding to the previous plaintext packet of the current plaintext packet.

3. The file encryption method according to claim 1, characterized in that, Before performing block encryption on the file to be encrypted, based on the randomly generated initialization vector, file hash value, and the number of encryption attempts using the file to be encrypted, the following steps are included: Perform a hash operation on the file to be encrypted and / or the preset items of the file to be encrypted to obtain the file hash value; The preset items include at least one of the following: file header, cross-reference table, file endnote, body, first block of body, and last block of body.

4. The file encryption method according to any one of claims 1-3, characterized in that, The step of encrypting the file into blocks based on a randomly generated initialization vector, file hash value, and the number of encryption attempts, includes: If the file size of the file to be encrypted is greater than or equal to a first preset threshold, then the preset encryption items of the file to be encrypted are grouped and encrypted based on the initialization vector, the file hash value, and the number of encryption attempts. The preset encryption items include at least one of the following: the first block of the text, the last block of the text, and the cross-reference table.

5. A file decryption method, characterized in that, include: Based on the randomly generated initialization vector, file hash value, and the number of decryption attempts for the file to be decrypted, the file to be decrypted is decrypted in groups. The file hash value is determined based on the file to be encrypted before it was encrypted.

6. The file decryption method according to claim 5, characterized in that, For each current ciphertext block of the file to be decrypted, the process of decrypting the file by blocks based on the randomly generated initialization vector, file hash value, and the number of times the file is decrypted includes: A third intermediate value is determined based on the file hash value and the current decryption count corresponding to the current ciphertext block; Based on the third intermediate value, the current ciphertext block, and the fused decryption data corresponding to the current ciphertext block, a fourth intermediate value is determined; The fourth intermediate value is decrypted based on a preset key to obtain the plaintext block corresponding to the current ciphertext block; If the current ciphertext block is the first ciphertext block to be decrypted in the file to be decrypted, the fused decryption data is the initialization vector; If the current ciphertext block is not the first ciphertext block to be decrypted in the file to be decrypted, the fused decryption data is the plaintext block corresponding to the previous ciphertext block of the current ciphertext block.

7. The file decryption method according to claim 5, characterized in that, The file hash value is obtained by performing a hash operation on the file to be encrypted and / or a preset item of the file to be encrypted. The preset items include at least one of the following: file header, cross-reference table, file endnote, body, first block of body, and last block of body.

8. The file decryption method according to any one of claims 5-7, characterized in that, The process of decrypting the file into groups based on a randomly generated initialization vector, file hash value, and the file to be decrypted, including the following steps: If the file size of the file to be decrypted is greater than or equal to the second preset threshold, then the preset decryption items of the file to be decrypted are decrypted in groups based on the initialization vector, the file hash value and the number of decryption attempts; The preset decryption items include at least one of the following: the first block of the text, the last block of the text, and the cross-reference table.

9. A file encryption device, characterized in that, include: The encryption module is used to perform block encryption on the file to be encrypted based on a randomly generated initialization vector, a file hash value, and a number of encryption attempts for the block encryption. The file hash value is determined based on the file to be encrypted.

10. A file decryption device, characterized in that, include: The decryption module is used to decrypt the file to be decrypted in groups based on a randomly generated initialization vector, a file hash value, and the number of decryption attempts. The file hash value is determined based on the file to be encrypted before it was encrypted.

11. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that, When the processor executes the computer program, it implements the file encryption method as described in any one of claims 1 to 4 or the file decryption method as described in any one of claims 5 to 8.

12. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the file encryption method as described in any one of claims 1 to 4 or the file decryption method as described in any one of claims 5 to 8.

13. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by the processor, it implements the file encryption method as described in any one of claims 1 to 4 or the file decryption method as described in any one of claims 5 to 8.