GIDForums  

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

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Apr-2005, 02:47
Venom Venom is offline
New Member
 
Join Date: Mar 2005
Posts: 4
Venom is on a distinguished road

encryption stuff


Hi, I was wondering if you could help…

I need to write a program to carry out encryption using a SSC. It needs to prompt the user for 3 filenames; key, plaintext, and cipertext files, then open the files. It needs to read the key into array Key, while (plaintextchar read from plaintextfile is not equal to EOF) convert char into ciphertextchar using key, write ciphertextchar to ciphertext file, and finally close files.

I have this already… it reads the key from a text file, and stores it in an array,the it prints the contents on screen:



CPP / C++ / C Code:
#include <stdio.h>

int main()
{
  FILE *in;
  int c; 
  int i; 
  int f; 
  char keyfn[100];
  char key[25]; 
  printf("Type the key filename and extension (key.txt):\n\n"); 
  gets(keyfn);
  
  
  if((in=fopen(keyfn,"r"))==NULL) 
  {
 printf("Unable to open file\n\n"); 
 exit(0); 
  }
  i=0;
    
  
  while((c=fgetc(in))!=EOF) 
    {
		key[i]=c;
		i++;
  } 

  
  for(f=0;f<=(i-1);f++)

  {
	printf("%c",key[f]);
  }
    fclose(in); 
   }
Last edited by dsmith : 10-Apr-2005 at 06:41. Reason: Please use [c] & [/c] when posting C code
  #2  
Old 10-Apr-2005, 14:13
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Warning: Do not use exit() unless you are using C and ONLY C. If you use C++ classes or any libraries that include classes, you must NOT use exit().

Ok, so what are you having trouble with?
__________________
-Aaron
  #3  
Old 10-Apr-2005, 14:31
Venom Venom is offline
New Member
 
Join Date: Mar 2005
Posts: 4
Venom is on a distinguished road
Well I could kind of get the prompts for the three files working (though I posted the code here without because I didn't have it completely sorted out). I screwed up trying to open them all and storing them in arrays. The files are ...one for the key - so A is W, B is E, etc... one for plaintext (like the english alphabet), and one for cipertext (what you want the letters to be substituted for, so W as A, etc).
  #4  
Old 10-Apr-2005, 17:58
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
if you can use C++, I would suggest using a map, or two. maps are like arrays, except that the key type can be anything you want instead of just int. so if you declared a
CPP / C++ / C Code:
map<string, bool> m_strb;
then you could access elements like
CPP / C++ / C Code:
m_strb["mymap"] = true;
So you would want to have a map<char, char>. the keys would be the regular alphabet, and then you could fill the values with the input from key.txt. Then, translation would be easy. You'd be able to do this:
CPP / C++ / C Code:
map<char, char> alphabets;
alphabets['a'] = 'z'; //or whatever
... //put elements in alphabets with keys of b thru z and use key.txt to get the values

char *outputchar = new char[strlen(plaintextchar)];
for (int i = 0; i < strlen(plaintextchar); ++i) outputchar[i] = alphabets[plaintextchar[i]]; //use the corresponding letter by looking it up in the map
  #5  
Old 10-Apr-2005, 23:37
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I think that's too complicated for him. Seems like this is a first programming course to me. So, you are having trouble opening the files and sorting their data into arrays? Here's how it should be done...

CPP / C++ / C Code:
// open 1st file
// if the file failed to open, display an error message and exit the program
// read each item from the file into the array
// open 2nd file
// if the file failed to open, display an error message and exit the program
// read each item from the file into the next array
// open the 3rd file
// if the file failed to open, display an error message and exit the program
// read each item from the file into the last array

So, the first thing I would do is write a function that reads data from a file and sorts it into an array, given the name of the file and the size of the array as parameters. Once you've got that done, the rest of the work lies in what to do with the data in the arrays. If you are having trouble storing data from the file into the array, here's a quick example of how it might be done.

CPP / C++ / C Code:
int main()
{
  FILE *fptr = fopen("integers.txt","rt"); // text-mode, read-only
  int numbers[10] = {0};
  int i = 0;

  while ( i < 10 )
      numbers[i++] = fgetc(fptr);

  return 0;
}

By the way, ubergeek, what the heck kind of identifier is m_strb? I hope you don't write all of your identifiers like that!
__________________
-Aaron
  #6  
Old 11-Apr-2005, 12:10
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
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 aaroncohn
I think that's too complicated for him.
Agree...

Quote:
Originally Posted by aaroncohn
So, the first thing I would do is write a function that reads data from a file and sorts it into an array, given the name of the file and the size of the array as parameters.
Not me.

The first thing I'd do is write functions to read the data from the files, one function per file. Write them one at a time. Print the data as it's read to verify proper reading. When working, remove the print section.

The second thing I'd do is write the output function, writing all the data arrays and make sure the arrays are still displayed properly.

The third thing is to write the encryption function and call it between your input and output functions.

Once the program is working, the last thing is to remove the unnessesary output.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #7  
Old 12-Apr-2005, 11:47
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
If the data is stored into each array exactly the same way for all 3 functions, then isn't it kind of silly to have 3 functions instead of 1?
__________________
-Aaron
  #8  
Old 12-Apr-2005, 13:44
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
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 aaroncohn
If the data is stored into each array exactly the same way for all 3 functions, then isn't it kind of silly to have 3 functions instead of 1?
It would be if the data is stored into each array exactly the same and you are an experienced programmer. But for a newbie it keeps things separate and easier to follow. Combine them later once you understand the process.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #9  
Old 12-Apr-2005, 14:33
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough
Quote:
By the way, ubergeek, what the heck kind of identifier is m_strb? I hope you don't write all of your identifiers like that!
no, i don't. usually they are much more descriptive. that was just an example, like: m_ = map, str=string, b=bool, so "map--string,bool"
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

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
What is the best place to buy computer stuff? Bulkhead Computer Hardware Forum 21 16-Jun-2005 06:50
need help with encryption bobthesled C Programming Language 9 15-Feb-2005 11:24
.o compile UNIX stuff ... help please! crq C++ Forum 5 28-Jan-2005 07:43
Help with binary files (encryption?) pablowablo C++ Forum 6 28-Apr-2004 22:47
RE: Sessions stuff for a confused programmer Dagma20 MySQL / PHP Forum 1 19-Mar-2004 05:10

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

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


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