The Programming Project: public key
Showing posts with label public key. Show all posts
Showing posts with label public key. Show all posts

Thursday, March 21, 2013

C Program #15


/*Dynamic Implementation of Stack With Basic operations!*/

#include<stdio.h>
#include<malloc.h>
#define TRUE 1
#define FALSE 0

struct stack
                        {
                        int item;
                        struct stack *right;
                        };
typedef struct stack node;
node *push(node *);
node *push1(node *,int);
void display(node *);
int Isempty(node *);
void top2bottomu(node *);
void top2bottom(node *);
void pop(node *);
int pop1(node *);
void duplicate(node *);
void filestack(node *, FILE *fp);
void stack2(node *);
int Isequal(node *,node *);
int length(node*);
int main()
{
int choice;
FILE *fp;
fp=fopen("data.txt","w");
node *root,*data,*root1,*data1,*root2,*data2;
root=(node *)malloc(sizeof(node));
root->right=NULL;
data=root;
root1=(node *)malloc(sizeof(node));
root1->right=NULL;
data1=root1;
root2=(node *)malloc(sizeof(node));
root2->right=NULL;
data2=root2;
            do
            {
                        printf("\n Press 1 to push a element:");
                        printf("\n Press 2 for bottom to top:");
                        printf("\n Press 3 to check for empty:");
                        printf("\n Press 4 to print elements from top to bottom,stack becoming empty:");
                        printf("\n Press 5 to pop:");
                        printf("\n Press 6 to print elements from top to bottom,stack unchanged:");
                        printf("\n Press 7 to duplicate the top element of the stack:");
                        printf("\n Press 8 to read from a file and print in reverse order:");
                        printf("\n Press 9 to push element in second stack:");
                        printf("\n Press 10 to display second stack:");
                        printf("\n Press 11 to check if two stacks are equal:");
                        printf("\n Enter choice:");
                        printf("\n Type 12 to exit:");
                        scanf("%d",&choice);
                        switch(choice)
                                    {
                                                case 1:
                                                data=push(data);
                                                data->right=NULL;
                                                break;
                                                case 2:
                                                display(root);
                                                break;
                                                case 3:
                                                if(Isempty(root))
                                                            printf("\n Stack is empty:\n");
                                                else
                                                            printf("\n Stack is not empty:\n");
                                                break;
                                                case 4:
                                                top2bottom(root);
                                                data=root;
                                                break;
                                                case 5:
                                                pop(root);
                                                data=root;
                                                break;
                                                case 6:
                                                top2bottomu(root);
                                                break;
                                                case 7:
                                                duplicate(root);
                                                break;
                                                case 8:
                                                filestack(root1,fp);
                                                break;
                                                case 9:
                                                data2=push(data2);
                                                data2->right=NULL;
                                                break;
                                                case 10:
                                                display(root2);
                                                break;
                                                case 11:
                                                if(Isequal(root,root2))
                                                            printf("\n Stack are equal:\n");
                                                else
                                                            printf("\n Stack are not equal:\n");
                                                break;
                                                default:
                                                break;

                                    }
                        }
                        while(1<=choice && choice<=11);
            return 0;
 }
int Isequal(node *data1,node *data2)
            {
            int flag=FALSE,i;
            if(Isempty(data1) || Isempty(data2))
                        {
                        if(Isempty(data1) && Isempty(data2))
                                    return (TRUE);
                        else
                                    return (FALSE);
                        }
            else
                        {
                         if(length(data1)!=length(data2))
                         //         {printf("{%d,%d}\n",length(data1),length(data2));
                                    return (FALSE);//}
                         else
                                    {
                                    for(i=0;i<=length(data1);i++)
                                                {
                                                if(data1->item!=data2->item)
                                                            flag=TRUE;
                                                data1=data1->right;
                                                data2=data2->right;
                                                }
                                    if(flag==FALSE)
                                                return (TRUE);
                                    else
                                                return (FALSE);
                                    }
                         }
            }
int length(node *data)
            {
            int k=0;
            while(data->right!=NULL)
                        {
                        k++;
                        data=data->right;
                        }
            return(k-1);
            }
