Hi everyone,

I'm trying to code a program that uses RSA to encrypt your message. I assume you know it but if you don't know let me explain simply:

Your enter a word (actually it is a sentence but for now a word) then program will convert letters to numbers (like a=1 m=13 etc.) and group them. Use with all given values n=pq (n is the modulus). C=(m^e) (mod n ) where C is encrypted message m is numbers (converted from letters).

My problem is that it gives warning messages as below.

[IMG]http://i43.tinypic.com/14kbiux.jpg[/IMG]

Since i'm a beginner in C, it may have serious problems i'd be glad if you assist me to fix problems and improve my code.

Thanks

#include <stdio.h>
#include <string.h>
#include <conio.h>
char msg[50];
char *p_msg;
void encrypt(int b[50])
{
     int index,n,p=43,q=59,e=13;
     n=p*q;
     for (index=0;index<strlen(b);index++)
     {
         b[index]=(pow(b[index],e)) % n;
         }
     for (index=0;index<strlen(b);index++)
     {
         printf("Your encrypted message is: \n %d\t",b[index]);
         }
         }
void convert(char a[50])
{
    int valuea[50],index;
    for (index=0;index<strlen(a);++index)
    {
        if (a[index]>='a' && a[index]<='z')
        {
                            valuea[index]=a[index]-'a'+1;
                            }
        }
        for (index=0;index<strlen(msg);++index)
        {
            printf("%d\t",valuea[index]);
            }
    encrypt(valuea);
            }

int main(void)
{
    printf("Enter your message: ");
    scanf("%s",msg);
    p_msg=msg;
    convert(msg);
    printf("\nYour message was: %s",msg);
    getch();
    return 0;
}

lines 10 and 14: Attempting to call strlen() with an array of integers instead of a character array. See the parameter to that function. You might want to add another parameter to that function that is the number of entries in the array. void encrypt(int b[50], int size)

lines 10 and 14: Attempting to call strlen() with an array of integers instead of a character array. See the parameter to that function. You might want to add another parameter to that function that is the number of entries in the array. void encrypt(int b[50], int size)

Thanks for your reply. I thought since valuea array contains numbers it needs to be int type but char is okay i see. Now i corrected these but this for loop (in encrypt function) has a problem i think because it doesn't priny anything.

for (index=0;index<strlen(b);index++)
     {
         printf("Your encrypted message is: \n%d\t",fmod(b[index],n));
         }

Do you have any idea why it doesn't display anything on the screen like it doesn't have a printf command?

EDIT: Oh and here is the whole fixed code:

#include <stdio.h>
#include <string.h>
#include <conio.h>
char msg[50];
char *p_msg;
void encrypt(char b[50])
{
     int index,n,p=43,q=59,e=13;
     n=p*q;
     for (index=0;index<strlen(b);index++)
     {
         b[index]=(pow(b[index],e));
         }
     for (index=0;index<strlen(b);index++)
     {
         printf("Your encrypted message is: \n %d\t",fmod(b[index],n));
         }
         }
void convert(char a[50])
{
    char valuea[50];
    int index;
    for (index=0;index<strlen(a);++index)
    {
        if (a[index]>='a' && a[index]<='z')
        {
                            valuea[index]=a[index]-'a'+1;
                            }
        }
        for (index=0;index<strlen(msg);++index)
        {
            printf("%d\t",valuea[index]);
            }
    encrypt(valuea);
            }

int main(void)
{
    printf("Enter your message: ");
    scanf("%s",msg);
    p_msg=msg;
    convert(msg);
    printf("\nYour message was: %s",msg);
    getch();
    return 0;
}

Since you put random integers in that character array strlen() may still not work if there are embedded 0s in that array. The safest way is to pass the length of the array as another parameter.

>> printf("Your encrypted message is: \n %d\t",fmod(b[index],n));

fmod() returns a float, not an integer. use "%f", not "%d"

Well, now i am confused. if i make valuea array as char type i can use strlen to send length of it to parameter size. (Also i should send size of it by using strlen, right?) But other parameter is int b[50] but sent array (which is valuea) is char so won't this be a problem? Also now i'm getting -1943 for printf values. (in encrypt function). And there is a warning in line 31.

Latest code:

