Quote (Xarai @ Mar 26 2013 02:09pm)
question - Modify the Cryptogram program to use a different type of key
system or algorithm. Consider using a user-defined key or a
different character set.
problem - not sure what they are meaning and the teacher is pretty much adamant on not helping
change the way the cryptographic functions work to change the output. go google XOR and implement that in your encrypt and decrypt functions rather than adding your random value.
you can also ask the user for a key to use instead of generating a random value. you would pass this key to the function.
or you can generate a random string the length of the text and xor each character with the key
Code
void gen_key(char *key, char *plaintext)
{
int len = strlen(plaintext);
//depending if you allocated memory or not for key you might need to call malloc to allocate memory. rmeember to free afterwards.
for(int i = 0; i < len; i++)
{
*key = rand()%26+'a';
}
}
then after you can call your encrypt function with the key
Code
void encrypt(char *key, char *plaintext)
{
int len = strlen(plaintext);
for(int i = 0; i < len; i++)
{
//implement xor where you XOR i of both arrays together storing them in plaintext
}
}
of course there is no error checking here which you might want to add, such as user input for plaintext is not greater than what the array can handle (to prevent buffer over flows). same with the key.
edit:: rather than `while(sMessage[x])` it's much cleaner to do `while(*sMessage)` it will auto increment your pointer for you until a null byte is reached (standard in all C strings) and it will eliminate the use of a "x" variable to increment.
This post was edited by AbDuCt on Mar 26 2013 12:27pm