Summary of the invention
The objective of the invention is to overcome above-mentioned shortcoming and defect, a kind of file encryption and distribution method based on digital certificate is provided, this method has advantages such as adopting multiple encryption method combination, convenience, secure distribution, fail safe height, and solved that traditional symmetric encipherment algorithm brings can't recover the problem of original text because of forgetting Password.
The objective of the invention is to be achieved through the following technical solutions: a kind of file encryption and distribution method based on digital certificate as shown in Figure 1, may further comprise the steps:
S1, file master initialization operational factor, the file encryption password that uses when encrypt file is set obtains file recipient's PKI, and a newly-built file enters step S2;
S2, file recipient's number is write in the new file, enter step S3;
S3, each file recipient's encrypted authentication information is write in the new file successively, enter step S4;
S4, the source file that needs are encrypted carry out being written to successively in the new file behind the block encryption, enter step S5;
S5, new file is sent to each file recipient.
To better implement the present invention, described file recipient comprises file master self.
Preferably, described file recipient's encrypted authentication information specifically comprises:
(1) use file recipient's PKI by rivest, shamir, adelman, is encrypted the file encryption password string among the step S1, obtains file recipient's file encrypted message;
(2) length value of above-mentioned file encrypted message.
Preferably, described step S3, each file recipient's encrypted authentication information is write in the new file successively, specifically may further comprise the steps:
S3.1, file instigator use file recipient's PKI, by rivest, shamir, adelman, the file encryption password string are encrypted, obtain file recipient's file encrypted message, obtain this document encrypted message string length, length value is appended in the new file, enter step S3.2;
S3.2, file chief commander file encrypted message are appended in the new file, enter step S3.3;
S3.3, file master judge whether to generate all files recipient's file encrypted message, if then enter step S4; If not, return step S3.1.
Preferably, described rivest, shamir, adelman is RSA cryptographic algorithms, ECC (elliptic curve) cryptographic algorithm or other rivest, shamir, adelmans.
Preferably, described step S4, the source file that needs are encrypted carry out being written to successively in the new file behind the block encryption, specifically are meant:
The file main root is according to the symmetric encipherment algorithm that will use, source file is divided into groups, when if the not enough composition one of last grouped data of source file divides into groups, then make last grouping consistent with other grouped data length in the terminal zero padding of data, the file master encrypts each grouping by symmetric encipherment algorithm;
The file master is appended to the physical length value of last group of source file in the new file, and each grouping after encrypting is write in the new file successively.
Preferably, described step S4, the source file that needs are encrypted carry out being written to successively in the new file behind the block encryption, specifically may further comprise the steps:
S4.1, file master are the length value that unit obtains source file with the byte, calculate the mould of source file length value and m, and the result is appended to during new file connects, and enter step S4.2;
S4.2, file master judge that whether source file length is 0, if be not 0, then enters step S4.3; If be 0, then jump to step S5;
S4.3, file master read the preceding m byte data of source file, when the not enough m byte of data, make its length just in time be the m byte in the terminal zero padding of data, use the file encryption password among the step S1, by symmetric encipherment algorithm this m byte data is encrypted, result after encrypting is appended in the new file, enters step S4.4;
S4.4, file master judge whether untreated residue file size is 0 in the source file, if be not 0, then enters step S4.5; If be 0, then jump to step S5;
S4.5, file master read ensuing m byte data, when the not enough m byte of data, terminal zero padding makes its length just in time be the m byte, use the file encryption password among the step S1, by symmetric encipherment algorithm this m byte data is encrypted, result after encrypting is appended in the new file, is back to step S4.4.
Preferably, the original length of each grouping when described m represents block encryption, the m value is by the symmetric encipherment algorithm decision of being adopted.
If adopt the AES cryptographic algorithm, then the m value is 16.
Preferably, described symmetric encipherment algorithm is one or more among DES, 3DES, RC4, RC5 and the Blowfish.
Compared with prior art, the present invention has following beneficial effect:
The first, multiple encryption method combination: the present invention combines traditional symmetric encipherment algorithm and modern public key encryption algorithm, use symmetric encipherment algorithm to realize encrypted content file, the cryptographic algorithm that uses public-key stamps file recipient's finger print information and file encryption information, the file recipient at first uses the private key deciphering of oneself to obtain the file encryption password, use oneself private key and file encryption password to carry out file decryption more simultaneously, obtain original document, guarantee safety of files.
The second, secure distribution: adopt file user PKI to come the file of needs distribution is encrypted, system is when encrypting file, in file header, used a variable-length file head form, each file recipient user's of corresponding record public key verifications information and encrypted authentication information realize the secure distribution file.The file master only needs to select simultaneously the PKI of the groups or users correspondence of needs distribution, once encrypts, and just can give different users with the file distributing after encrypting, and reach once the purpose that encryption, multi-user's multigroup component are sent out authentication.
What three, solved that traditional symmetric encipherment algorithm brings can't recover the problem of original text because of forgetting Password.In encrypt file, write down file master's file encrypted message simultaneously, as fingerprint, the convenience file master fetches password when forgetting Password: file master's file encryption encrypted message is encrypted by himself's PKI, data encrypted is put into the assigned address of file header, when the file master forgets Password, can fetch the corresponding file Crypted password by file master's oneself private key.
Four, improve fail safe: the user must use effective private key and file encryption password simultaneously when declassified document, has improved safety of files.
Five, convenience: system can both can carry out above cryptographic operation to a file according to user's actual needs, also can carry out above cryptographic operation to a file, and was very convenient.
Embodiment
Below in conjunction with embodiment and accompanying drawing, the present invention is described in further detail, but embodiments of the present invention are not limited thereto.
Embodiment one
A kind of file encryption and distribution method based on digital certificate as shown in Figure 2, may further comprise the steps:
S1, file master initialization operational factor, the file encryption password that uses when comprising encrypt file, file master's PKI, and file recipient's PKI enter step S2;
The number that S2, file master add up the encrypt file preserver, this numerical value is written in preceding 4 bytes of new files 1, wherein encrypt file preserver's number comprises that file recipient and file master self (for example wish to send to user B, C behind file of user A encryption, user A self also preserves this encrypt file simultaneously, then the value at this place is 3), enter step S3;
S3, file instigator use the PKI of oneself, by RSA cryptographic algorithms, the file encryption password string are encrypted, and obtain file master's file encrypted message; The file master obtains the length of file encrypted message character string, length value is written in next 4 bytes of new file 1, enters step S4;
S4, file chief commander file encrypted message are appended in the new file 1, enter step S5;
S5, file instigator use file recipient's PKI, by RSA Algorithm, the file encryption password string are encrypted, obtain file recipient's file encrypted message, obtain this document encrypted message string length, length value is appended in new file 1 ensuing 4 bytes, enter step S6;
S6, file chief commander file encrypted message are appended in the new file 1, enter step S7;
S7, file master judge whether to generate all files recipient's file encrypted message, if then enter poly-S8 of step; If not, return step S5;
S8, file master are the length value that unit obtains source file with the byte, calculate the mould of source file length value and 16, and the result is appended in new file 1 ensuing 4 bytes, enter step S9;
S9, file master judge that whether source file length is 0, if be not 0, then enters step S10; If be 0, then jump to step S13;
S10, file master read preceding 16 byte datas of source file, when not enough 16 bytes of data, replenish specific data, the file encryption password that provides among the step S1 is provided, by the AES cryptographic algorithm this 16 byte data is encrypted, the result after encrypting is appended in the new file 1, enter step S11;
S11, file master judge whether untreated residue file size is 0 in the source file, if be not 0, then enters step S12; If be 0, then jump to step S13;
S12, read ensuing 16 byte datas, when not enough 16 bytes of data, replenish specific data, the file encryption password that provides among the step S1 is provided, by the AES cryptographic algorithm this 16 byte data is encrypted, the result after encrypting is appended in the new file 1, be back to step S11;
S13, file master ends file are encrypted, and each file recipient is preserved and sent to new file 1.
Among above-mentioned steps S10 and the step S12, described additional specific data is meant that specifically making its length in the terminal zero padding of data just in time is 16 bytes.Because in S8, write down the physical length of last group of source file, when decryption oprerations, can add according to the deletion of the physical length of last group of source file those zero.
The form of encrypt file in the present embodiment one is shown in Table 1:
● encrypt file preserver number: length is 4 bytes, the number that is used for the encrypted file preserver, comprise file recipient and file master self, for example user A wishes to send to user B, C after encrypting a file, user A self also preserves this encrypt file simultaneously, and then the value at this place is 3.
● file master's file encrypted message length: length is 4 bytes, be used to preserve file master's file encrypted message string length, above-mentioned said file master's file encrypted message is that the PKI by the file master carries out obtaining after RSA cryptographic algorithms is encrypted to the file encryption password.
● file master's file encrypted message: length is determined by the value that last item provides, is used to preserve file master's file encrypted message.
● recipient 1 file encrypted message length: length is 4 bytes, be used to preserve file recipient 1 file encrypted message string length, above-mentioned said file recipient's 1 file encrypted message is that the PKI by file recipient 1 carries out obtaining after RSA cryptographic algorithms is encrypted to the file encryption password.
● recipient 1 file encrypted message: length is determined by the value that last item provides, is used to preserve file recipient 1 file encrypted message.
● recipient 2 file encrypted message length: length is 4 bytes, be used to preserve file recipient 2 file encrypted message string length, above-mentioned said file recipient's 2 file encrypted message is that the PKI by file recipient 2 carries out obtaining after RSA cryptographic algorithms is encrypted to the file encryption password.
● recipient 2 file encrypted message: length is determined by the value that last item provides, is used to preserve file recipient 2 file encrypted message.
●……
●……
● the file encrypted message length of recipient n: length is 4 bytes, be used to preserve the file encrypted message string length of file recipient n, the file encrypted message of above-mentioned said file recipient n is that the PKI by file recipient n carries out obtaining after RSA cryptographic algorithms is encrypted to the file encryption password.
● the file encrypted message of recipient n: length is determined by the value that last item provides, is used to preserve the file encrypted message of file recipient n.
● last group figure place of block encryption: length is 4 bytes, is used to write down the residue figure place (for the mould of source file length value and 16) of last group when source file carried out block encryption, if source file length just is the integral multiple of 16 bytes, should place's value be 0 then.
Body part: when the source file content was empty, this part was empty; When source file was not empty, this part was used to preserve the file content after the encryption, is meant that specifically with 16 bytes be one group, uses the file encryption password by the AES cryptographic algorithm each group to be encrypted, and successively the content after encrypting is written to here and preserves.If last organizes not enough 16 bytes, it just in time is to encrypt after 16 bytes that then terminal zero padding makes its length again.
Based on the file encryption and the distribution method of above-mentioned digital certificate, the file recipient receives new file 1 and is decrypted, and reads file content, specifically may further comprise the steps:
S20, file recipient receive new file 1, are required input file recipient's private key and file decryption password, enter step S21;
S21, file recipient read preceding 4 bytes in the new file 1, learn that total n+1 people can read this document, and promptly this document head includes n+1 file encrypted message, enters step S22;
S22, read new file 1 ensuing 4 byte datas, this data value is represented the string length of ensuing file encrypted message, enters step S23;
S23, according to the data value of reading among the step S22, in new file 1, then reading the data (these data are the file encrypted message) of this data value length, and storage; Enter step S24;
S24, file recipient use the private key of oneself to attempt by RSA Algorithm file encryption information being decrypted, if decrypted result is consistent with the file decryption password of file recipient input, then this document recipient obtains the file decryption password, enters step S25; If deciphering is unsuccessful, then the file recipient further judges whether to have read n+1 file encrypted message, if then enter step S29; If not, then return step S22;
S25, file recipient judge whether to read n+1 file encrypted message, if then enter step S27; If not, then enter step S26;
S26, file recipient read ensuing 4 byte datas in the new file 1, according to this data value, then read the data of this data value length in new file 1, and return step S25;
S27, file recipient read ensuing 4 byte datas in the new file 1, and step S28 is stored and entered to the residue figure place of last group when this data representation source file carried out block encryption;
S28, file recipient then read the remaining data in the new file 1, are one group with 16 bytes in order, and remaining data is divided into groups, and use the file decryption password that obtains among the step S24 successively grouped data to be decrypted by aes algorithm, and preserve; Wherein behind last component group data decryption,, preserve the data of corresponding data length, finally obtain the source file content, enter step S29 according to the residue figure place among the step S27;
S29, end operation.
Based on the file encryption and the distribution method of above-mentioned digital certificate, the file master self preserves new file 1 and is decrypted, and reads file content, specifically may further comprise the steps:
S31, file master read preceding 4 bytes in the new file 1, learn that total n+1 people can read this document, and promptly this document head includes n+1 file encrypted message, enters step S32;
S32, read new file 1 ensuing 4 byte datas, this data value is represented the string length of ensuing file encrypted message, enters step S33;
S33, according to the data value of reading among the step S32, in new file 1, then reading the data (these data are the file encrypted message) of this data value length, and storage, enter step S34;
Whether S34, file master remember the file decryption password, if forget, then the file instigator is decrypted the file encrypted message by RSA Algorithm with the private key of oneself, obtains the file decryption password, and enters step S35; If the file master remembers the file decryption password, then enter step S35;
S35, file master read ensuing 4 byte datas in the new file 1, according to this data value, then read the data of this data value length in new file 1, and enter step S36;
S36, file master judge whether to read n+1 file encrypted message, if then enter step S37; If not, then return step S35;
S37, file master read ensuing 4 byte datas in the new file 1, and step S38 is stored and entered to the residue figure place of last group when this data representation source file carried out block encryption;
S38, file master then read the remaining data in the new file 1, are one group with 16 bytes in order, and remaining data is divided into groups, and use the file decryption password that obtains among the step S34 successively grouped data to be decrypted by aes algorithm, and preserve; Wherein behind last component group data decryption,, preserve the data of corresponding data length, finally obtain the source file content, end operation according to the residue figure place among the step S37.
Embodiment two
A kind of file encryption and distribution method based on digital certificate may further comprise the steps:
S1, file master initialization operational factor, file encryption password that uses when comprising encrypt file and file recipient's PKI enter step S2;
The number that S2, file master add up the encrypt file recipient is written to this numerical value in preceding 4 bytes of new files 2, and (for example user A wishes to send to user B, C after encrypting a file, and then the value at this place is 2) enters step S3;
S3, file instigator use file recipient's PKI, pass through RSA cryptographic algorithms, the file encryption password string is encrypted, obtain file recipient's file encrypted message, obtain this document encrypted message string length, length value is appended in new file 2 ensuing 4 bytes, enters step S4;
S4, file chief commander file encrypted message are appended in the new file 2, enter step S5;
S5, file master judge whether to generate all files recipient's file encrypted message, if then enter step S6; If not, return step S3;
S6, file master are the length value that unit obtains source file with the byte, calculate the mould of source file length value and 7, and the result is appended in new file 2 ensuing 4 bytes, enter step S7;
S7, file master judge that whether source file length is 0, if be not 0, then enters step S8; If be 0, then jump to step S11;
S8, file master read preceding 7 byte datas of source file, when not enough 7 bytes of data, replenish specific data, the file encryption password that provides among the step S1 is provided, by the des encryption algorithm this 7 byte data is encrypted, the result after encrypting is appended in the new file 2, enter step S9;
S9, file master judge whether untreated residue file size is 0 in the source file, if be not 0, then enters step S10; If be 0, then jump to step S11;
S10, read ensuing 7 byte datas, when not enough 7 bytes of data, replenish specific data, the file encryption password that provides among the step S1 is provided, by the des encryption algorithm this 7 byte data is encrypted, the result after encrypting is appended in the new file 2, be back to step S9;
S11, file master ends file are encrypted, and new file 2 is sent to each file recipient.
Among above-mentioned steps S8 and the step S10, described additional specific data, be meant that specifically making its length in the terminal zero padding of data just in time is 7 bytes, because in S6, write down the physical length of last group of source file, be decrypted when operation algorithm can add according to the physical length deletion of last group of source file those zero.
The form of encrypt file in the present embodiment two is shown in Table 2:
● encrypt file recipient number: length is 4 bytes, is used for encrypted file recipient's number, and for example user A wishes to send to user B, C after encrypting a file, and then the value at this place is 2.
● recipient 1 file encrypted message length: length is 4 bytes, be used to preserve file recipient 1 file encrypted message string length, above-mentioned said file recipient's 1 file encrypted message is that the PKI by file recipient 1 carries out obtaining after RSA cryptographic algorithms is encrypted to the file encryption password.
● recipient 1 file encrypted message: length is determined by the value that last item provides, is used to preserve file recipient 1 file encrypted message.
● recipient 2 file encrypted message length: length is 4 bytes, be used to preserve file recipient 2 file encrypted message string length, above-mentioned said file recipient's 2 file encrypted message is that the PKI by file recipient 2 carries out obtaining after RSA cryptographic algorithms is encrypted to the file encryption password.
● recipient 2 file encrypted message: length is determined by the value that last item provides, is used to preserve file recipient 2 file encrypted message.
●……
●……
● the file encrypted message length of recipient n: length is 4 bytes, be used to preserve the file encrypted message string length of file recipient n, the file encrypted message of above-mentioned said file recipient n is that the PKI by file recipient n carries out obtaining after RSA cryptographic algorithms is encrypted to the file encryption password.
● the file encrypted message of recipient n: length is determined by the value that last item provides, is used to preserve the file encrypted message of file recipient n.
● last group figure place of block encryption: length is 4 bytes, is used to write down the residue figure place (for the mould of source file length value and 7) of last group when source file carried out block encryption, if source file length just is the integral multiple of 7 bytes, should place's value be 0 then.
Body part: when the source file content was empty, this part was empty; When source file was not empty, this part was used to preserve the file content after the encryption, is meant that specifically with 7 bytes be one group, uses the file encryption password by the des encryption algorithm each group to be encrypted, and successively the content after encrypting is written to here and preserves.If last organizes not enough 7 bytes, it just in time is to encrypt after 7 bytes that then terminal zero padding makes its length again.
Based on the file encryption and the distribution method of above-mentioned digital certificate, the file recipient receives new file 2 and is decrypted, and reads file content, specifically may further comprise the steps:
S20, file recipient receive new file 2, are required input file recipient's private key and file decryption password, enter step S21;
S21, file recipient read preceding 4 bytes in the new file 2, learn that total n people can read this document, and promptly this document head includes n file encrypted message, enters step S22;
S22, read new file 2 ensuing 4 byte datas, this data value is represented the string length of ensuing file encrypted message, enters step S23;
S23, according to the data value of reading among the step S22, in new file 2, then reading the data (these data are the file encrypted message) of this data value length, and storage; Enter step S24;
S24, file recipient use the private key of oneself to attempt by RSA Algorithm file encryption information being decrypted, if decrypted result is consistent with the file decryption password of file recipient input, then this document recipient obtains the file decryption password, enters step S25; If deciphering is unsuccessful, then the file recipient further judges whether to have read n file encrypted message, if then jump to step S29; If not, then return step S22;
S25, file recipient judge whether to read n file encrypted message, if then enter step S27; If not, then enter step S26;
S26, file recipient read ensuing 4 byte datas in the new file 2, according to this data value, then read the data of this data value length in new file 2, and return step S25;
S27, file recipient read ensuing 4 byte datas in the new file 2, and step S28 is stored and entered to the residue figure place of last group when this data representation source file carried out block encryption;
S28, file recipient then read the remaining data in the new file 2, are one group with 7 bytes in order, and remaining data is divided into groups, and use the file decryption password that obtains among the step S24 successively grouped data to be decrypted by the DES algorithm, and preserve; Wherein behind last component group data decryption,, preserve the data of corresponding data length, finally obtain the source file content, enter step S29 according to the residue figure place among the step S27;
S29, end operation.
Aes algorithm can use DES, 3DES, RC4, RC5, Blowfish or other symmetric encipherment algorithm to substitute in the above process, and RSA Algorithm can substitute with ECC or other rivest, shamir, adelman.
The foregoing description is a preferred implementation of the present invention; but embodiments of the present invention are not limited by the examples; other any do not deviate from change, the modification done under spirit of the present invention and the principle, substitutes, combination, simplify; all should be the substitute mode of equivalence, be included within protection scope of the present invention.