0
#include<stdio.h>
#include<time.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>

int p,q,r;
int i,t[100],t1[100],t2[100];
char M[100],C[100];
int phi,n,e=53,d=11;

int cipher(int t)
{
    int i,temp;
    temp = 1;
    for(i=0;i< e;i++)
        temp = temp * t % n;
    temp = temp % n;
    return temp;
}

int plain(int t)
{
    int temp,i;
    temp = 1;
    for(i=0; i< d; i++)
        temp=temp * t % n;
    temp = temp % n;
    return temp;
}

void prime()
{
    int i;
    int flag=0;

    long int r;
    randomize();

    r=random(100);

      //    printf("%ld\t",r);
    for(i=2;i<=(r/2);i++)
    {
        if(r%i==0)
        {
            flag=1;
            break;
        }
    }
    if(flag==0)
    {
        if(q==0)
            q=r;
        else if(p==0)
            p=r;
    }
}
int gcd( int m, int n)
{
    while( m!= n)
    {
        if( m > n)
            m= m - n;
        else
            n= n - m;
    }
    return ( m);
}
void main()
{
    int s,k,j,count,sum;
    clrscr();
    do
    {
        q=0;
        while(q==0)
            prime();

        p=0;
        while(p==0)
            prime();
    }while(gcd(p,q)!=1) ;

    printf("\n\np = %d\nq = %d",p,q);

    n = p*q;
    phi=(p-1)*(q-1);

    //
    printf("\n\tN = %d",n);
    printf("\n\tF(n) = %d",phi);

    do
    {
        randomize();
        r=random(phi);
        prime();
        e=r;
    }while((gcd(e,phi)!=1) && e<1 || e>500);
    printf("\n\ne = %d",e);

       //   e=3;

       //   d=1;
    do
    {
        s=(d*e)%phi;
        d++;
    }while(s!=1);
    d=d-1;

    printf("\n\nd = %d",d);

    printf("\n\tPublic Key\t: {%d,%d}",e,n);
    printf("\n\tPrivate Key\t: {%d,%d}",d,n);


    //
    printf("\n\nPlain Text:   ");
    gets(M);

**  printf("\nEncrypted Text:");
    for(i=0;i<strlen(M);i++)
    {
        t[i] = (int)M[i];
        t1[i] = cipher(t[i]);
        printf("%c ",t1[i]);
    }
    printf("\n\nDecrypted Text: ");
    for(i=0;i<strlen(M);i++)
    {
        t2[i] = plain(t1[i]);
        printf("%c ",t2[i]);
    }

    getch();
}

My problem is, e and k keys are generated incorrect. Can anyone please help wit this?

2
Contributors
1
Reply
18
Views
3 Years
Discussion Span
Last Post by JasonHippy
0

Just spotted that this post hasn't had a reply yet... And I can see why there are no replies now! :/

@Ikhushee: If you would use more meaningful variable names and include some comments in your code, it would allow others here to better understand the intent behind your code/logic.

As it stands, your code is a bit of a mess. I'm not sure exactly what you're trying to do in several places. It's evident that you are trying to create encryption keys and encrypt and decrypt some text using them; But because the variables have unintuitive single letter names and because there are no comments in the code, it's not really obvious what you are doing or what algorithms you are trying to implement. You need to write code so that other programmers can read and understand the logic/intent behind it!

Naming your variables sanely and commenting your code clearly will give others a strong idea of what you are trying to do in your code and will make it easier for others to see any logical errors you've made.

One additional thing I will say is: try not to use too many global variables. Try to keep variables within their particular scope.

Most of the global variables you've declared look like they are only used in main, so they should be declared in main rather than as globals. Global variables should be used sparingly and only where necessary. Globals are visible/in scope in all functions in your program. By indiscriminately using globals, you could easily introduce tricky bugs into your program. Particularly if you have a lot of different functions accessing/modifying them! Try to keep variables as local as they need to be and only use globals when absolutely necessary!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.