Encryption using an offset file

G

Gactimus

Here is a program that encodes and decodes a text file. What I need to do
is write a C++ program that requests 3 different file names. One filename
is for the source file to be encoded, another is the name of the 'encoded'
output file, and the final filename is an offset file. On a character by
character basis, the program needs to look at the first character of the
source file and offset it by the first character in the offset file. The
second character in the source is offset by the second character in the
secret offset file. I need to deal with the possibility that your source
is longer than the offset file in which case it just needs start over or
repeat the secret offset file from the beginning.

The one thing I am having trouble with is offsetting the source file with
the offset file. Here is my code. I need to implement the offset file. Any
ideas?


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;

#define cypher (char) ASCII + 1
#define decypher (char) ASCII - 1

int main()
{
char ASCII;
cout << "1) Encode\n2) Decode\n<q to quit>: ";
int coding;
cin >> coding;

if(coding == 1)
{
ifstream source;
ofstream clone;

source.open("lazy_dog.txt", ios::in);
clone.open("cypher.txt", ios::eek:ut | ios::trunc);

while(!source.eof())
{
char NewASCII;
ASCII = source.get();
if (source.fail())
return 0;
NewASCII = cypher;
clone.put(NewASCII);
}

source.close();
clone.close();
return 0;
}

else if(coding == 2)
{
ifstream origin;
ofstream copy;

origin.open("cypher.txt", ios::in);
copy.open("decypher.txt", ios::eek:ut | ios::trunc);

while(!origin.eof())
{
char NewASCII;
ASCII = origin.get();
if (origin.fail())
return 0;
NewASCII = decypher;
copy.put(NewASCII);
}

origin.close();
copy.close();
return 0;
}

else
return 0;
}
 
S

Someonekicked

I dont see the point of that offset file, i get what you mean by it, but its
not a good way for encryption.
in ur earlier post u asked "I want to do it very simply, as in adding 1 to
every ASCII character";
maybe you better stick with ur earlier suggestion, which is simpler in
implementation.

so you can change encoding for example :
while(!source.eof())
{
char char_read;
char_read = source.get();
clone.put(++char_read);
}
 
G

Gactimus

I dont see the point of that offset file, i get what you mean by it, but
its not a good way for encryption.

It may not be a good way but it's the way I have to do it.
in ur earlier post u asked "I want to do it very simply, as in adding 1
to every ASCII character";

Yeah, I got that program working, but in a different way than you suggested.
 
S

Someonekicked

the following code read all characters from someFile.dat, and put them in an
array ( called data ), the length of that array is the total number of
characters in the file plus 1; the last character in the array is the null
character :
ifstream inData("someFile.dat",ios::binary);
inData.seekg(0,ios::end); // move the pointer to end of file.
int length = inData.tellg(); // reads position of the pointer.
inData.seekg(0); // move pointer to beginning of file.
char* data = new char; // declaring the array.
inData.read(data,length); // filling the array.
data[length] = '\0'; // setting last character of the array to
null.

the benefits of that, you read all characters at once, and then u can
manipulate them like you want without accessing the source file, and for
example if you do
cout << data << endl; // you will get all the source file.


you can use this twice to get two different arrays, source_data of the
source ( of length source_length) and offset_data the offset file ( of
length offset_length).
then to write characters to the encoded file,
int count = 0;
while (count < source_length)
encoded << source_data[count] << offset_data[ count % offset_length ];
 
O

osmium

Gactimus said:
Here is a program that encodes and decodes a text file. What I need to do
is write a C++ program that requests 3 different file names. One filename
is for the source file to be encoded, another is the name of the 'encoded'
output file, and the final filename is an offset file. On a character by
character basis, the program needs to look at the first character of the
source file and offset it by the first character in the offset file. The
second character in the source is offset by the second character in the
secret offset file. I need to deal with the possibility that your source
is longer than the offset file in which case it just needs start over or
repeat the secret offset file from the beginning.

The one thing I am having trouble with is offsetting the source file with
the offset file. Here is my code. I need to implement the offset file. Any
ideas?
<snip>

The usual way to encipher along these lines it to do an exclusive or.

enc = plain ^ key;

Where enc is the enciphered output char, plain is from the input file and
key is from what you call "offset".

You can then retrieve the original text by writing:

plain = enc ^ key;

where plain is the original, that is, deciphered, text.

There are several ways to handle the key file being too short. The easiest
for a beginner might be to simply close and re-open the file. If that
doesn't appeal to you, you can "seek" within a file.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top