C++ <文本文件的加密与解密>

本文介绍了一个简单的文件加密与解密程序,使用C++实现。该程序能够读取指定路径下的文件内容,并通过用户输入的密钥进行加密处理后保存到新的文件中。同样地,它也支持对已加密文件进行解密操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

未加密文本.txt

almost lover

Your fingertips against my skin 
The palm trees swaying in the wind in my chase 
You sang me Spanish lullabies 
The sweetest sadness in your eyes 
Clever trick

I never wanna see you unhappy 
I thought you want the same for me 
Goodbye my almost lover 
Goodbye my hopeless dream 
I'm trying not to think about you 
Can't you just let me be? 
So long my luckless romance 
My back is turned on you 
Should've known you'd bring me heartbreak 
Almost lovers always do

We walked along a crowded street 
You took my hand and danced with me 
In the shade 
And when you left you kissed my lips 
You told me you'd never ever forget these images 
No

I'd never wanna see you unhappy 
I thought you'd want the same for me 
Goodbye my almost lover 
Goodbye my hopeless dream 
I'm trying not to think about you 
Can't you just let me be? 
So long my luckless romance 
My back is turned on you 
Should've known you'd bring me heartbreak 
Almost lovers always do

I cannot go to the ocean 
I cannot try the streets at night 
I cannot wake up in the morning 
Without you on my mind 
So you're gonna and I'm haunted 
And I bet you are just fine 
I'll make it there 
You walk right in and out of my life

Goodbye my almost lover 
Goodbye my hopeless dream 
I'm trying not to think about you 
Can't you just let me be? 
So long my luckless romance 
My back is turned on you 
Should've known you'd bring me heartache 
Almost lovers always do

加密后文本.txt (key = 20)

u!"$()4!$+y'm$*'4z}#{y')}%(4u{u}#()4".4( }#4h|y4%u!"4)'yy(4(,u.}#{4}#4)|y4,}#x4}#4".4w|u(y4m$*4(u#
{4"y4g%u#}(|4!*!!uv}y(4h|y4(,yy)y()4(ux#y((4}#4.$*'4y.y(4W!y+y'4)'}w 
]4#y+y'4,u##u4(yy4.$*4*#|u%%.4]4)|$*
{|)4.$*4,u#)4)|y4(u"y4z$'4"y4[$$xv.y4".4u!"$()4!$+y'4[$$xv.y4".4|$%y!y((4x'yu"4];"4)'.}#{4#$)4)$4)|}# 
4uv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()4!$+y'(4u!,u.(4x$

主程序:
说明:key值等于1至20之间时程序运行正常,大于20解密后的文件格式会错乱且有些数据与源文件不符,暂时没有更好的解决办法。

#include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

void encryptFile(ifstream& inFile, ofstream& outFile, int key);
char encryptChar(char c, int key);
void declassifyFile(ifstream& inFile, ofstream& outFile, int key);
char declassifyChar(char c, int key);

int main() {
    ifstream inFile;
    inFile.open("/Users/br/Documents/C:C++/secretCode/secretGardon.txt");
    if (inFile.fail()) {
        cout << "inFile opening failed.";
        exit(EXIT_FAILURE);
    }

    ofstream outFile;
    outFile.open("/Users/br/Documents/C:C++/secretCode/encyptc.txt");
    if (outFile.fail()) {
        cout << "outFile opening failed.";
        exit(EXIT_FAILURE);
    }
    cout << "Enter a key use it a number between 1 and 100: ";
    int key;
    cin >> key;

    encryptFile(inFile, outFile, key);
    inFile.close();
    outFile.close();

    inFile.open("/Users/br/Documents/C:C++/secretCode/encyptc.txt");
    if (inFile.fail()) {
        cout << "inFile opening failed.";
        exit(EXIT_FAILURE);
    }

    outFile.open("/Users/br/Documents/C:C++/secretCode/declassify.txt");
    if (outFile.fail()) {
        cout << "outFile opening failed.";
        exit(EXIT_FAILURE);
    }

    int key1;
    int count = 5;

    do {
        cout << "Enter a key to unlock the file: ";
        cin >> key1;
        count--;

        if (key1 == key)
            declassifyFile(inFile, outFile, key);
        else
            cout << "you have " << count << " times to unlock it." << '\n';
    } while (count != 0 && key1 != key);

    inFile.close();
    outFile.close();

    return 0;
}

void encryptFile(ifstream& inFile, ofstream& outFile, int key) {
    char c, ec;
    while (! inFile.eof()) {
        inFile.get(c);
        ec = encryptChar(c, key);
        outFile.put(ec);
    }
}

char encryptChar(char c, int key) {
    char enChar;

    if (c + key > 126)
        enChar = 32 + ((c + key) - 127);
    else
        enChar = c + key;

    return enChar;
}

void declassifyFile(ifstream& inFile, ofstream& outFile, int key) {
    char c, dec;
    while (! inFile.eof()) {
        inFile.get(c);
        dec = declassifyChar(c, key);
        outFile.put(dec);
    }
}

char declassifyChar(char decChar, int key) {
    decChar -= key;

    if ((decChar > 31 || decChar == 10) && decChar < 126)
        return decChar;
    else
        return decChar - 32 + 127;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值