Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Encrption 1

Status
Not open for further replies.

Scooby316

IS-IT--Management
Jan 30, 2003
32
0
0
US
Hi all

I am having some bother with a while loop that is driving me nuts. The code works but doesn't seem to encrpt the inputted text. It works on that it will encrpt until a higher letter is entered that small a to z.

c = (unsigned char)getchar(); /* read in first character */

/*
* main encryption/decryption loop
* loop while characters are lower case letters or spaces
*/
while (c>='a' && c<='z' || c==' ')
{
if (c==' ')
{
switch (type)
{
case 'd':
c = decrypt;
default:
c = encrypt;
}
}
putchar(c); /* display encrypted/decrypted character */
c = (unsigned char)getchar(); /* get next character */
}
/*
* end of main while loop
*/
 
What are encrypt and decrypt in your code?
If they're meant to be functions, they're not being called.

Also, your code only actually tries to encrypt/decrypt space characters. Everything else is unaffected.

--
 
Hi Salem

the other code to de/ encrypt is

/*
* function encrypt()
* ==================
* encrypts a single character
*/
unsigned char encrypt(unsigned char letter)
{
unsigned char r;
r = (unsigned char)random(26);
letter = letter + r;
if (letter>'z')
{
letter = letter - 26;
}
return(letter);
}
/*
* end of encrypt()
*/


/*
* function decrypt()
* ==================
* decrypts a single character
*/
unsigned char decrypt(unsigned char letter)
{
{
unsigned char r;
r = (unsigned char)random(26);
letter = letter - r;
if (letter>'z')
{
letter = letter + 26;
}
return(letter);
}
/*
* end of decrypt()
*/
 
> c = encrypt;
This doesn't call the function

Use this
c = encrypt(c);

--
 
Hi Salem

Cheers got the encryption working? I don't think the decrpytion process is working know. After encryption the word when put into the decryption comes back with a differnet word. The fun of it all.
 
Cheers Salem

well sorted now!

Me very happy and can now sleep!
 
Well your decrypt() isn't the inverse of your encrypt() for starters.

And you need to make sure you reset the seed value for your random number generator.



--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top