void filestack(node *data, FILE *fp)
            {
            node *p;
            p=data;
            fclose(fp);
            fp=fopen("data.txt","w");
            int n,i,x;
            printf("\n Enter the number of elements to be entered in file:");
            scanf("%d",&n);
            for(i=1;i<=n;i++)
                        {
                        printf("\n Enter the %d# elemet:",i);
                        scanf("%d",&x);
                        fprintf(fp,"%d ",x);
      }
            fclose(fp);
            fp=fopen("data.txt","r");
            printf("\n Elements in the file:");
            for(i=1;i<=n;i++)
                        {
                        fscanf(fp,"%d",&x);
                        printf("%d ",x);
                        data=push1(data,x);
                        data->right=NULL;
                        }
            fclose(fp);
            printf("\n Elements from the file in reverse order:");
            top2bottomu(p);
            printf("\n");
            }
node *push1(node *data,int x)
            {
  //        node *p;
            while(data->right!=NULL)
                        data=data->right;
            data->item=x;
            data->right=(node *)malloc(sizeof(node));
            data=data->right;
            return(data);
            }
void duplicate(node *data)
            {
            node *p,*q;
            q=(node *)malloc(sizeof(node));
            p=data;
            int x;
            while(p->right->right!=NULL)
                        p=p->right;
            x=p->item;
            p->right->item=x;
            p->right->right=q;
            q->right=NULL;
   }
void top2bottom(node *data)
            {
                        while(!Isempty(data))
                                    {
                                    pop(data);
                                    }
            }
void pop(node *data)
            {
            node *p;
            if(Isempty(data))
                        {
                                    printf("\n Stack is empty, cannot be popped:\n");
                        }
            else
                        {
                                    if(data->right->right==NULL)
                                                {
                                                printf("\nElement popped out is %d \n",data->item);
                                                p=data;
                                                free(data);
                                                p->right=NULL;
                                                }
                                    else
                                                {
                                                while(data->right->right->right!=NULL)
                                                            data=data->right;
                                                            p=data->right;
                                                            printf("\nElement popped out is %d \n",p->item);
                                                            free(p);
                                                            data->right->right=NULL;

                                                }
                        }
            }
void top2bottomu(node *data)
            {
            node *q;
            q=data;
            int k=0,i,*a;
            while(data->right!=NULL)
                        {
                        k++;
                        data=data->right;
                        }
            //printf("%d.....\n",k);
            a=(int*)malloc(k*sizeof(int));
            for(i=0;i<k;i++)
                        {
                        *(a+i)=q->item;
                        q=q->right;
                        }
            for(i=k-1;i>=0;i--)
                        printf("%d ",*(a+i));

   free(a);
            printf("\n");
            }
int Isempty(node *data)
            {
                        if(data->right==NULL)
                                    return (TRUE);
                        else
                                    return (FALSE);
            }
node *push(node *data)
            {
  //        node *p;
            while(data->right!=NULL)
                        data=data->right;
            printf("\n Enter the element to be inserted:");
            scanf("%d",&data->item);
            data->right=(node *)malloc(sizeof(node));
            data=data->right;
            return(data);
            }
void display(node *data)
            {
                        if(Isempty(data))
                                    printf("\n Stack is empty, nothing to display:\n");
            else{    while(data->right!=NULL)
                                    {
                                    printf("%d ",data->item);
                                    data=data->right;
                                    }
                         }
            printf("\n");
            }
int pop1(node *data)
            {
            node *p;
            if(Isempty(data))
                        {
                                    printf("\n Stack is empty, cannot be popped:\n");
                        }
            else
                        {
                                    if(data->right->right==NULL)
                                                {
            p=data;
                                                p->right=NULL;
            return(data->item);
                                                }
                                    else
                                                {
                                                while(data->right->right->right!=NULL)
                                                            data=data->right;
                                                            p=data->right;
                                                            data->right->right=NULL;
                                                            return(p->item);
                                                            }
                        }
            }

Saturday, December 15, 2012

RSA Encryption : C Code

