⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 一个文件加密的例子
💻 CPP
字号:
#include <iostream>
#include "time.h"
#include "stdlib.h"
#include "fstream.h"
#include "string.h"
using std::endl;

int wrap(int num) //wraps around a number
{
	if (num > 255)
	{
		num = num - 256;
	} //end if
	if (num < 0)
	{
		num = num + 257;
	} //end if
	return(num);
} //end wrap()

void encode(char *line, int key) //encodes a string of text
{
	for (int i = 0; i < static_cast<int> (strlen(line)); i++)
	{
		line[i] = static_cast<char> (wrap((static_cast<int>(line[i])) + key));
	} //end for
} //end encode()

void decode(char *line, int key) //decodes a string of text
{
	for(int i = 0; i < static_cast<int>(strlen(line)); i++)
	{
		line[i] = static_cast<char>(wrap((static_cast<int>(line[i])) - key));
	} //end for
} //end decode()

char* back(char *line) //reverses a string of text
{
	char temp[200];
	int length = static_cast<int>(strlen(line));
	for(int i = 0; i < length; i++)
	{
		temp[i] = line[length - i];
	} //end for
	temp[i] = static_cast<char>(0);
	return(temp);
} //end back()

int main() //main body
{
	//set variables
	char file_name[100],file_out[100],buffer[200];
	ofstream fout;
	ifstream fin;
	int choose,key = 0;
	//get input
	cout << "Encode (1) or decode (2) a file? ";
	cin >> choose;
	cout << "Choose file to input: ";
	cin >> file_name;
	fin.open(file_name);
	cout << "Choose file to output to: ";
	cin >> file_out;
	fout.open(file_out);
	//do preliminary setup
	if (choose == 1)
	{
		while (key == 0)
		{
			key = time(NULL) % 11;
		} //end while
		fout << (3*key) << endl;
	} //end if
	if (choose == 2)
	{
		cout << "\n";
		fin >> key;
		key = static_cast<int>(key/3);
	} //end if

	//end line and do operation
	while(!fin.eof())
	{
		fin.getline(buffer,200,'\n');
		if (choose == 1)
		{
			encode(back(buffer),key);
		} //end if
		if (choose == 2)
		{
			decode(back(buffer),key);
		} //end if
		fout << buffer << endl;
	} //end while

	//do finishing steps
	fout.close();
	fin.close();
	cout << "Operation Complete";
	cin >> buffer;
	return(1);
} //end main()

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -