Encrypt a .txt file

Jul 29, 2015 at 6:49am
closed account (9G8MoG1T)
Hey again guys,

Been having trouble on how exactly do I encrypt a file. Been at it for a few hrs.
Tried text book; doesn't help much.

This is what I have so far: "On how to read the text file."

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string line;
fstream input("plain.txt");

fstream output("Decrypt.txt");

if (input.is_open())
{
while (getline(input, line))
{
cout << line << endl;
}
input.close();
}

else cout << "ERROR: Unable to open file.";

return 0;

}

Just don't know how to encrypts the file by adding
4 to the ASCII value of each character.
Jul 29, 2015 at 9:09am
For instance:
1
2
3
4
for(size_t i = 0; i < line.size(); ++i)
{
  line[i] += 4;
}
Jul 29, 2015 at 4:41pm
Looks like you are doing the same assignment that i was assigned.... if you are using Gaddis textbook than it would be helpful to read Chapter 12(Pg.673)...here's what's wrong:

1) If you are trying to take "plain.txt" --> than encrypt it as "coded.txt" --> decrypt the file in output as "decoded.txt" ???? Start with pseudo code before you jump in....

2) Here's an e.g. of code that reads string "Line by Line" from a file as you are trying preserve the format of "plain.txt":
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
string raw_data;

ifstream inFile ("plain.txt", ios::in);

if (inFile)
{
    getline(inFile, raw_data); // Read first line
    while (inFile)  // While last read operation successful, continue
    {
        cout << raw_data << endl; //Display last read line
        getline(inFile, raw_data); // Read next line
        // Figure out What to do here use coder777's code than you have to do something else
        // To preserve your output....
    }
    inFile.close(); // Close the file    
}
else cout << "Error:...\n"; 


3) You are in right path just remember each each character's is in it's ASCII value so you can add subtract an int to it.
Jul 29, 2015 at 9:38pm
closed account (9G8MoG1T)
Thank you for the hint! I appreciate eveyone's help
Jul 30, 2015 at 7:12am
You can try this code to encrypt a file

#include <vector>

typedef unsigned __int8 BYTE;

std::vector<BYTE> xor_encryptdecrypt(const std::vector<BYTE>& encryptionKey, const std::vector<BYTE>& input)
{
std::vector<BYTE> ret;
ret.reserve(input.size());

std::vector<BYTE>::const_iterator keyIterator = encryptionKey.begin();

for(std::vector<BYTE>::const_iterator inputIterator = input.begin(); inputIterator != input.end(); ++ inputIterator)
{
ret.push_back(*inputIterator ^ *keyIterator);

// advance the key iterator, wrapping to begin.
if(++ keyIterator == encryptionKey.end())
keyIterator = encryptionKey.begin();
}
return ret;
}


int main()
{
// variable-length key of randomness, used for both encryption and decryption.
std::vector<BYTE> key;
key.push_back(0x55);
key.push_back(0xae);
key.push_back(0x8c);
key.push_back(0x14);

std::vector<BYTE> input;
input.push_back(0x00);
input.push_back(0x01);
input.push_back(0x02);
input.push_back(0x03);
input.push_back(0x04);
input.push_back(0x05);

// encrypt
std::vector<BYTE> encrypted = xor_encryptdecrypt(key, input);

// decrypt
std::vector<BYTE> decrypted = xor_encryptdecrypt(key, encrypted);

return 0;
}
Topic archived. No new replies allowed.