U E D R , A S I H C RSS

비밀키/권정욱

비밀키/권정욱


string 비이용

~cpp 
#include <iostream> 
#include <fstream> 
using namespace std; 

int main(){ 
	char secret[30]; 
	cout << "암호화할 텍스트를 입력하세요 : "; 
	cin >> secret; 
	ifstream fin(secret); 
	ofstream fout("source_enc.txt"); 
	
	int key1, key2;  
	char ch;
	char text;
	
	cout << "암호키를 입력하세요 : "; 
	cin >> key1; 
	
	fin.get(text); 
	
	cout << "암호화를 실행합니다.\n"; 

	while (!fin.eof()){
		ch = text + key1;
		if (text == '\n') {   
			cout << endl; 
			fout << endl; 
		} 
		else{ 
			cout << ch;
			fout << ch;
		} 
		fin.get(text);
	} 


	fin.close();
	fout.close();


	char unsecret[30];

	cout << "복호화할 텍스트를 입력하세요 : ";
	cin >> unsecret;
	ifstream ffin(unsecret);
	ofstream ffout("resource_enc.txt");

	cout << "암호키를 입력하세요 : "; 
	
	cin >> key2; 
	
	if (key1 != key2) {
		cout << "키가 틀립니다.\n";
		return 0;
	}
	ffin.get(text); 
	
	cout << "복호화를 실행합니다.\n"; 
	
	while (!ffin.eof()){
		ch = text - key2;
		if (text == '\n') {   
			cout << endl; 
			ffout << endl; 
		} 
		else{ 
			cout << ch;
			ffout << ch;
		} 
		ffin.get(text);
	} 
	

	return 0;
}

string 이용

~cpp 
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main(){ 
	char secret[30]; 
	cout << "암호화할 텍스트를 입력하세요 : "; 
	cin >> secret; 
	ifstream fin(secret); 
	ofstream fout("source_enc.txt"); 
	
	int key; 
	string text; 
	char ch;
	
	cout << "암호키를 입력하세요 : "; 
	cin >> key; 
	
	fin >> text; 
	
	cout << "암호화를 실행합니다.\n"; 
	
	int i = 0;
	while (!fin.eof()){
		ch = text[i] + key;
		if (text[i] == '\0') {   
			i = 0;
			cout << endl; 
			fout << endl; 
			fin >> text;
		} 
		else{ 
			cout << ch;
			fout << ch;
			i++;
		} 
	} 
	cout << endl; 
	cout << "키값은 : " << key << "입니다.\n"; 
	fout << "키값은 : " << key << "입니다.\n";       
	return 0; 
} 
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:36
Processing time 0.0181 sec