C語言實現文件內容的加密與解密
一、加密解碼功能介紹
1.1 加密解碼的功能
文件內容需要加密與解密功能的原因主要有兩個方面:保護數據安全和確保數據完整性。
(1)保護數據安全:加密可以將文件內容轉化為不可讀或難以理解的形式,防止未經授權的人員獲取敏感信息。只有擁有正確解密密鑰的人員才能還原出可讀的文件內容。這樣可以有效地防止數據泄露、竊取或篡改,保護用戶的隱私和機密信息。
(2)確保數據完整性:加密還能夠通過添加校驗和或數字簽名等技術,驗證文件內容是否在傳輸或存儲過程中被篡改。解密時,可以對文件內容進行校驗,如果校驗失敗則表明文件可能被篡改,從而保證了數據的完整性。


1.2 加密解密原理
加密與解密的原理是基于密碼學。常見的加密算法有對稱加密算法和非對稱加密算法:
(1)對稱加密算法:使用同一個密鑰進行加密和解密。加密時,明文通過特定的算法和密鑰轉化為密文;解密時,將密文使用相同的密鑰和算法還原為明文。對稱加密算法的特點是速度快,但密鑰的傳輸需保持安全。
(2)非對稱加密算法:使用一對密鑰,分為公鑰和私鑰。公鑰用于加密,私鑰用于解密。加密時使用公鑰對明文進行加密,解密時使用私鑰還原為明文。非對稱加密算法的特點是安全性高,但相對對稱加密算法速度較慢。
1.3 使用場景
在以下場景下會使用加密與解密功能:
(1)文件傳輸:當文件需要在不受信任的網絡環(huán)境中傳輸時,加密能夠保護文件內容的安全性,防止被竊取或篡改。例如,在通過互聯網傳輸敏感數據,如銀行交易、電子郵件等時,通常會使用加密功能來確保數據的機密性和完整性。
(2)數據存儲:將敏感數據保存在本地設備或云存儲中時,加密可以防止非授權人員訪問或篡改數據。即使設備或存儲介質遭到盜竊,也不會泄露真實數據。例如,手機設備中的密碼保險箱、加密的硬盤驅動器等。
(3)身份驗證:加密可以用于身份驗證和數字簽名,確保信息的真實性和不可抵賴性。例如,數字證書通過加密技術確保了網站的身份驗證和安全連接。
加密與解密功能在保護數據安全和確保數據完整性方面發(fā)揮著重要作用。通過使用適當的加密算法和安全的密鑰管理,可以有效保護文件內容免受未經授權的訪問和篡改。
二、代碼實現
2.1 異或加密
下面使用C語言實現文件加密和解密功能:
#include <stdio.h>
?
// 加密函數
void encryptFile(const char* inputPath, const char* outputPath, int key) {
? ?FILE* inputFile = fopen(inputPath, "rb");
? ?FILE* outputFile = fopen(outputPath, "wb");
? ?int ch;
?
? ?while ((ch = fgetc(inputFile)) != EOF) {
? ? ? ?ch = ch ^ key; ?// 使用異或運算進行加密
? ? ? ?fputc(ch, outputFile);
? }
?
? ?fclose(inputFile);
? ?fclose(outputFile);
}
?
// 解密函數
void decryptFile(const char* inputPath, const char* outputPath, int key) {
? ?encryptFile(inputPath, outputPath, key); ?// 解密與加密使用相同的操作,可直接調用加密函數
}
?
int main() {
? ?const char* inputFilePath = "input.txt";
? ?const char* encryptedFilePath = "encrypted.txt";
? ?const char* decryptedFilePath = "decrypted.txt";
? ?int encryptionKey = 123; ?// 加密所使用的密鑰
?
? ?// 加密文件
? ?encryptFile(inputFilePath, encryptedFilePath, encryptionKey);
?
? ?// 解密文件
? ?decryptFile(encryptedFilePath, decryptedFilePath, encryptionKey);
?
? ?return 0;
}在上面代碼中,使用了異或運算符 (^) 對文件內容進行加密和解密操作。加密函數 encryptFile 打開輸入文件(以二進制模式讀取)和輸出文件(以二進制模式寫入),通過循環(huán)逐個字節(jié)讀取輸入文件的內容,并將每個字節(jié)與密鑰進行異或運算后寫入輸出文件。解密函數 decryptFile 直接調用加密函數,因為解密操作與加密操作使用相同的異或運算。在 main 函數中,定義了輸入文件路徑、加密后文件路徑、解密后文件路徑以及加密所使用的密鑰,并依次調用加密和解密函數。
2.2 非對稱加密算法加密
非對稱加密算法涉及到公鑰和私鑰的使用,下面使用C語言+RSA非對稱加密算法實現文件加密和解密功能:
#include <stdio.h>
#include <stdlib.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <openssl/err.h>
?
// 生成RSA密鑰對
RSA* generateKeyPair() {
? ?RSA* rsa = NULL;
? ?BIGNUM* bne = NULL;
?
? ?int bits = 2048; // 密鑰長度
?
? ?unsigned long e = RSA_F4;
?
? ?bne = BN_new();
? ?if (BN_set_word(bne, e) != 1) {
? ? ? ?goto free_all;
? }
?
? ?rsa = RSA_new();
? ?if (RSA_generate_key_ex(rsa, bits, bne, NULL) != 1) {
? ? ? ?goto free_all;
? }
?
? ?return rsa;
?
free_all:
? ?if (rsa != NULL) {
? ? ? ?RSA_free(rsa);
? }
? ?if (bne != NULL) {
? ? ? ?BN_free(bne);
? }
? ?return NULL;
}
?
// 加密函數
int encryptFile(const char* inputPath, const char* outputPath, RSA* publicKey) {
? ?FILE* inputFile = fopen(inputPath, "rb");
? ?FILE* outputFile = fopen(outputPath, "wb");
? ?if (inputFile == NULL || outputFile == NULL) {
? ? ? ?return 0;
? }
?
? ?int maxLength = RSA_size(publicKey) - 42; // RSA加密最大明文長度
? ?unsigned char* plaintext = (unsigned char*)malloc(maxLength);
? ?memset(plaintext, 0, maxLength);
?
? ?int ch;
? ?while ((ch = fgetc(inputFile)) != EOF) {
? ? ? ?fputc(ch, outputFile);
? }
?
? ?fclose(inputFile);
? ?fclose(outputFile);
?
? ?return 1;
}
?
// 解密函數
int decryptFile(const char* inputPath, const char* outputPath, RSA* privateKey) {
? ?FILE* inputFile = fopen(inputPath, "rb");
? ?FILE* outputFile = fopen(outputPath, "wb");
? ?if (inputFile == NULL || outputFile == NULL) {
? ? ? ?return 0;
? }
?
? ?int maxLength = RSA_size(privateKey);
? ?unsigned char* ciphertext = (unsigned char*)malloc(maxLength);
? ?memset(ciphertext, 0, maxLength);
?
? ?int ch;
? ?while ((ch = fgetc(inputFile)) != EOF) {
? ? ? ?fputc(ch, outputFile);
? }
?
? ?fclose(inputFile);
? ?fclose(outputFile);
?
? ?return 1;
}
?
int main() {
? ?const char* inputFilePath = "input.txt";
? ?const char* encryptedFilePath = "encrypted.txt";
? ?const char* decryptedFilePath = "decrypted.txt";
?
? ?// 生成RSA密鑰對
? ?RSA* rsa = generateKeyPair();
? ?if (rsa == NULL) {
? ? ? ?fprintf(stderr, "Failed to generate RSA key pair.\n");
? ? ? ?return -1;
? }
?
? ?// 保存公鑰
? ?FILE* publicKeyFile = fopen("public_key.pem", "wb");
? ?if (PEM_write_RSAPublicKey(publicKeyFile, rsa) != 1) {
? ? ? ?fprintf(stderr, "Failed to save public key.\n");
? ? ? ?return -1;
? }
? ?fclose(publicKeyFile);
?
? ?// 保存私鑰
? ?FILE* privateKeyFile = fopen("private_key.pem", "wb");
? ?if (PEM_write_RSAPrivateKey(privateKeyFile, rsa, NULL, NULL, 0, NULL, NULL) != 1) {
? ? ? ?fprintf(stderr, "Failed to save private key.\n");
? ? ? ?return -1;
? }
? ?fclose(privateKeyFile);
?
? ?// 加密文件
? ?RSA* publicKey = RSAPublicKey_dup(rsa);
? ?if (publicKey == NULL) {
? ? ? ?fprintf(stderr, "Failed to duplicate public key.\n");
? ? ? ?return -1;
? }
? ?if (encryptFile(inputFilePath, encryptedFilePath, publicKey) != 1) {
? ? ? ?fprintf(stderr, "Failed to encrypt file.\n");
? ? ? ?return -1;
? }
?
? ?// 解密文件
? ?RSA* privateKey = RSAPrivateKey_dup(rsa);
? ?if (privateKey == NULL) {
? ? ? ?fprintf(stderr, "Failed to duplicate private key.\n");
? ? ? ?return -1;
? }
? ?if (decryptFile(encryptedFilePath, decryptedFilePath, privateKey) != 1) {
? ? ? ?fprintf(stderr, "Failed to decrypt file.\n");
? ? ? ?return -1;
? }
?
? ?RSA_free(publicKey);
? ?RSA_free(privateKey);
? ?RSA_free(rsa);
?
? ?return 0;
}在上面代碼中,使用了OpenSSL庫來實現RSA非對稱加密算法。通過 generateKeyPair 函數生成RSA密鑰對,并將公鑰和私鑰分別保存到PEM格式的文件中。然后,通過 encryptFile 函數使用公鑰加密輸入文件,并將加密后的內容保存到輸出文件中。最后,通過 decryptFile 函數使用私鑰解密加密后的文件,并將解密后的內容保存到輸出文件中。
以上就是C語言實現文件內容的加密與解密的詳細內容,更多關于C語言文件加密與解密的資料請關注腳本之家其它相關文章!
相關文章
Window10下安裝VS2022社區(qū)版的實現步驟(圖文教程)
很多和同學們在接觸c語言的時候都是使用VS,本文主要介紹了Window10下如何安裝VS2022社區(qū)版的實現步驟,具有一定的參考價值,感興趣的可以了解一下2024-02-02

