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


1.2 加密解密原理
加密與解密的原理是基于密碼學。常見的加密算法有對稱加密算法和非對稱加密算法:
(1)對稱加密算法:使用同一個密鑰進行加密和解密。加密時,明文通過特定的算法和密鑰轉(zhuǎn)化為密文;解密時,將密文使用相同的密鑰和算法還原為明文。對稱加密算法的特點是速度快,但密鑰的傳輸需保持安全。
(2)非對稱加密算法:使用一對密鑰,分為公鑰和私鑰。公鑰用于加密,私鑰用于解密。加密時使用公鑰對明文進行加密,解密時使用私鑰還原為明文。非對稱加密算法的特點是安全性高,但相對對稱加密算法速度較慢。
1.3 使用場景
在以下場景下會使用加密與解密功能:
(1)文件傳輸:當文件需要在不受信任的網(wǎng)絡(luò)環(huán)境中傳輸時,加密能夠保護文件內(nèi)容的安全性,防止被竊取或篡改。例如,在通過互聯(lián)網(wǎng)傳輸敏感數(shù)據(jù),如銀行交易、電子郵件等時,通常會使用加密功能來確保數(shù)據(jù)的機密性和完整性。
(2)數(shù)據(jù)存儲:將敏感數(shù)據(jù)保存在本地設(shè)備或云存儲中時,加密可以防止非授權(quán)人員訪問或篡改數(shù)據(jù)。即使設(shè)備或存儲介質(zhì)遭到盜竊,也不會泄露真實數(shù)據(jù)。例如,手機設(shè)備中的密碼保險箱、加密的硬盤驅(qū)動器等。
(3)身份驗證:加密可以用于身份驗證和數(shù)字簽名,確保信息的真實性和不可抵賴性。例如,數(shù)字證書通過加密技術(shù)確保了網(wǎng)站的身份驗證和安全連接。
加密與解密功能在保護數(shù)據(jù)安全和確保數(shù)據(jù)完整性方面發(fā)揮著重要作用。通過使用適當?shù)募用芩惴ê桶踩拿荑€管理,可以有效保護文件內(nèi)容免受未經(jīng)授權(quán)的訪問和篡改。
二、代碼實現(xiàn)
2.1 異或加密
下面使用C語言實現(xiàn)文件加密和解密功能:
#include <stdio.h>
?
// 加密函數(shù)
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);
}
?
// 解密函數(shù)
void decryptFile(const char* inputPath, const char* outputPath, int key) {
? ?encryptFile(inputPath, outputPath, key); ?// 解密與加密使用相同的操作,可直接調(diào)用加密函數(shù)
}
?
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;
}在上面代碼中,使用了異或運算符 (^) 對文件內(nèi)容進行加密和解密操作。加密函數(shù) encryptFile 打開輸入文件(以二進制模式讀?。┖洼敵鑫募ㄒ远M制模式寫入),通過循環(huán)逐個字節(jié)讀取輸入文件的內(nèi)容,并將每個字節(jié)與密鑰進行異或運算后寫入輸出文件。解密函數(shù) decryptFile 直接調(diào)用加密函數(shù),因為解密操作與加密操作使用相同的異或運算。在 main 函數(shù)中,定義了輸入文件路徑、加密后文件路徑、解密后文件路徑以及加密所使用的密鑰,并依次調(diào)用加密和解密函數(shù)。
2.2 非對稱加密算法加密
非對稱加密算法涉及到公鑰和私鑰的使用,下面使用C語言+RSA非對稱加密算法實現(xiàn)文件加密和解密功能:
#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;
}
?
// 加密函數(shù)
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;
}
?
// 解密函數(shù)
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庫來實現(xiàn)RSA非對稱加密算法。通過 generateKeyPair 函數(shù)生成RSA密鑰對,并將公鑰和私鑰分別保存到PEM格式的文件中。然后,通過 encryptFile 函數(shù)使用公鑰加密輸入文件,并將加密后的內(nèi)容保存到輸出文件中。最后,通過 decryptFile 函數(shù)使用私鑰解密加密后的文件,并將解密后的內(nèi)容保存到輸出文件中。
以上就是C語言實現(xiàn)文件內(nèi)容的加密與解密的詳細內(nèi)容,更多關(guān)于C語言文件加密與解密的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Matlab計算變異函數(shù)并繪制經(jīng)驗半方差圖詳解
這篇文章主要為大家詳細介紹了基于MATLAB求取空間數(shù)據(jù)的變異函數(shù),并繪制經(jīng)驗半方差圖的方法。文中的示例代碼講解詳細,感興趣的可以了解一下2023-04-04
Window10下安裝VS2022社區(qū)版的實現(xiàn)步驟(圖文教程)
很多和同學們在接觸c語言的時候都是使用VS,本文主要介紹了Window10下如何安裝VS2022社區(qū)版的實現(xiàn)步驟,具有一定的參考價值,感興趣的可以了解一下2024-02-02
C++中volatile和mutable關(guān)鍵字用法詳解
這篇文章主要介紹了C++中volatile和mutable關(guān)鍵字用法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02
C++?primer超詳細講解關(guān)聯(lián)容器
兩個主要的關(guān)聯(lián)容器為map和set,map中元素是一些關(guān)鍵字—值對,關(guān)鍵字起索引的作用,值則表示與索引相關(guān)聯(lián)的數(shù)據(jù)。set中每個元素只包含一個關(guān)鍵字,set支持高效的關(guān)鍵字查詢操作——檢查一個給定關(guān)鍵字是否在set中2022-07-07