RSA is an algorithm for public-key cryptography that is based on the presumed difficulty of factoring large integers, the factoring problem. RSA stands for Ron Rivest, Adi Shamir and Leonard Adleman, who first publicly described it in 1977. Clifford Cocks, an English mathematician, had developed an equivalent system in 1973, but it was classified until 1997. A user of RSA creates and then publishes the product of two large prime numbers, along with an auxiliary value, as their public key. The prime factors must be kept secret. Anyone can use the public key to encrypt a message, but with currently published methods, if the public key is large enough, only someone with knowledge of the prime factors can feasibly decode the message.Whether breaking RSA encryption is as hard as factoring is an open question known as the RSA problem. Below is the C program to demonstrate the algorthm (using small prime numbers, not suitable for real life application)

#include<iostream.h>
#include<math.h>
#include<string.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void keygen(void);
void ran(void);
void man(void);
void encrypt(void);
void decrypt(void);
int sqrmul(int,int,int);
int invr1(int,int);
int invr(int,int);
int prime(int);
int gcd(int,int);
void main()
{
      int chk;
      do
      {
      printf("\n-------------------------------------------\n");
      printf("\n*MAIN MENU*");
      printf("\n");
      printf("\t \t \t \n *PROGRAM TO DEMONSTRATE RSA CIPHER         
      SYSTEM*");
      printf("\n--------------------------------------------\n");
      printf("\n Press 1 to ENCRYPT:");
      printf("\n Press 2 to DECRYPT:");
      printf("\n Press 3 to GENERATE KEY:");
      printf("\n Press 4 to EXIT:");
      printf("\n Please enter your choice......");
      scanf("%d",&chk);
      printf("\n");
      switch(chk)
            {
            case 1:
            encrypt();
            break;
            case 2:
            decrypt();
            break;
            case 3:
            keygen();
            break;
            case 4:
            break;
            default:
            printf("\n You have enter a wrong choice!!!!!!!!\n");
            break;
            }
      }
      while(chk!=4);
}

void encrypt()
{
      char pln[100];
      int len,e,n,tm;
      printf("\n Enter the plain text:");
      cin.getline(pln,'\100');
      printf("\n Enter the public key (e,n):");
      scanf("%d %d",&e,&n);
      len=strlen(pln);
      printf("\n The cipher text is:");
      for(int i=0;i<len;i++)
            {
             tm=pln[i];
             printf("%d ",sqrmul(tm,e,n));
             }
      printf("\n Length of cipher text is: %d",len);
      getche();
}
void decrypt()
{
int cip[50],e,n1,l;
printf("\n Enter the lenth:");
scanf("%d",&l);
printf("\n Enter d and n:");
scanf("%d %d",&e,&n1);
printf("\n Enter the cipher text:");
for(int i=0;i<l;i++)
scanf("%d",&cip[i]);
printf("\n The decoded message is: ");
for(int j=0;j<l;j++)
      {
      printf("%c",sqrmul(cip[j],e,n1));
      }
getche();
}
void keygen()
{
int ch;
do
      {
      printf("\n\n");
      printf("\nKEY GENERATOR FOR RSA");
      printf("\nPress 1 for random selection");
      printf("\nPress 2 for manual selection");
      printf("\nPress 3 to return to main menu\n\t\t");
      scanf("%d",&ch);
            switch(ch)
            {
            case 1:
            ran();
            break;
            case 2:
            man();
            break;
            case 3:
            break;
            default:
            printf("\n Wrong choice:");
            }
      }while(ch!=3);
}
void man(void)
{
int u,i,ar[1000],k=0,p,q,ph,n,e;
printf("\n Enter the upper limit:");
scanf("%d",&u);
printf("\n You can choose any two primes from the following prime(s):\n");
for(i=2;i<=u;i++)
      {
      if(prime(i)==1)
            {
            ar[k]=i;
            printf("\t%d(%d)",ar[k],k);
            k++;
            }
      }
printf("\n");
printf("\n Enter  two numbers between 0 and %d :",k-1);
scanf("%d %d",&p,&q);
printf("\n");
printf("\n Your choice of p and q is:%d %d",ar[p],ar[q]);
ph=(ar[p]-1)*(ar[q]-1);
n=(ar[p])*(ar[q]);
printf("\n");
printf("\n phi(%d) is: %d",ar[p]*ar[q],ph);
int z=0;
printf("\n");
printf("\n You can choose any one number for 'e':\n");
for(i=2;i<ph;i++)
      {
      if(gcd(i,ph)==1)
            {
            ar[z]=i;
            printf("\t%d(%d)",ar[z],z);
            z++;
            }
      }
printf("\n");
printf("\n Enter  one number between 0 and %d :",z-1);
scanf("%d",&e);
printf("\n");
printf("\n Your choice of e is:%d",ar[e]);
printf("\n");
printf("\n You have chosen the public key:(%d,%d)",ar[e],n);
printf("\n");
printf("\n Private key is: (%d,%d)",invr(ar[e],ph),n);
printf("\n Press any key............");
getche();
}
void ran()
{
int u,i,ar[400],k=0,p,q,ph,n,e;
printf("\n Enter the upper limit:");
scanf("%d",&u);
printf("\n p and q will be seleted automatically from the list below:\n");
printf("\n");
for(i=2;i<=u;i++)
      {
      if(prime(i)==1)
            {
            ar[k]=i;
            printf("\t%d",ar[k]);
            k++;
            }
      }
printf("\n");
p=rand()%k;
do
      {
      q=rand()%k;
      }while(q==p);
printf("\n");
printf("\n Selected value p and q is:%d %d",ar[p],ar[q]);
printf("\n");
ph=((ar[p]-1)*(ar[q]-1));
n=(ar[p])*(ar[q]);
printf("\n");
printf("\n phi(%d) is: %d",n,ph);
printf("\n");
int z=0;
printf("\n");
printf("\n e will be seleted automatically from the list below :\n");
printf("\n");
for(i=2;i<ph;i++)
      {
      if(gcd(i,ph)==1)
            {
            ar[z]=i;
            printf("\t%d",ar[z]);
            z++;
            }
      }
printf("\n");
e=rand()%z;
printf("\n");
printf("\n Selected e is:%d",ar[e]);
printf("\n");
printf("\n The public key:(%d,%d)",ar[e],n);
printf("\n");
printf("\n Private key is: (%d,%d)",invr(ar[e],ph),n);
printf("\n Press any key.........");
getche();
}
int prime(int n)
{
int i,rem,k=0;
for(i=2;i<=sqrt(n);i++)
      {
      rem=n%i;
      if(rem==0)
            {
            k++;
            }
      }
if(k>0)
return 0;
else
return 1;
}
int gcd(int a,int b)
{
int x,y,rem;
x=abs(a);
y=abs(b);
if(x<y)
{
int tmp=y;
y=x;
x=tmp;
}
do
{
rem=x%y;
x=y;
y=rem;
} while(rem!=0);
if(x==1)
return 1;
else
return 0;
}
int sqrmul(int p,int e,int n)
{
      unsigned long int rem,q,a[20],i=19,mod;
while(q!=1)
      {
      rem=e%2;
      q=(e-rem)/2;
      e=q;
      a[i]=rem;
      i--;
      }
      a[i]=1;
      long int k=0,tmp;
for(int j=i;j<20;j++)
{
      if(k==0)
      {
      mod=p%n;
      tmp=mod;
      }
      else
      {
            if(a[j]==1)
            {
            mod=(tmp*tmp*p)%n;
            tmp=mod;
            }
            else
            {
            mod=(tmp*tmp)%n;
            tmp=mod;
            }
      }

      k++;

}
return(tmp);

}
int invr(int a,int b)
{
long int temp=0,inv;
do
      {
      inv=a*temp-1;
      ++temp;
      }
       while(inv%b!=0);
       return (temp-1);
}
int invr1(int e,int n)
{
long int temp=0,inv;
do
      {
      inv=e*temp-1;
      ++temp;
      }
       while(inv%n!=0);
       return(temp-1);
}