Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 30th, 2005, 5:37 AM   #1
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3 ivan is on a distinguished road
Text encryption in C

I was very bored in school so i made a sort of xor encryption in c.
Here is the source code:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
  char fileName[30];
  int temp;
  FILE *fp;
  FILE *fp1;
  
  printf( "Text Encoder 1.0 \n\n" );
  printf( "Enter the file name you want to encrypt/decrypt: \n" );
  printf( "The file must be in the same folder as the program. \n" );
  printf( "The lenght of the file name may not contain spaces nor more then 30 chars. \n" );
    
  scanf( "%s", &fileName );
  
  if((fp = fopen( fileName, "r" )) == NULL) 
  {
         printf( "That file doesn't exist!!! \n\n" );
         system( "PAUSE" );
         return 0;
  }

  fp1 = fopen( "TE_file.txt", "w" );
  
  printf( "Processing.... please wait \n" );
  
  while( temp != -1 )
  {
         temp = fgetc( fp );
         if( temp == -1 ) break;
         temp = temp ^ 1926701;
         fputc( temp, fp1 );
  } 
  
  printf( "Done. \n \n" );
  
  system("PAUSE");	
  return 0;
}
ivan is offline   Reply With Quote
Old Sep 30th, 2005, 6:01 AM   #2
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Quote:
  printf( "The lenght of the file name may not contain spaces nor more then 30 chars. \n" );
    
  scanf( "%s", &fileName );
gonna check it out in a sec, but i noticed this. This code is quite unsafe, and it sucks you can't use spaces. You could do this instead:
fgets(fileName, 30, stdin);
Polyphemus_ is offline   Reply With Quote
Old Sep 30th, 2005, 6:08 AM   #3
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3 ivan is on a distinguished road
I know, it is my first prog in c, after 1 hour of reading the c documentation.
I want to learn c so I am happy that it works.
The experience will come later.
ivan is offline   Reply With Quote
Old Sep 30th, 2005, 6:36 AM   #4
Silvanus
Hobbyist Programmer
 
Silvanus's Avatar
 
Join Date: Aug 2005
Location: Hiding from... them...
Posts: 110
Rep Power: 4 Silvanus is on a distinguished road
Quote:
Originally Posted by ivan
I know, it is my first prog in c, after 1 hour of reading the c documentation.
I want to learn c so I am happy that it works.
The experience will come later.
A bit of advice from a fellow beginner programmer: even if you're just learning a language, try to make your code as close to perfect as possible. Polyphemus_ is giving you advice; take it and you'll learn more.
Silvanus is offline   Reply With Quote
Old Sep 30th, 2005, 7:49 AM   #5
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
cool. here is my XOR function in C#...
If you want to, feel free to convert it to C/C++ ...


static string MyXOR (string data)
		{
			string key = "test";
			string retValue = "";
 
			int i = 0;
			int x = 0;
 
			int[] cipher = new int[data.Length];
 
			x = 0;
			for (i = 0; i < data.Length; i++)
			{
				//Console.Write((char)((data[i] ^ key[x])));
				retValue = retValue + (char)((data[i] ^ key[x]));
				cipher[i] = (data[i] ^ key[x]);
				x++;
 
				if (x >= key.Length)
					x = 0;
			}
 
			return retValue;
		}

EDIT: These code tags hammers my identations all of the time
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 30th, 2005, 7:54 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I have problems with it too, IR. Your source editor MUST have the ability to replace the tabs with the appropriate number of spaces before you copy and paste. Unilaterally taking that step "as you work" tends to screw up auto-indenting, so the visible page often isn't suitable.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 30th, 2005, 8:19 AM   #7
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
Hmmm, seems like I could toggle that feature some where in the IDE options. I'll look into it. Thanks for the info
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Sep 30th, 2005, 12:24 PM   #8
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3 ivan is on a distinguished road
Quote:
Originally Posted by Silvanus
A bit of advice from a fellow beginner programmer
I am not really a beginner in programming, I am just a beginner in C.
ivan is offline   Reply With Quote
Old Sep 30th, 2005, 1:19 PM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I would be interested in hearing you reassess your own statement in a mere five years.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 30th, 2005, 1:27 PM   #10
ivan
Professional Programmer
 
ivan's Avatar
 
Join Date: Sep 2005
Location: serbia & montenegro
Posts: 484
Rep Power: 3 ivan is on a distinguished road
What are you trying to say?
Well, maybe i am not a guru like you DaWei , but I am neither a very very beginner...
ivan is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC