÷è÷ë²Ù×÷ϵͳÖеÄÎļþ¼ÓÃÜÏ¢ÕùÃÜÔõÑù±£»¤ÄãµÄÒþ˽£¿
÷è÷ë²Ù×÷ϵͳÖеÄÎļþ¼ÓÃÜÏ¢ÕùÃÜÔõÑù±£»¤ÄãµÄÒþ˽£¿
Ëæ×ÅÐÅÏ¢ÊÖÒÕµÄÉú³¤£¬ÎÒÃǵÄ˽ÈËÐÅÏ¢Ô½À´Ô½ÈÝÒ×Êܵ½Ð¹Â¶ºÍÇÖÕ¼¡£ÎªÁ˱£»¤ÎÒÃǵÄÒþ˽£¬Îļþ¼ÓÃÜÏ¢ÕùÃܳÉΪÁËÒ»ÖÖ³£ÓõÄÊֶΡ£ÔÚ÷è÷ë²Ù×÷ϵͳÖУ¬ÎÒÃÇ¿ÉÒÔʹÓÃÆäÌṩµÄÎļþ¼ÓÃÜÏ¢ÕùÃܹ¦Ð§À´±£»¤×Ô¼ºµÄÒþ˽ºÍÃô¸ÐÊý¾Ý¡£±¾ÎĽ«ÏÈÈÝ÷è÷ë²Ù×÷ϵͳÖеÄÎļþ¼ÓÃÜÏ¢ÕùÃܹ¦Ð§£¬²¢¸ø³öÏìÓ¦µÄ´úÂëʾÀý¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒªÏàʶ÷è÷ë²Ù×÷ϵͳÌṩµÄÎļþ¼ÓÃÜÏ¢ÕùÃܽӿڡ£÷è÷ë²Ù×÷ϵͳÌṩÁËÒ»Ì×Îļþ¼ÓÃÜÏ¢ÕùÃܿ⣬°üÀ¨Á˳£ÓõļÓÃÜË㷨ϢÕùÃÜËã·¨¡£ÎÒÃÇ¿ÉÒÔͨ¹ýŲÓÃÕâЩ¿âÖеĺ¯ÊýÀ´ÊµÏÖÎļþµÄ¼ÓÃÜÏ¢ÕùÃÜ¡£ÏÂÃæÊÇÒ»¸ö¼òÆӵļÓÃܺ¯ÊýʾÀý£º
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/evp.h> void encrypt_file(const char *input_file, const char *output_file, const char *key) { EVP_CIPHER_CTX *ctx; FILE *input, *output; unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; int outlen, len, total = 0; // ³õʼ»¯¼ÓÃÜÇéÐÎ ctx = EVP_CIPHER_CTX_new(); EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, NULL); // ·¿ªÊäÈëÎļþ input = fopen(input_file, "rb"); if (!input) { fprintf(stderr, "Failed to open input file: %s ", input_file); return; } // ·¿ªÊä³öÎļþ output = fopen(output_file, "wb"); if (!output) { fprintf(stderr, "Failed to open output file: %s ", output_file); fclose(input); return; } // Öð¿é¼ÓÃÜÊý¾Ý while ((len = fread(inbuf, 1, sizeof(inbuf), input)) > 0) { EVP_EncryptUpdate(ctx, outbuf, &outlen, inbuf, len); fwrite(outbuf, 1, outlen, output); total += outlen; } // ¿¢ÊÈÎÃüÜÀú³Ì EVP_EncryptFinal_ex(ctx, outbuf, &outlen); fwrite(outbuf, 1, outlen, output); total += outlen; // ÕûÀíÊÂÇé fclose(input); fclose(output); EVP_CIPHER_CTX_free(ctx); printf("Encryption finished. Encrypted %d bytes. ", total); } int main() { const char *input_file = "plain.txt"; const char *output_file = "encrypted.txt"; const char *key = "abcdefghijklmnop"; // 16×Ö½ÚµÄÃÜÔ¿ encrypt_file(input_file, output_file, key); return 0; }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÑÝʾÁËÔõÑùʹÓÃ÷è÷ë²Ù×÷ϵͳÖеÄÎļþ¼ÓÃܽӿڽ«Ò»¸öÎļþ¼ÓÃܳÉÁíÒ»¸öÎļþ¡£ÎÒÃÇÊ×ÏÈÐèÒª·¿ªÊäÈëÎļþºÍÊä³öÎļþ£¬È»ºóʹÓÃÖ¸¶¨µÄÃÜÔ¿¶ÔÊäÈëÎļþ¾ÙÐмÓÃÜ£¬²¢½«Ð§¹ûдÈëÊä³öÎļþÖС£×îºó£¬ÎÒÃÇÐèÒªÕûÀíÏà¹Ø×ÊÔ´£¬²¢Êä³ö¼ÓÃܵÄ×Ü×Ö½ÚÊý¡£ÐèҪעÖصÄÊÇ£¬ÃÜÔ¿µÄ³¤¶ÈÐèÒªÖª×ã¼ÓÃÜËã·¨µÄÒªÇó¡£
³ýÁËÎļþ¼ÓÃÜ£¬÷è÷ë²Ù×÷ϵͳ»¹ÌṩÁËÎļþ½âÃܵĹ¦Ð§¡£ÏÂÃæÊÇÒ»¸ö¼òÆӵĽâÃܺ¯ÊýʾÀý£º
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <openssl/evp.h> void decrypt_file(const char *input_file, const char *output_file, const char *key) { EVP_CIPHER_CTX *ctx; FILE *input, *output; unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH]; int outlen, len, total = 0; // ³õʼ»¯½âÃÜÇéÐÎ ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, NULL); // ·¿ªÊäÈëÎļþ input = fopen(input_file, "rb"); if (!input) { fprintf(stderr, "Failed to open input file: %s ", input_file); return; } // ·¿ªÊä³öÎļþ output = fopen(output_file, "wb"); if (!output) { fprintf(stderr, "Failed to open output file: %s ", output_file); fclose(input); return; } // Öð¿é½âÃÜÊý¾Ý while ((len = fread(inbuf, 1, sizeof(inbuf), input)) > 0) { EVP_DecryptUpdate(ctx, outbuf, &outlen, inbuf, len); fwrite(outbuf, 1, outlen, output); total += outlen; } // ¿¢Ê½âÃÜÀú³Ì EVP_DecryptFinal_ex(ctx, outbuf, &outlen); fwrite(outbuf, 1, outlen, output); total += outlen; // ÕûÀíÊÂÇé fclose(input); fclose(output); EVP_CIPHER_CTX_free(ctx); printf("Decryption finished. Decrypted %d bytes. ", total); } int main() { const char *input_file = "encrypted.txt"; const char *output_file = "plain.txt"; const char *key = "abcdefghijklmnop"; // 16×Ö½ÚµÄÃÜÔ¿ decrypt_file(input_file, output_file, key); return 0; }
µÇ¼ºó¸´ÖÆ
ÉÏÃæµÄ´úÂëÑÝʾÁËÔõÑùʹÓÃ÷è÷ë²Ù×÷ϵͳÖеÄÎļþ½âÃܽӿڽ«Ò»¸ö¼ÓÃܵÄÎļþ½âÃܳÉÔʼÎļþ¡£ÎÒÃÇÊ×ÏÈÐèÒª·¿ªÊäÈëÎļþºÍÊä³öÎļþ£¬È»ºóʹÓÃÖ¸¶¨µÄÃÜÔ¿¶ÔÊäÈëÎļþ¾ÙÐнâÃÜ£¬²¢½«Ð§¹ûдÈëÊä³öÎļþÖС£×îºó£¬ÎÒÃÇÐèÒªÕûÀíÏà¹Ø×ÊÔ´£¬²¢Êä³ö½âÃܵÄ×Ü×Ö½ÚÊý¡£
ͨ¹ýÉÏÊöʾÀý´úÂ룬ÎÒÃÇ¿ÉÒÔÔÚ÷è÷ë²Ù×÷ϵͳÖÐʹÓÃÎļþ¼ÓÃÜÏ¢ÕùÃܹ¦Ð§À´±£»¤ÎÒÃǵÄÒþ˽ºÍÃô¸ÐÊý¾Ý¡£Çë×¢ÖØ£¬ÔÚÏÖʵӦÓÃÖУ¬ÎÒÃÇÐèҪעÖØÃÜÔ¿µÄÌìÉú¡¢´æ´¢ºÍÖÎÀí£¬ÒÔ¼°¼ÓÃÜËã·¨µÄÑ¡ÔñºÍ²ÎÊýÉèÖã¬À´Ìá¸ßÎļþ¼ÓÃܵÄÇå¾²ÐÔ¡£
×ܶøÑÔÖ®£¬÷è÷ë²Ù×÷ϵͳÖеÄÎļþ¼ÓÃÜÏ¢ÕùÃܹ¦Ð§ÎªÎÒÃDZ£»¤Òþ˽ÌṩÁËÀû±ãºÍ¿É¿¿µÄÊֶΡ£ÎÒÃÇ¿ÉÒÔƾ֤×Ô¼ºµÄÐèÇóºÍÏÖÕæÏàÐΣ¬ÎÞаÔËÓÃÕâЩ¹¦Ð§À´ÔöÇ¿¶ÔÃô¸ÐÊý¾ÝµÄ±£»¤¡£
ÒÔÉϾÍÊÇ÷è÷ë²Ù×÷ϵͳÖеÄÎļþ¼ÓÃÜÏ¢ÕùÃÜÔõÑù±£»¤ÄãµÄÒþ˽£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