d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > C Programming > Cryptogram
Add Reply New Topic New Poll
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 26 2013 12: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.



Code
/* 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.
system or algorithm. Consider using a user-defined key or a
different character set.*/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
//function prototypes
void encrypt(char [], int);
void decrypt(char [], int);
main()
{
char myString[21] = {0};
int iSelection = 0;
int iRand;
srand (time(NULL));
iRand = (rand() % 4) + 1; // random #, 1-4
while ( iSelection != 4 ) {
printf("\n\n1\tEncrypt Clear Text\n");
printf("2\tDecrypt Cipher Text\n");
printf("3\tGenerate New Key\n");
printf("4\tQuit\n");
printf("\nSelect a Cryptography Option: ");
scanf("%d", &iSelection);
switch (iSelection) {
case 1:
printf("\nEnter one word as clear text to encrypt: ");
scanf("%s", myString);
encrypt(myString, iRand);
break;
case 2:
printf("\nEnter cipher text to decrypt: ");
scanf("%s", myString);
decrypt(myString, iRand);
break;
case 3:
iRand = (rand() % 4) + 1; // random #, 1-4
printf("\nNew Key Generated\n");
break;
} //end switch
} //end loop
} //end main
void encrypt(char sMessage[], int random)
{
int x = 0;
//encrypt the message by shifting each characters ASCII value
while ( sMessage[x] ) {
sMessage[x] += random;
x++;
} //end loop
x = 0;
printf("\nEncrypted Message is: ");
//print the encrypted message
while ( sMessage[x] ) {
printf("%c", sMessage[x]);
x++;
} //end loop
} //end encrypt function
void decrypt(char sMessage[], int random)
{
int x = 0;
x = 0;
//decrypt the message by shifting each characters ASCII value
while ( sMessage[x] ) {
sMessage[x] = sMessage[x] - random;
x++;
} //end loop
x = 0;
printf("\nDecrypted Message is: ");
//print the decrypted message
while ( sMessage[x] ) {
printf("%c", sMessage[x]);
x++;
} //end loop
} //end decrypt function


problem - not sure what they are meaning and the teacher is pretty much adamant on not helping
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 26 2013 12:24pm
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
Member
Posts: 51
Joined: Feb 7 2013
Gold: 0.00
Mar 26 2013 12:39pm
lol found out why he wasnt helping XD
this was extra credit
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll