11 Replies - 6520 Views - Last Post: 23 March 2011 - 07:24 AM Rate Topic: -----

#1 wsymaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 11

encrypt and decrypt.

Posted 22 March 2011 - 11:09 AM

#include <stdio.h>
#include <string.h>

char encrypt( char [] ); //function prototype
char decrypt( char [] );

char encrypt(char msg[]) //function definition
{
     int i;
    for( i = 0; i < 4; i++)
    {
            char enc;
            char key;
            enc[i] = msg [key[i]]; 
    } 
    return enc;
}

char decrypt(char enc[])
{
     char msg;
     int i;
    for( i = 0; i < 4; i++)
    {
        char msg;
        char key;
        msg[i] = enc[key[i]];
    } 
    return msg;
}

int main()
{
    char msg;
    char enc;
    
    char msg[ ] = "this is a secret!";
    printf("the original message is :%s\n", msg);
 
    
char encrypt(char msg[]); // function call
char decrypt(char enc[]);

printf("the encrpyted message is : %s", enc);
printf("the decrpyted message is : %s", msg);
  
    system ("PAUSE");
    return 0;
}






i do it in functions but i am confused about the use of strings and arrays.

there are several errors:


C:\Users\Asus User\Documents\cdev\sc5.c In function `encrypt':
14 C:\Users\Asus User\Documents\cdev\sc5.c subscripted value is neither array nor pointer
16 C:\Users\Asus User\Documents\cdev\sc5.c `enc' undeclared (first use in this function)
C:\Users\Asus User\Documents\cdev\sc5.c In function `decrypt':
C:\Users\Asus User\Documents\cdev\sc5.c In function `main':
37 C:\Users\Asus User\Documents\cdev\sc5.c conflicting types for 'msg'
34 C:\Users\Asus User\Documents\cdev\sc5.c previous declaration of 'msg' was here

MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

Is This A Good Question/Topic? 0
  • +

Replies To: encrypt and decrypt.

#2 TMKCodes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 48
  • View blog
  • Posts: 440
  • Joined: 21-March 09

Re: encrypt and decrypt.

Posted 22 March 2011 - 11:23 AM

Please use the code tags!
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: encrypt and decrypt.

Posted 22 March 2011 - 11:52 AM

Read my tutorials on functions, linked from my signature.
Was This Post Helpful? 0
  • +
  • -

#4 wsymaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 11

Re: encrypt and decrypt.

Posted 22 March 2011 - 06:30 PM

#include <stdio.h>
#include <string.h>

char encrypt( char [] ); //function prototype
char decrypt( char [] );

char encrypt(char msg[]) //function definition
{
     int i;
    for( i = 0; i < 4; i++)
    {
            char enc;
            char key;
            enc[i] = msg [key[i]]; 
    } 
    return enc;
}

char decrypt(char enc[])
{
     char msg;
     int i;
    for( i = 0; i < 4; i++)
    {
        char msg;
        char key;
        msg[i] = enc[key[i]];
    } 
    return msg;
}

int main()
{
    char msg;
    char enc;
    
    char msg[ ] = "this is a secret!";
    printf("the original message is :%s\n", msg);
 
    
char encrypt(char msg[]); // function call
char decrypt(char enc[]);

printf("the encrpyted message is : %s", enc);
printf("the decrpyted message is : %s", msg);
  
    system ("PAUSE");
    return 0;
}




Was This Post Helpful? 0
  • +
  • -

#5 wsymaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 11

Re: encrypt and decrypt.

Posted 22 March 2011 - 07:11 PM

char encrypt(char msg[]) //function definition
{
     int i; 
     char enc[20];
    for( i = 0; i < 20; i++)
    {
           
            char key[20];
            enc[i] = msg [key[i]]; // sramble
 }
    return enc;
}



some of the error messages are :
11 C:\Users\Asus User\Documents\cdev\sc5.c invalid initializer
32 C:\Users\Asus User\Documents\cdev\sc5.c [Warning] return makes integer from pointer without a cast
Was This Post Helpful? 0
  • +
  • -

#6 wsymaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 11

Re: encrypt and decrypt.

Posted 22 March 2011 - 07:23 PM

