A decryption code for the
encryption code
/*
* password decrypt program for the previous encryption program
*By K Sreram
*/
#include <iostream>
#include <conio.h>
#include <fstream>
#include <list>
#include <string>
const char encryptionPin[] = { "password" };
int main(int argc, char **argv)
{
std::string fileName;
if (!(argc >= 2))
{
std::cout << " enter file name:";
std::getline(std::cin, fileName);
}
else
fileName = argv[1];
char data; size_t i;
std::fstream file(fileName, std::ios::in | std::ios::binary);
std::list<char> filesData;
std::list<char>::iterator index;
std::string password;
std::string checkPin;
std::cout << "\n\n enter the password\n";
std::cin >> password; // get the password (does not include space)
while (!file.eof())
{
file.read(&data, sizeof(char)/*always 1*/);
filesData.push_back(data); /// get the file's data in a list
}
file.clear();
file.close();
const size_t passwordLength = password.size();
for (index = filesData.begin(), i = 0; i < 8; i++, index++)
{ /// check pin
if (index == filesData.end())
{
std::cout << "error: this file is currept";
return -1;
}
(*index) = (*index)- (char)i - password.c_str()[i%passwordLength];
checkPin.push_back((*index));
}
if (checkPin != encryptionPin)
{
std::cout << "incorrect password";
std::cout << checkPin;
system("pause");
return -2;
}
file.open(fileName, std::ios::out | std::ios::binary); /// prepare the file for writing
for (; index != filesData.end() && i < filesData.size()-2; index++, i++)
{
(*index) = (*index)- (char)i - password.c_str()[i%passwordLength];
data = (*index);
file.write(&data, sizeof(char));
}
file.clear();
file.close();
std::cout << "file decryption completed";
system("pause");
return 0;
}
About my blog
No comments:
Post a Comment