Connecting Tech Pros Worldwide Help | Site Map

Encryption using an offset file

Gactimus
Guest
 
Posts: n/a
#1: Jul 22 '05
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::out | 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::out | 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;
}
Someonekicked
Guest
 
Posts: n/a
#2: Jul 22 '05

re: Encryption using an offset file


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);
}



"Gactimus" <gactimus@xrs.net> wrote in message
news:bEBmd.1416$Oc.1211@tornado.tampabay.rr.com...[color=blue]
> 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::out | 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::out | 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;
> }[/color]


Gactimus
Guest
 
Posts: n/a
#3: Jul 22 '05

re: Encryption using an offset file


"Someonekicked" <someonekicked@comcast.net> wrote in
news:cJydnY2-V-JodwfcRVn-uw@comcast.com:
[color=blue]
> I dont see the point of that offset file, i get what you mean by it, but
> its not a good way for encryption.[/color]

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

Yeah, I got that program working, but in a different way than you suggested.
Someonekicked
Guest
 
Posts: n/a
#4: Jul 22 '05

re: Encryption using an offset file


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 ];




"Gactimus" <gactimus@xrs.net> wrote in message
news:bEBmd.1416$Oc.1211@tornado.tampabay.rr.com...[color=blue]
> 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::out | 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::out | 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;
> }[/color]


osmium
Guest
 
Posts: n/a
#5: Jul 22 '05

re: Encryption using an offset file


"Gactimus" writes:
[color=blue]
> 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?[/color]
<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.
----
If you don't like the above, look very closely at what "offset" means. You
have defined it internally or someone else has defined it for you. In your
solution, note that the "signedness" of char is *not* defined in the C++
standard.




Closed Thread