HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppModerate

FileScrambler, a command-line file encryption tool

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
encryptionfilelinefilescramblercommandtool

Problem

I decided to try my hand at making an encryption utility, which I lovingly call "FileScrambler". FileScrambler allows the user to encrypt a file (a master-password list, personal diary, or whatever) using a password, and save the resulting jumbled mess to their computer (same folder as the executable is located). They can then decrypt the file using the same password, revealing all their hidden secrets.

Any and all comments are welcome, especially in regards to optimization and standards compliance.

Being a novice programmer, please be gentle with the criticism. This is only my second completed project.

```
#include
#include
#include
#include
#include
#include

std::string load(std::string nameOfFile){ //done

std::ifstream input;
input.open(nameOfFile.c_str());
std::string content((std::istreambuf_iterator(input)), std::istreambuf_iterator());
return content;
}

std::string encrypt(std::string content, std::string password){ //also decrypts! :D

std::string out;
int p = 0;
for (int index = 0; index = password.size())
p = 0;
}
return out;
}

void save(std::string nameOfFile, std::string content){ //saves a file with content Content
char response;
std::ifstream in(nameOfFile.c_str());
std::ofstream of;
if (!in){ //prevents a file from being overwritten unless the user says it's okay (in the nested IF statement).
in.close();
of.open(nameOfFile.c_str());
of > response;
if(response == 'y'){
of.open(nameOfFile.c_str());
of > nameOfFile;
data = load(nameOfFile.c_str());
std::cout > responseC;
if (responseC == 'y')
std::cout > response;
save(response, data);
break;
}
case 3: //Encrypt the content of 'data' by XORing it against a password.
{
std::cout > responseC;
if (responseC == 'y'){

//

Solution

Your XOR algorithm is excessively basic, and does not XOR as much as it should.

out += content[index] xor password[p] * p % 30


The operator precedence order is and % have equal precedence, and are both higher precedence than xor: operator precedence. Because and % are equal-order, they are processed left-to-right.

Your function above, thus, decomposes to the following:

out += content[index] xor ( ( password[p] * p ) % 30)


In essence, your xor operator will only ever have a value from 0 through 29 (inclusive) on the right hand side.

In bits, this means that the high-3 bits will always 'flip' on every operation, and are essentially never encrypted.

I suspect that your intention is to set the precedence for the p % 30 to be higher than the password[p] * p. To do that, you need braces....

Code Snippets

out += content[index] xor password[p] * p % 30
out += content[index] xor ( ( password[p] * p ) % 30)

Context

StackExchange Code Review Q#75677, answer score: 10

Revisions (0)

No revisions yet.