#include <stdio.h>
#include <string.h>

char encrypt( char msg[] ); //function prototype

int main()
{
   
    char m[20] = "this is a secret!";
     char e[20]= encrpyt(m);// function call

     printf("the encrpyted message is : %s", e);

     system ("PAUSE");
    return 0;
}

char encrypt(char msg[]) //function definition
{
     int i; 
      char enc;
      char key;
    for( i = 0; i < 4; i++)
    {
          enc[i] = msg [key[i]]; 
    } 
    return enc;
}







still have error : 25 C:\Users\Asus User\Documents\cdev\sc5.c subscripted value is neither array nor pointer
how to fix this?
Was This Post Helpful? 0
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: encrypt and decrypt.

Posted 22 March 2011 - 07:35 PM

char key;

key is a character, not an array. so when you access key with the [] operator it's an error right?

dose this seem right if key is not an array?
enc[i] = msg [key[i]];

Was This Post Helpful? 0
  • +
  • -

#8 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: encrypt and decrypt.

Posted 22 March 2011 - 07:41 PM

As stated by ishkabible you define two char variables named enc and key but try to use them as char arrays([]) in the loop.
char encrypt(char msg[]) //function definition
{
     int i; 
      char enc;
      char key;
    for( i = 0; i < 4; i++)
    {
          enc[i] = msg [key[i]]; 
    } 
    return enc;
}



Also you never initialize either of your variables. Why are you passing msg into your function, you never use it.

Jim

This post has been edited by jimblumberg: 22 March 2011 - 07:42 PM

Was This Post Helpful? 0
  • +
  • -

#9 wsymaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 11

Re: encrypt and decrypt.

Posted 23 March 2011 - 05:33 AM

#include <stdio.h>
#include <string.h>

void encrypt (char e[], char const enc[]);
void decrypt( char * ePtr );

void encrypt (char e[],char const enc[])
{
     int i;
     for(  i=0; enc != '\0'; ++i ) 
     {
     e[i]=enc[i];
     ++e[i];
    
     }
}

void decrypt( char * ePtr )
 {
     for( *ePtr=0; * ePtr != '\0'; ++* ePtr ) 
     --(* ePtr);
 }

int main()
{
    
    printf(" :::::::::::::::::::: WELCOME TO THE SECRET CODE PROGRAM :::::::::::::::::\n\n\n");
// create a string to encrypt
printf("Please enter your message : \n");
char string[200];
gets(string);
char string2[200];
printf("The original message is : %s\n\n", string);
 
encrypt( string2,string);
// call to the function encrypt( )
printf("The secret code is : %s\n\n", string2);

decrypt( string );
// call to the function decrypt( )
printf("The decrpyted message is : %s\n\n", string);


system ("PAUSE");
return 0;
}




i try a different code for this encryption. there is no error but after executing the code and entering the message in the black screen, the program stop working : "secret.exe stopped working". i have tried many times using different computers but still not working. what is the problem actually?
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: encrypt and decrypt.

Posted 23 March 2011 - 07:02 AM

Please explain in plain English how you want to encrypt your data. What encryption method are you trying to use?

Since most of errors in the code you posted deal with the differences between a char and a character array please study these links:Variables, Arrays and Character sequences. You may also want to read this link on pointers.

If you have any questions about any of the information in the above links, tell us which link you have questions about and then ask specific questions about that link.

Jim
Was This Post Helpful? 0
  • +
  • -

#11 wsymaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 11-January 11

Re: encrypt and decrypt.

Posted 23 March 2011 - 07:14 AM

i am sorry. the encryption is just simple actually. let say the word id 'plus', the encrypted msg would be 'qmvt', which will take the letter after each character. the last code that i posted above has no error but after i typed the message, the program is kind of stuck. what actually happened during the execution? can you help me figure it out?
btw, i will read those links for better understanding about arrays n char, tq.
Was This Post Helpful? 0
  • +
  • -

#12 jimblumberg  Icon User is online

  • member icon


Reputation: 4232
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: encrypt and decrypt.

Posted 23 March 2011 - 07:24 AM

Your loop in encrypt is an endless loop, probably because you don't understand the difference between a char and an array of char.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1