最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實(shí)現(xiàn)文件內(nèi)容的加密與解密

 更新時(shí)間:2023年08月16日 08:46:48   作者:DS小龍哥  
文件內(nèi)容需要加密與解密功能的原因主要有兩個(gè)方面:保護(hù)數(shù)據(jù)安全和確保數(shù)據(jù)完整性,所以接下來小編就給大家介紹一下如何通過C語言實(shí)現(xiàn)文件內(nèi)容加密與解密,需要的朋友可以參考下

一、加密解碼功能介紹

1.1 加密解碼的功能

文件內(nèi)容需要加密與解密功能的原因主要有兩個(gè)方面:保護(hù)數(shù)據(jù)安全和確保數(shù)據(jù)完整性。

(1)保護(hù)數(shù)據(jù)安全:加密可以將文件內(nèi)容轉(zhuǎn)化為不可讀或難以理解的形式,防止未經(jīng)授權(quán)的人員獲取敏感信息。只有擁有正確解密密鑰的人員才能還原出可讀的文件內(nèi)容。這樣可以有效地防止數(shù)據(jù)泄露、竊取或篡改,保護(hù)用戶的隱私和機(jī)密信息。

(2)確保數(shù)據(jù)完整性:加密還能夠通過添加校驗(yàn)和或數(shù)字簽名等技術(shù),驗(yàn)證文件內(nèi)容是否在傳輸或存儲過程中被篡改。解密時(shí),可以對文件內(nèi)容進(jìn)行校驗(yàn),如果校驗(yàn)失敗則表明文件可能被篡改,從而保證了數(shù)據(jù)的完整性。

image-20230629103036629

image-20230629103100438

1.2 加密解密原理

加密與解密的原理是基于密碼學(xué)。常見的加密算法有對稱加密算法和非對稱加密算法:

(1)對稱加密算法:使用同一個(gè)密鑰進(jìn)行加密和解密。加密時(shí),明文通過特定的算法和密鑰轉(zhuǎn)化為密文;解密時(shí),將密文使用相同的密鑰和算法還原為明文。對稱加密算法的特點(diǎn)是速度快,但密鑰的傳輸需保持安全。

(2)非對稱加密算法:使用一對密鑰,分為公鑰和私鑰。公鑰用于加密,私鑰用于解密。加密時(shí)使用公鑰對明文進(jìn)行加密,解密時(shí)使用私鑰還原為明文。非對稱加密算法的特點(diǎn)是安全性高,但相對對稱加密算法速度較慢。

1.3 使用場景

在以下場景下會使用加密與解密功能:

(1)文件傳輸:當(dāng)文件需要在不受信任的網(wǎng)絡(luò)環(huán)境中傳輸時(shí),加密能夠保護(hù)文件內(nèi)容的安全性,防止被竊取或篡改。例如,在通過互聯(lián)網(wǎng)傳輸敏感數(shù)據(jù),如銀行交易、電子郵件等時(shí),通常會使用加密功能來確保數(shù)據(jù)的機(jī)密性和完整性。

(2)數(shù)據(jù)存儲:將敏感數(shù)據(jù)保存在本地設(shè)備或云存儲中時(shí),加密可以防止非授權(quán)人員訪問或篡改數(shù)據(jù)。即使設(shè)備或存儲介質(zhì)遭到盜竊,也不會泄露真實(shí)數(shù)據(jù)。例如,手機(jī)設(shè)備中的密碼保險(xiǎn)箱、加密的硬盤驅(qū)動器等。

(3)身份驗(yàn)證:加密可以用于身份驗(yàn)證和數(shù)字簽名,確保信息的真實(shí)性和不可抵賴性。例如,數(shù)字證書通過加密技術(shù)確保了網(wǎng)站的身份驗(yàn)證和安全連接。

加密與解密功能在保護(hù)數(shù)據(jù)安全和確保數(shù)據(jù)完整性方面發(fā)揮著重要作用。通過使用適當(dāng)?shù)募用芩惴ê桶踩拿荑€管理,可以有效保護(hù)文件內(nèi)容免受未經(jīng)授權(quán)的訪問和篡改。

二、代碼實(shí)現(xiàn)

2.1 異或加密

下面使用C語言實(shí)現(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; ?// 使用異或運(yùn)算進(jìn)行加密
 ? ? ? ?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;
}

在上面代碼中,使用了異或運(yùn)算符 (^) 對文件內(nèi)容進(jìn)行加密和解密操作。加密函數(shù) encryptFile 打開輸入文件(以二進(jìn)制模式讀?。┖洼敵鑫募ㄒ远M(jìn)制模式寫入),通過循環(huán)逐個(gè)字節(jié)讀取輸入文件的內(nèi)容,并將每個(gè)字節(jié)與密鑰進(jìn)行異或運(yùn)算后寫入輸出文件。解密函數(shù) decryptFile 直接調(diào)用加密函數(shù),因?yàn)榻饷懿僮髋c加密操作使用相同的異或運(yùn)算。在 main 函數(shù)中,定義了輸入文件路徑、加密后文件路徑、解密后文件路徑以及加密所使用的密鑰,并依次調(diào)用加密和解密函數(shù)。

2.2 非對稱加密算法加密

非對稱加密算法涉及到公鑰和私鑰的使用,下面使用C語言+RSA非對稱加密算法實(shí)現(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庫來實(shí)現(xiàn)RSA非對稱加密算法。通過 generateKeyPair 函數(shù)生成RSA密鑰對,并將公鑰和私鑰分別保存到PEM格式的文件中。然后,通過 encryptFile 函數(shù)使用公鑰加密輸入文件,并將加密后的內(nèi)容保存到輸出文件中。最后,通過 decryptFile 函數(shù)使用私鑰解密加密后的文件,并將解密后的內(nèi)容保存到輸出文件中。

以上就是C語言實(shí)現(xiàn)文件內(nèi)容的加密與解密的詳細(xì)內(nèi)容,更多關(guān)于C語言文件加密與解密的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C++連接mysql數(shù)據(jù)庫的兩種方法小結(jié)

    C++連接mysql數(shù)據(jù)庫的兩種方法小結(jié)

    這篇文章主要介紹了C++連接mysql數(shù)據(jù)庫的兩種方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • 使用C++制作簡單的web服務(wù)器

    使用C++制作簡單的web服務(wù)器

    本文給大家分享的是使用C++簡單實(shí)現(xiàn)web服務(wù)器的代碼,雖然非常的簡陋,功能也很少,主要是為了更好的理解WEB服務(wù)器的工作原理,推薦給大家,也希望對大家能夠有所幫助。
    2015-03-03
  • OpenGL繪制貝塞爾曲線

    OpenGL繪制貝塞爾曲線

    這篇文章主要為大家詳細(xì)介紹了OpenGL繪制貝塞爾曲線,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 最新評論

    汕尾市| 比如县| 天津市| 房山区| 潮州市| 汾阳市| 和硕县| 赫章县| 宁安市| 宁河县| 公安县| 营山县| 武威市| 白城市| 肥西县| 荆州市| 淄博市| 西充县| 南雄市| 尼木县| 庆城县| 黔西县| 宝丰县| 渝中区| 佳木斯市| 兴义市| 隆子县| 会理县| 重庆市| 浮梁县| 神池县| 稻城县| 湖州市| 朝阳市| 红河县| 彭泽县| 南木林县| 富源县| 乐至县| 广宗县| 合作市|