GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 30-Jul-2005, 10:21
Zorachus's Avatar
Zorachus Zorachus is offline
Junior Member
 
Join Date: Jul 2005
Posts: 58
Zorachus will become famous soon enough

Vernam encryption cipher


I have been working on a program that is supposed to encrypt and decrypt text files using a vernam cipher: http://en.wikipedia.org/wiki/Vernam_..._Vernam_cipher
I have been working on this for about a week, and have gotten the program down to where when it encrypts and decrypts files, it decrypts them with only a few errors. Here is my problem:
When working with the space (" ") character, it seems to have a lot of problems. It also has problems processing files with more than one line; it will decrypt them, but with several errors. Here is an example of a typical decryption on a one-line file with no spaces:
abcdefghijklmnopqrstuvwxyz
to
abcdefgkijklmnepqrstnvwxyz
Last, it seems to have trouble with all characters that are not numbers or letters. The way I have it set up, all characters should be treated the same, and though i have worked on this problem for a long time, I have not been able to come up with a solution.

Anyway, here is the code. I know that posting the entire program is generally frowned upon, but in this case the program is very difficult to understand with only small parts of it to work with. Everything that may require some explanation is commented. Any help is greatly appreciated!

CPP / C++ / C Code:
//*********************************************//
//Encrypts text documents using a Vernam Cipher//
//So far, encrypts, doesn't yet decrypt********//
//Plus, has some bugs.  problems in comments***//
//********Zach Walton, 2005********************//
//*********************************************//
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <time.h>
#include <iomanip>
using namespace std;
int main()
{
srand(time('\0'));
char alph[95] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`1234567890-=~!@#$%^&*()_+[]\{}|;': ,./<>?";
char readbuffer[500],writebuffer[500],filename[50],bufferkey[500],;
int doclen=0,a=0, frlp, frlptwo, frlpthree, beginmenu, z=0, b=0;
begin:
cout << setw(10) << "Vernam Cipher Encryption System" << endl;
cout << setw(17) << "Developed by Zach Walton" << endl;
cout << "2005" << endl;
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout << "1. Encrypt a file\n";
cout << "2. Decrypt a file\n";
cin >> beginmenu;
while(z!=1 && beginmenu!=2)
{
system ("cls");
cout << "Warning!  Will delete file named Key.txt.  If you wish to keep this file, move it to a different directory.\n";
system ("pause");
system("echo a > Key.txt");
system ("cls");
encrypt:
cout << "Input the filename (make sure to include extension!): ";
cin >> filename;
fstream file (filename);
if (! file.is_open())             //Checks for open file
{
      cout << "The file does not exist.  Did you type the extension?\n";
      system ("pause");
      system ("cls");
      goto encrypt;
}
while (! file.eof())            //finds document length
{
      file.getline(readbuffer,200);
      doclen = doclen+strlen(readbuffer);
}
char masterkey[doclen], filestr[doclen], encrypt[doclen];
int mastkint[doclen],filesint[doclen], encrypint[doclen];
for (frlp=0; frlp<(doclen); frlp++) //creates a random key equal to the document length
{
    a=rand()%94;
    masterkey[frlp]=alph[a];
}
if (strlen(masterkey)>doclen)    //stores NULL to each character longer than the document length- for error checking
{
for (frlp=doclen; frlp<strlen(masterkey); frlp++)
{
        masterkey[frlp]='\0';
}
}
frlptwo=0, frlpthree=frlptwo;
file.close();
fstream keytext ("Key.txt");
keytext << masterkey << endl;
keytext.close();
fstream filetwo (filename);
a=0;
while (! filetwo.eof() && b!=900)        //stores entire document to a variable (filestr)
    {
    filetwo.getline(readbuffer,200); 
    for (frlptwo; frlptwo<(frlpthree+strlen(readbuffer)); frlptwo++)
        {
        filestr[frlptwo]=readbuffer[a];
        a++;
        if (frlptwo==(doclen-1))
           b=900;
        }
    frlpthree=frlptwo;
    a=0;
    }
if (strlen(filestr)>doclen)                //stores NULL to each character in filestr over document length
{
for (frlp=doclen; frlp<strlen(filestr); frlp++)
{
        filestr[frlp]='\0';
}
}
remove(filename);                         //clears contents of old "Key.txt" file
filetwo.close();  
for (frlp=0; frlp<doclen; frlp++)         //converts characters to their respective numbers; problem probably here.
{
    for(a=0; a<95; a++)        //converts characters in masterkey to numbers in mastkint
    {
              if(masterkey[frlp]==alph[a])
               mastkint[frlp]=a;
    }
    for(a=0; a<95; a++)    //converts characters in filestr to numbers in filesint
    {
             if(filestr[frlp]==alph[a])
             filesint[frlp]=a;
    }
}
for (frlp=0; frlp<doclen; frlp++)  //subtracts number in filesint from number in mastkint.
                                   //if negative, converts to positive.
                                   //requires understanding of vernam cipher
{
    encrypint[frlp]=mastkint[frlp]-filesint[frlp];
    if (encrypint[frlp]<0)
    encrypint[frlp]=encrypint[frlp]*-1;
}
for (frlp=0; frlp<doclen; frlp++) //converts encrypint to its related character; final encrypted text
{
    a=encrypint[frlp];
    encrypt[frlp]=alph[a];
}
if (strlen(encrypt)>doclen)
{
for (frlp=doclen; frlp<strlen(encrypt); frlp++) //stores NULL to all characters in encrypt longer than doclen
{
        encrypt[frlp]='\0';
}
}

fstream enc (filename);
enc << encrypt << endl;                    //stores encrypted text to the original document
cout << "File succesfully encrypted!\n";
z++;
}
/*
/
/
//END ENCRYPTION SECTION
//BEGIN DECRYPTION SECTION
/
/
*/
while(z!=1 && beginmenu!=1)
{
decrypta:
system ("cls");
cout << "Make sure that both Key.txt and the desired file to be decrypted are in the same directory as the program, and input the name of the file to be decrypted(Don't forget the extension!): ";
cin >> filename;
fstream dfile (filename);  //loads file to be decrypted  
if (! dfile.is_open())     //error check- is file open?
{
      cout << "The file does not exist.\n";
      system ("pause");
      goto decrypta;
}
decrypt:
while (! dfile.eof())  //finds document length of file
{
      dfile.getline (readbuffer,200);
      doclen = doclen+strlen(readbuffer);
}
dfile.close();
char dkey[doclen], dfileu[doclen], decrypt[doclen];
int dkint[doclen], dfint[doclen], dalcomp[doclen];
frlptwo=0, frlpthree=frlptwo;
fstream dfiletwo (filename);       //re-declares the file
while (! dfiletwo.eof() && b!=900) //stores contents of file to a string (dfileu)
    {
    dfiletwo.getline(readbuffer,200); 
    for (frlptwo; frlptwo<(frlpthree+strlen(readbuffer)); frlptwo++)
        {
        dfileu[frlptwo]=readbuffer[a];
        a++;
        if (frlptwo==(doclen-1))
           b=900;
        }
    frlpthree=frlptwo;
    a=0;
    }
dfiletwo.close();
b=0;
if (strlen(dfileu)>doclen)  //error check; stores NULL to extra characters
{
for (frlp=doclen; frlp<strlen(dfileu); frlp++)
{
        dfileu[frlp]='\0';
}
}
fstream dkeyfile ("Key.txt");  //loads the key file
a=0, frlptwo=0, frlpthree=frlptwo;
while (! dkeyfile.eof() && b!=900) //stores the key file to a string
    {
    dkeyfile.getline(readbuffer,200); 
    for (frlptwo; frlptwo<(frlpthree+strlen(readbuffer)); frlptwo++)
        {
        dkey[frlptwo]=readbuffer[a];
        a++;
        if (frlptwo>doclen)
           b=900;
        }
    frlpthree=frlptwo;
    a=0;
    }
dkeyfile.close();
if (strlen(dkey)>doclen) //error check; stores NULL to extra characters
{
for (frlp=doclen; frlp<strlen(dkey); frlp++)
{
        dkey[frlp]='\0';
}
}
for (frlp=0; frlp<doclen; frlp++) //converts both strings to their numeric equivalents
{
    for(a=0; a<95; a++) //converts characters in dkey to their numeric equivalents, stores in dkint
    {
              if(dkey[frlp]==alph[a])
               dkint[frlp]=a;
    }
    for(a=0; a<95; a++)//converts characters in dfileu to their numeric equivalents, stores in dfint
    {
             if(dfileu[frlp]==alph[a])
             dfint[frlp]=a;
    }
}
for (frlp=0; frlp<doclen; frlp++) //subtracts dfint[x] from dkint[x], multiplies negatives by -1, stores result to dalcomp
{
    dalcomp[frlp]=dkint[frlp]-dfint[frlp];
    if (dalcomp[frlp]<0)
    dalcomp[frlp]=dalcomp[frlp]*-1;
}
for (frlp=0; frlp<doclen; frlp++) //converts numbers in dalcomp to their character equivalents, stores to decrypt (final decrypted text)
{
    a = dalcomp[frlp];
    decrypt[frlp]=alph[a];
}
if (strlen(decrypt)>doclen) //error check; stores NULL to extra characters
{
for (frlp=doclen; frlp<strlen(decrypt); frlp++)
{
        decrypt[frlp]='\0';
}
}
fstream decrypted (filename);
decrypted << decrypt << endl; //stores decrypted text to original encrypted document
cout << "File successfully decrypted!\n";
cout << "" << endl;
z++;
}
//END DECRYPTION SECTION
system ("pause");
}
  #2  
Old 30-Jul-2005, 15:02
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Problem is you haven't given us any explanation as to what is wrong. "Errors" is just too broad of a description...

Does the encryption part work correctly?

Does the decryption part work correctly?

Output values from the program thoughout the process so you can see exactly where the problem is.

Since I (and most here I'm guessing) don't know what a "vernam cipher" is, I can't tell you whether you coded the algorithm correctly.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 30-Jul-2005, 18:25
Zorachus's Avatar
Zorachus Zorachus is offline
Junior Member
 
Join Date: Jul 2005
Posts: 58
Zorachus will become famous soon enough
My apologies. Though I did provide a link, Wikipedia's definition of a Vernam Cipher isn't very clear. I can try to explain it myself:

Normal encryption methods use a form of encryption known as "substitution." This doesn't require much explanation; an example would be: a=r. Then, when encrypting the file, go through it and change all instances of a to r. When decrypting the file, change all instances of r to a. This, however, is very easy to crack through several methods.

A vernam cipher, however, has been deemed uncrackable. In all its years of use, it has never been cracked. Here is an example:
Say, in the text that you want to encrypt, the first letter is a. To reach "a" in the alphabet, one must advance 0 spaces. That's pretty simple.
The first step in encryption is to generate a random key comprised of letters, numbers, and all other characters (in this case on the keyboard). This key must be equal to the length of the document that is to be encrypted.

Now, let's say that the first letter of the random key is "c." One must advance 2 spaces in the alphabet to get to c. So, you would take 2(c)-0(a) to get the answer: 2. When you advance 2 letters into the alphabet, you get c. So, the encrypted letter is c. To reverse this process (for decryption), all you would have to do is take the letter from the encrypted text (c) and the letter from the key (c). Both of these are reached by advancing two spaces in the alphabet. So, 2-2=0. One would check 0, and seeing that the character reached when advancing into the alphabet 0 spaces is "a," one knows that the decrypted character is "a."
Here is the list of values assigned to each character on the keyboard:
a 0
b 1
c 2
d 3
e 4
f 5
g 6
h 7
i 8
j 9
k 10
l 11
m 12
n 13
o 14
p 15
q 16
r 17
s 18
t 19
u 20
v 21
w 22
x 23
y 24
z 25
A 26
B 27
C 28
D 29
E 30
F 31
G 32
H 33
I 34
J 35
K 36
L 37
M 38
N 39
O 40
P 41
Q 42
R 43
S 44
T 45
U 46
V 47
W 48
X 49
Y 50
Z 51
` 52
1 53
2 54
3 55
4 56
5 57
6 58
7 59
8 60
9 61
0 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

Now that there is a better explanation of the Vernam Cipher, I will attempt to answer some of the other questions about this program.

Quote:
Originally Posted by WaltP
Problem is you haven't given us any explanation as to what is wrong. "Errors" is just too broad of a description...

Sorry. When I say errors, what I mean that when decrypting the file manually using the above value assignments, there are usually anywhere from 1-3 errors, errors meaning that the decrypted characters are not the same as the ones from the original file.
edit: Also, these problems happen more often when decrypting/encrypting files with more than one line, spaces, and non-numeric, non-alphabetic characters.

Quote:
Originally Posted by WaltP
Does the decryption part work correctly?
As far as I can tell, yes. The final decrypted text is normally equal to what I decrypt manually. I think the problem is in the encryption loop.

Quote:
Originally Posted by WaltP
Output values from the program thoughout the process so you can see exactly where the problem is.
I have done this for every loop and variable in the program. Where I think the problem is is commented as "problem probably here;" however, i have been through that area several times and cannot see where the problem is.


I think that should clear things up significantly. I do have one further question: The program cannot handle the " character, because obviously typing """ in c++ to indicate a quote as a character does not work. Is there a way around this?

Again, any help is greatly appreciated! Thanks!
  #4  
Old 30-Jul-2005, 20:49
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Zorachus
My apologies. Though I did provide a link, Wikipedia's definition of a Vernam Cipher isn't very clear. I can try to explain it myself:
Yes you did, but to be honest, I'm not interested in learning this particular algorithm. You as the expert needs to provide enough information to explain what is needed and how your code relates.

Quote:
Originally Posted by Zorachus
Here is the list of values assigned to each character on the keyboard:
--------
edit: Also, these problems happen more often when decrypting/encrypting files with more than one line, spaces, and non-numeric, non-alphabetic characters.
I don't see any conversion for the EOL characters 0x0A and 0x0D, as well as TAB:0x09, Form Feed:0x0C, and other legal characters in a file. This may be throwing off your multi-line encryption.

Quote:
Originally Posted by Zorachus
Sorry. When I say errors, what I mean that when decrypting the file manually using the above value assignments, there are usually anywhere from 1-3 errors, errors meaning that the decrypted characters are not the same as the ones from the original file.
----
The final decrypted text is normally equal to what I decrypt manually. I think the problem is in the encryption loop.
Well, what do you get when you manually encrypt the text? Is it the same as when your code encrypts it? If not, there's where the problem is.

Quote:
Originally Posted by Zorachus
I do have one further question: The program cannot handle the " character, because obviously typing """ in c++ to indicate a quote as a character does not work. Is there a way around this?
use 0x22, \x22, or \", depending on where you need the character


Quote:
Originally Posted by Zorachus
I have done this for every loop and variable in the program. Where I think the problem is is commented as "problem probably here;" however, i have been through that area several times and cannot see where the problem is.
I think there's probably an easier solution to this area. First of all, I don't understand fully what you are trying to do here, but if you consider that each character, whether number, letter, punctuation, and even CR, LF and FF are just numbers, maybe you can use that fact to make this 'numbering' scheme easier to code. For example, based on your list:
CPP / C++ / C Code:
key['a'] = 0;
key['b'] = 1;
key['D'] = 29;
key['P'] = 41;
key[':'] = 86;
etc
Maybe thinking in these terms may help you simplify the concept.



I see a couple things in your code that are bad to do. One is using dfile.eof() as you're doing. It the same as feof() in C if memory serves. Check this information for the reason. Look into the C++ equivalent of fseek()/ftell() instead. Somone here can post the actual method names.

The second is a pet peeve of mine. This should explain...

And read the tutorial on Formatting Code. Your formatting is halfway there, but needs work.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #5  
Old 30-Jul-2005, 21:17
Zorachus's Avatar
Zorachus Zorachus is offline
Junior Member
 
Join Date: Jul 2005
Posts: 58
Zorachus will become famous soon enough
Quote:
Originally Posted by WaltP
Yes you did, but to be honest, I'm not interested in learning this particular algorithm. You as the expert needs to provide enough information to explain what is needed and how your code relates.

I agree completely. I will do this in the future.

Quote:
Originally Posted by WaltP
I don't see any conversion for the EOL characters 0x0A and 0x0D, as well as TAB:0x09, Form Feed:0x0C, and other legal characters in a file. This may be throwing off your multi-line encryption.

Indeed it may, and the reason that I didn't do this is because I didn't think to try the hex equivalent. However, as I can't do this with the method I was using, would strcpy work? It seems as if it would return a const char to char error. I would check this myself but currently don't have access to a compiler.

Quote:
Originally Posted by WaltP
Well, what do you get when you manually encrypt the text? Is it the same as when your code encrypts it? If not, there's where the problem is.

The manual encryption always works; it's something in the program.

Quote:
Originally Posted by WaltP
use 0x22, \x22, or \", depending on where you need the character

Thanks. Again, can this be stored with strcpy?


Quote:
Originally Posted by WaltP
I think there's probably an easier solution to this area. First of all, I don't understand fully what you are trying to do here, but if you consider that each character, whether number, letter, punctuation, and even CR, LF and FF are just numbers, maybe you can use that fact to make this 'numbering' scheme easier to code. For example, based on your list:
CPP / C++ / C Code:
key['a'] = 0;
key['b'] = 1;
key['D'] = 29;
key['P'] = 41;
key[':'] = 86;
etc
Maybe thinking in these terms may help you simplify the concept.

Essentially, that is what I am doing. To get around the problem of not being able to relate a const char to a number, I assigned the chars to their respective numbers in the array. EX: alph[0]="a".

Quote:
Originally Posted by WaltP
I see a couple things in your code that are bad to do. One is using dfile.eof() as you're doing. It the same as feof() in C if memory serves. Check this information for the reason. Look into the C++ equivalent of fseek()/ftell() instead. Somone here can post the actual method names.

The second is a pet peeve of mine. This should explain...

I will check this out. I do need to clean up my code, that has always been a bit of an issue of mine .

Quote:
Originally Posted by WaltP
And read the tutorial on Formatting Code. Your formatting is halfway there, but needs work.

I will look at this as well. Thanks for the help, I'll try to implement the end of line and tab characters ASAP.
  #6  
Old 30-Jul-2005, 21:36
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by Zorachus
Quote:
Originally Posted by WaltP
I don't see any conversion for the EOL characters 0x0A and 0x0D, as well as TAB:0x09, Form Feed:0x0C, and other legal characters in a file. This may be throwing off your multi-line encryption.
Indeed it may, and the reason that I didn't do this is because I didn't think to try the hex equivalent. However, as I can't do this with the method I was using, would strcpy work? It seems as if it would return a const char to char error. I would check this myself but currently don't have access to a compiler.
Not following... This is a character by character encryption, is it not? How does strcpy() fit in?

But yes, strcpy() only needs the ending '\0' to signify the end of a string. It will copy everything up to the ending null.



Quote:
Originally Posted by Zorachus
The manual encryption always works; it's something in the program.
Then you need to execute the program a step at a time and do a manual encryption. Then the moment the values don't match, viola! Do you know how to use the debugger for your compiler? If not, start adding printf()'s/cout's liberally, with pauses to follow the flow.


Quote:
Originally Posted by Zorachus
Essentially, that is what I am doing. To get around the problem of not being able to relate a const char to a number, I assigned the chars to their respective numbers in the array. EX: alph[0]="a".
But a const char is a number. Please describe (using code) what you can't do. I no unnerstan...
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #7  
Old 30-Jul-2005, 21:45
Zorachus's Avatar
Zorachus Zorachus is offline
Junior Member
 
Join Date: Jul 2005
Posts: 58
Zorachus will become famous soon enough
Quote:
Originally Posted by WaltP
Quote:
Originally Posted by Zorachus

Essentially, that is what I am doing. To get around the problem of not being able to relate a const char to a number, I assigned the chars to their respective numbers in the array. EX: alph[0]="a".
But a const char is a number. Please describe (using code) what you can't do. I no unnerstan...

Actually, I had no idea that const chars were numbers. Are you saying what you posted earlier
CPP / C++ / C Code:
key['b']=1;

would actually work? If I could implement it into a program by doing something like:

CPP / C++ / C Code:
key['b']=1;
if (k=key[1])
cout << key[1] << endl;

with the output as 'b?' This would greatly simplify this program. Again, I apologize for not attempting this myself. I will have access to a compiler later tonight.
  #8  
Old 30-Jul-2005, 22:20
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
[quote=Zorachus]Actually, I had no idea that const chars were numbers. [quote=Zorachus]
Of course they are. Everything in computers are just numbers...

Quote:
Originally Posted by Zorachus
Are you saying what you posted earlier
CPP / C++ / C Code:
key['b']=1;
would actually work?
Yes. Since 'b' == 98, this is the same as
CPP / C++ / C Code:
key[98]=1;


Quote:
Originally Posted by Zorachus
If I could implement it into a program by doing something like:

CPP / C++ / C Code:
key['b']=1;
if (k=key[1])
cout << key[1] << endl;

with the output as 'b?' This would greatly simplify this program. Again, I apologize for not attempting this myself. I will have access to a compiler later tonight.
Not quite. If you load key['b'] with 1, only key[98] is affected. The contents of key[1] still needs to be defined.

Concept:
Make two arrays, encrypt[] and decrypt[]. For each character you load the encrypt[] array with your encryption value,
CPP / C++ / C Code:
encrypt['a'] = 0;
encrypt['b'] = 1;
encrypt['c'] = 2;
encrypt['d'] = 3;
encrypt['e'] = 4;
  etc.
Then for every character being encrypted, you have the value for your algorithm.
Load decrypt[] with the reverse, of course...

Will this fit in with this encryption technique?
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #9  
Old 31-Jul-2005, 10:46
Zorachus's Avatar
Zorachus Zorachus is offline
Junior Member
 
Join Date: Jul 2005
Posts: 58
Zorachus will become famous soon enough
Well, after spending almost four hours trying to make it fit this algorithm, I would have to say no. Defining the character numbers myself seems to be much easier, mainly because it is in a more chronological order, which makes it easier to handle with loops. I'm still working on it though, thanks for the advice!
  #10  
Old 31-Jul-2005, 15:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,217
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by Zorachus
I'm still working on it though

I have a few suggestions:

1. Make a test program that reads chars from one file and puts each char to another file. Make absolutely certain that the result has exactly the number of chars as the original!!! Your method does not do this. I suggest infile.get() and outfile.put() to read and write a character at a time (Don't forget to open the files in binary mode. If you are on a Windows platform, it makes a difference.)

Don't do anything else until you can perform this step with large and small files. Make sure the resultant output file has exactly the same length (and contents) as the input file. If hyou can't do this, then I don't see any hope of ever recovering an original file from an encrypted one.

2. Since the Vernam algorithm works on a character "stream", you don't have to read the entire file into an array and generate a key array of that size, etc., etc. The original Vernam method worked on a character at a time using mechanical teletype and relay logic. Memories of the size of arbitrarily long messages simply weren't available then, and are not necessary now for this algorithm. Also, I can't see where you performed the exclusive-or operation that the Vernam algorithm specifies.

3. You say that you are using text files, but text files have non-printing characters: newlines, carriage-returns, tabs, and possibly others. Why not make an algorithm that will work with any input file? If you want to restrict the algorithm's usefulness to printable ascii characters, you still must take into account certain control characters --- otherwise how could you reproduce your original file, since it undoubtedly has control characters?

4. Here is a possible approach for encryption:

Code:
a. Inititialize the random number generator. Your method of srand(time(0)) is good! b. Open the input file for reading, the key file for writing and another output file to hold the encrypted data. c. Execute the following loop until there are no more characters in the input file: i. read a char from the input file ii. get a random byte from rand(). iii. write the random byte to the key file. iv. perform an exclusive-or between the input byte and the random byte. v. write the result of the exclusive-or to the encrypted data output file. d. Close the files and exit.

This way, you are generating the key, using the key for encryption, and writing the key file a byte at a time, all in one pass through the input, and with no memory arrays to clutter up the landscape. That's kind of the point of a streaming algorithm.

Now if your instructor told you to make these large arrays for the different items, you can still do it, but the calculations are the same.

4. Then for decryption:

Code:
a. Open the encrypted data input file and the key file for reading. b. Open the decrypted data output file for writing. c. Repeat the following loop until there are no more encrypted bytes to process: i. read a char from the encrypted data input file and read a char from the key file. ii. Perform an exclusive-or with these two bytes. iii. Write the result of the exclusive-or to the decrypted data output file. d. Close the files and exit.

Again, the processing is done a byte at a time with one pass through the message.

In summary:

I would write two separate programs to test encryption and decryption. I would put the file names hard-coded in the files to keep from having to enter file names for test purposes. I would use different file names so that I wouldn't destroy the original file each time I ran the program (your encoding program branch destroys the original file).

Then, after the algorithms for encrypting and decrypting are thoroughly tested, I would put them together into whatever program organization that the assignment specified.

Regards,

Dave
Last edited by davekw7x : 31-Jul-2005 at 16:20.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
encryption stuff Venom C Programming Language 8 12-Apr-2005 15:33
need help with encryption bobthesled C Programming Language 9 15-Feb-2005 12:24
Encryption implementation issue bigbangman C Programming Language 6 02-Sep-2004 12:21
Help with binary files (encryption?) pablowablo C++ Forum 6 28-Apr-2004 23:47

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:20.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.