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;
}
|