Binary code decryption

Hello! : )

I am C++ beginner, and i want to write a program that will read a text from a file turn it into a binary code, and save it into the other file. Then my program should generate a password(save it into a second file), ask the user for it and if its good decrypt the binary code file.

Everything work perfect, except decryption. I have no idea how to decrypt my file, and i need help. Sholud I change my encryption method or is it possible to do this my way ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  // 
// doc..

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <ctime>
#include <bitset>
using namespace std;

void chartobinary(char);
void decryption(char);
void passwordgenerator(int[]);

ofstream saveresult; // 1* 

int main()
{
	char ch,ch1;
	int i = 0, y=0, p=0,g;
	int x[5], ans[5], ans1[5];
	srand(time(NULL));

	saveresult.open("C:\\Users\\Piotr\\Desktop\\saveresult.txt"); 

	// Read text from a file 
	ifstream encrypt;
	encrypt.open("C:\\Users\\Piotr\\Desktop\\Enigma.txt");
	if (encrypt.is_open())
	{	
		while (!encrypt.eof())
		{			
			encrypt.get(ch);    
			chartobinary(ch); 
			i++;
		}
		encrypt.close();
	}
	else
	{
		cout << "Unable to open file " << endl;
	}	
	
	passwordgenerator(x);
	saveresult.close();
	cout << endl;
	cout << "If you want to decrypt this file you need a password: " <<    endl;
	for (int p = 0;p < 5;++p)
	{
		cin >> ans[p];
	}
	
	ifstream pass;
	pass.open("C:\\Users\\Piotr\\Desktop\\password.txt");
	if (pass.is_open())
	{
		while (pass.good())
		{
			pass >> ans1[y];			
			cout << ans1[y] << endl;
			y++;
		}
		pass.close();
	}

	if (x[0] == ans[0] && x[1] == ans[1] && x[2] == ans[2] && x[3] == ans[3] && x[4] == ans[4]) 
	{
		/*
		ifstream decryption;
		decryption.open("C:\\Users\\Piotr\\Desktop\\saveresult.txt");   
		if (decryption.is_open())
		{
			while (decryption.good())         // Here my program should decrypt file 
			{
				decryption.get(ch1);
				decryption1(ch1);
				p++;
			}
		}
		*/
	}
	else
	{
		cout << "Wrong password " << endl;
	}

	cout << endl;
	system("pause");
	return 0;
}

// Transfer characters into binary 
void chartobinary(char ch)
{
	for (int i = 7; i >= 0; --i)       // <-- THIS IS MY ENCRYPTION METHOD 
	{
       char x = putchar((ch & (1 << i)) ? '1' : '0'); 
	   saveresult << x;
	}
	putchar(' '); 
}

// Decryption function
void decryption1(char ch1) 
{
		for (int i = 0;i < 7;i++)
		{
			// < ==== ????

		}

}
// Password generator - five random numbers from 1 to 9
void passwordgenerator(int x[])
{ 	
	ofstream myfile("C:\\Users\\Piotr\\Desktop\\password.txt");
	if (myfile.is_open())
	{
		for (int i = 0; i < 5; ++i)
		{
			x[i] = (rand() % 9) + 1;
			myfile << x[i];
		}
		myfile.close();
	}
	else
	{
		cout << "Unable to create a file " << endl;
	}
}


Thanks for any help ! : )
Thanks to "fun2code" my problem is solved, thank you very very much ! :)
Topic archived. No new replies allowed.