#include <stdio.h>
#include <string.h>
#include <conio.h>
char msg[50];
char *p_msg;
void encrypt(int b[50], int size)
{
     int index,n,p=43,q=59,e=13;
     n=p*q;
     for (index=0;index<size;index++)
     {
         b[index]=(pow(b[index],e));
         printf("Your encrypted message is: \n %.0f\t",fmod(b[index],n));
         }
         }
void convert(char a[50])
{
    char valuea[50];
    int index;
    for (index=0;index<strlen(a);++index)
    {
        if (a[index]>='a' && a[index]<='z')
        {
                            valuea[index]=a[index]-'a'+1;
                            }
        }
        for (index=0;index<strlen(msg);++index)
        {
            printf("%d\t",valuea[index]);
            }
    encrypt(valuea,strlen(valuea));
            }

int main(void)
{
    printf("Enter your message: ");
    scanf("%s",msg);
    p_msg=msg;
    convert(msg);
    printf("\nYour message was: %s",msg);
    getch();
    return 0;
}

>>(Also i should send size of it by using strlen, right?

No. Count the length as you enter the characters into the array. The function convert() is already doing that -- variable index. Just pass the value of index instead of strlen().

I think we finally have something. Because for small letters (like a b c) it works but for others (like u r k) it just gives -1943. I think this is because of data type. (which is int). u is 21 and 21^13 is really big number so i thought it would be okay if i change types (valuea and b[50]) to double it all becomes 0. If i change them to float then i still got wrong values. Any idea?

#include <stdio.h>
#include <string.h>
#include <conio.h>
char msg[50];
char *p_msg;
void encrypt(int b[50], int size)
{
     int index,n,p=43,q=59,e=13;
     n=p*q;
     for (index=0;index<size;index++)
     {
         b[index]=(pow(b[index],e));
         printf("Your encrypted message is: \n %.0f\t",fmod(b[index],n));
         }
         }
void convert(char a[50])
{
    int valuea[50];
    int index;
    for (index=0;index<strlen(a);++index)
    {
        if (a[index]>='a' && a[index]<='z')
        {
                            valuea[index]=a[index]-'a'+1;
                            }
        }
        for (index=0;index<strlen(msg);++index)
        {
            printf("%d\t",valuea[index]);
            }
    encrypt(valuea,index);
            }

int main(void)
{
    printf("Enter your message: ");
    scanf("%s",msg);
    p_msg=msg;
    convert(msg);
    printf("\nYour message was: %s",msg);
    getch();
    return 0;
}

I think we finally have something. Because for small letters (like a b c) it works but for others (like u r k) it just gives -1943. I think this is because of data type. (which is int). u is 21 and 21^13 is really big number so i thought it would be okay if i change types (valuea and b[50]) to double it all becomes 0. If i change them to float then i still got wrong values. Any idea?

#include <stdio.h>
#include <string.h>
#include <conio.h>
char msg[50];
char *p_msg;
void encrypt(int b[50], int size)
{
     int index,n,p=43,q=59,e=13;
     n=p*q;
     for (index=0;index<size;index++)
     {
/*
         b[index]=(pow(b[index],e));
         printf("Your encrypted message is: \n%.0f\t",fmod(b[index],n));
*/
        // Use this instead

        temp= (pow(b[index],e));
       printf("Your encrypted message is: %.0f\n",fmod(temp,n));

         }
         }
void convert(char a[50])
{
    int valuea[50];
    int index;
    for (index=0;index<strlen(a);++index)
    {
        if (a[index]>='a' && a[index]<='z')
        {
                            valuea[index]=a[index]-'a'+1;
                            }
        }
        for (index=0;index<strlen(msg);++index)
        {
            printf("%d\t",valuea[index]);
            }
    encrypt(valuea,index);
            }

int main(void)
{
    printf("Enter your message: ");
    scanf("%s",msg);
    p_msg=msg;
    convert(msg);
    printf("\nYour message was: %s",msg);
    getch();
    return 0;
}

Make the correction suggested above ..... I think that will solve your problem

abhimanipal, thanks for your reply. Yes it looks like it is working now but somehow there is a problem with u letter. U is 21th number and (21^13)%2537 equals to 41 but program keeps giving me 36 which is wrong. Others are fine. I changed data type to double (even long double) but still no luck.But if change e p and q to small numbers then it works. I still don't know why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.