Hello guys!
I'm challenging myself to write a program that encrypts a text file with a key and the program requests the user, before running, for a password.
The way i'm setting up the password thing is that i'm creating a binary file within the project folder (for now, i'm working with visual studio 2017).
When the program starts I do a few things
_check if the password binary file exists, if it doesn't it creates it
_check if the file contains a password, if it does, prompt the user to
enter the password and check validity. If it doesn't, prompt the user
to enter a password twice, check validity, if correct write to binary
file, if not correct, ask for password until it is correct and write
to file.
The thing i seem to have an issue is that i'm storing the password in a string which I write on the binary file with
file.write(reinterpret_cast<char *>(&passWord), sizeof(passWord));
where file is a binary file opened in output mode and passWord is a string. That goes OK.
Now the thing i think is causing me issues is when i run the program, it reads the file to store the password in the passWord string, yet it doesn't know the size of the data it's supposed to read on the file, and using
sizeof(passWord)
doesn't seem relevant since passWord is so far uninitialised.
How would you fix this, how can you tell the program to read on a binary file without knowing in advance the size of the data it should read from it?
Here is my code:
Main.cpp
1 2 3 4 5 6 7 8 9 10
|
#include <iostream>
#include "Crypt.h"
int main()
{
Encryption encrypt;
pause(-1);
}
|
Crypt.h
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
|
#ifndef CRYPT_H
#define CRYPT_H
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void pause(int);
class Encryption
{
private:
fstream passWordFile;
string passWord;
string passWordInput;
bool passWordSet;
bool correctPassWord;
public:
Encryption();
~Encryption();
void checkPWFileIntegrity();
void setPassWord();
bool getPassWord();
};
#endif
|
Crypt.cpp
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
|
#include "Crypt.h"
void pause(int num)
{
switch (num)
{
case -1:
cout << "\n\n\n Press ENTER to exit...";
cin.sync();
cin.get();
cout << "\n\n\n";
break;
case 0 :
cout << "\n\n\n Press ENTER to resume program...";
cin.sync();
cin.get();
cout << "\n\n\n";
break;
}
return;
}
Encryption::Encryption()
{
checkPWFileIntegrity();
if (passWordSet)
correctPassWord = getPassWord();
else
setPassWord();
}
Encryption::~Encryption()
{
}
void Encryption::checkPWFileIntegrity()
{
ifstream PWFile;
PWFile.open("pw.bin", ios::binary);
if (PWFile)
{
cout << "\nFile successfully opened!";
}
else
{
PWFile.close();
ofstream createFile;
createFile.open("pw.bin", ios::binary);
createFile.close();
PWFile.open("pw.bin", ios::binary | ios::in);
cout << "\nSuccessfully created \"pw.bin\" file.";
};
//I THINK THIS IS WHERE THE ISSUE LIES
PWFile.read(reinterpret_cast<char *>(&passWord), sizeof(passWord));
if (passWord != "")
passWordSet = true;
else
passWordSet = false;
cout << "\n\nPassword set status: " << passWordSet
<< "\nPassword: _" << passWord << "_";
PWFile.close();
return;
}
void Encryption::setPassWord()
{
while (!passWordSet)
{
cout << "\n\nPlease enter a password:\t_";
getline(cin, passWord);
cout << "Confirm password:\t\t_";
getline(cin, passWordInput);
if (passWord == passWordInput)
passWordSet = true;
else
cout << "\n\nInput incorrect..\n";
}
passWordFile.open("pw.bin", ios::binary | ios::out);
passWordFile.write(reinterpret_cast<char *>(&passWord), sizeof(passWord));
passWordFile.close();
return;
}
bool Encryption::getPassWord()
{
cout << "\n\nEnter password: _";
getline(cin, passWordInput);
if (passWordInput == passWord)
return true;
else
{
cout << "\n\nPassword incorrect.";
return false;
}
}
|
Thanks in advance for any help or insight.
Wishing you all a great day!
Hugo.
EDIT:
The error i'm getting:
file "iosfwd" opens and this part is highlighted:
1 2 3
|
static constexpr int_type to_int_type(const char& _Ch) _NOEXCEPT
{ // convert character to metacharacter
return (static_cast<unsigned char>(_Ch));
|
Error reads:
Exception thrown: read access violation.
_Ch was 0x2A81F0. |
Note that this error is random, according to what the password is set to, program crashed or doesn't. It seems somewhat random.