Encryption Methods

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
#include <iostream>
#include <string>
#include<fstream>

struct data{
	std::string extension;
	short int* mydata;
	int size;
};

using namespace std;

int randgen (int seed)
{
	short int a = 1473;
	seed = (seed*a+87)%72;

return seed;
}




int main()
{
	cout<< "E to encrypt, or D to decrypt:\n";
	string selection;
	cin>>selection;


	if(selection == "E"){
	data mydata;
	string filename;
	int seed;
	cout<< "Which file would you like to encode?\n";
	cin >> filename;
	cout<< "What is the file extension(including the '.'?\n";
	cin>> mydata.extension;
	cout<<"What is the seed?\n";
	cin >> seed;

	char* cleartext;


	ifstream file (filename+mydata.extension, ios::in|ios::binary|ios::ate);
  if (file.is_open())
  {
    int size = file.tellg();
	mydata.size = size;
	 cleartext = new char [size];
    file.seekg (0, ios::beg);
    file.read (cleartext, size);
    file.close();
  }
  mydata.mydata = new short int[mydata.size];
	for( int i=0; i< mydata.size; i++){
	seed = randgen(seed);
	mydata.mydata[i] = (int)cleartext[i]*seed;
	}

	std::ofstream fileo (filename+".enc", std::ios::out|std::ios::binary);
  if (fileo.is_open())
  {
	 
	  fileo.write((char*)&mydata.size, sizeof(mydata.size));
	  for(int i = 0; i < mydata.size; i++){
	  fileo.write ((char*)&mydata.mydata[i],sizeof(short int));
	  }
	  fileo.write((char*)&mydata.extension, sizeof(mydata.extension));
	  fileo.close();
  }
	cout<<"File encrypted.";
	getchar();
	getchar();
	}







	else if(selection == "D"){
	int seed;
	string filename;
	cout<< "Which file would you like to decode?\n";
	cin >> filename;
	cout<<"What is the seed?\n";
	cin >> seed;
	data mydata;

	std::ifstream file (filename+".enc", std::ios::out|std::ios::binary);
  if (file.is_open())
  {
	  file.read((char*)&mydata.size, sizeof(mydata.size));
	  mydata.mydata = new short int [mydata.size];
	  for(int i = 0; i <mydata.size;i++){
	  file.read ((char*)&mydata.mydata[i], sizeof(short int));
	  }
	  file.read((char*)&mydata.extension, sizeof(mydata.extension));
	  file.close();
  }

  char* output;
  output = new char[mydata.size];
	
	for( int i=0; i< mydata.size; i++){
	seed = randgen(seed);
	output[i] = (mydata.mydata[i]/seed);
	}
	
	std::ofstream fileo (filename+"(decrypted)"+mydata.extension, std::ios::out|std::ios::binary);
  if (fileo.is_open())
  {
	  for(int i = 0; i < mydata.size; i++){
	  fileo.write ((char*)&output[i],sizeof(char));
	  }
  }
  file.close();


	cout<<"File decrypted.";
	getchar();
	getchar();
	}
	else{}
	return 0;
}


Hey you guys.
First of all, I'd just like to say that this is my first post, and I don't really know where it belongs, but beginners sounds good to me.
I was just wondering how more advanced encryption codes are written. The one I posted above was a quick experiment I did, but I know that the encryption for the data is very inefficient (Doubling the bytes), and that it is not a very hard algorithm to break. I was just wondering how professional encryption works, especially with the much larger keys.
What is a short int?
Last edited on
A short int is basically an int that uses less memory. In the case of Windows, an int is 4 bytes and a short int is 2. Sizeof simply returns the size of the parameter (In this case 2 bytes).
I mean, why are you writing short int instead of simply short?
Last edited on
Haha, personal preference I guess? I dunno. Makes it easier to read for me.
OK.
As to an answer to your question, I found this:
http://www.cplusplus.com/forum/general/13920/#msg67449
Actually there are Open Source C++ library around which you can use instead of coding your own. If I'm not wrong, the "latest" algorithm is AES. There are various language implementation based on that algorithm. DES, RSA etc are considered "old".
Topic archived. No new replies allowed.