9, నవంబర్ 2012, శుక్రవారం

INFORMATION,NETWORKSECURITY PROGRAMS

6. TAKE A 64-BIT PLAIN TEXT AND ENCRYPT THE SAME USING DES ALGORITHM.


Aim:-To encrypt a 64-bit plain text using DES algorithm.


Program:-

int s00[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}},s0,s1;
int s11[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
int f1[4],f2[4],k1[8]={1,0,1,0,0,1,0,0},k2[8]={0,1,0,0,0,0,1,1};
int ip[8],ipi[8]={1,5,2,0,3,7,4,6},cp[8],epi[8]={3,0,1,2,1,2,3,0},ep[8];
int p[4],r1,c1,r2,c2,p4[4],p44[4]={1,3,2,0},i,j,k[10],r[4],c=0,rs[8];
int p[8]={1,0,0,0,1,1,0,1},ipii[8]={3,0,2,4,6,1,7,5};
main()
{
 clrscr();
 printf(“\n Enter the 8 bits of plain text:”);
 for(i=0;i<8;i++)
        scanf(“%d”,&p1[i]);
printf(“\n enter the 10-bit key:”);
        scanf(“%d”, &k[i]);
 for(i=0;i<8;i++)
      ip[i]=p1[ipi[i]];
fn();
c++;
fn();
for(i=0;i<4;i++)
      ip[i]=r[i];
for(i=0,j=4;i<4;i++,j++)
      ip[j]=f2[i];
for(i=0;i<8;i++)
      cp[i]=ip[ipii[i]];
printf(“\n The cipher text is \n”);
for(i=0;i<8;i++)
         printf(“%d,\t”,cp[i]);
getch();
}

int fn()
{
  for(i=0,j=0;i<4;i++,j++)
          f1[j]=ip[i];
  for(i=4,j=0;i<8;i++,j++)
          f2[j]=ip[i];
 for(i=0;i<8;i++)
ep[i]=f2[epi[i]];
if(c==0)
{
       for(i=0;i<8;i++)
        {
               if(k1[i]==ep[i])
                          rs[i]=0;
               else
                         rs[i]=1;
        }
 }
 else
  {
    for(i=0;i<8;i++)
        {
                  if(k2[i]==ep[i])
                    rs[i]=0;
                 else
                    rs[i]=1;
         }
  }
      r1=rs[0]*2+rs[3]*1;
      c1=rs[1]*2+rs[2]*1;
      r2=rs[4]*2+rs[7]*1;
      c2=rs[5]*2+rs[6]*1;
      s0=s00[r1][c1];
      s1=s11[r2][c2];
      i=0;
      p[i]=s0/2;i++;
      p[i]=s0%2;i++;
      p[i]=s1/2;i++;
      p[i]=s1%2;

    for(i=0;i<4;i++)
     p4[i]=p[p44[i]];
    for(i=0;i<4;i++)
     {
         if(p4[i]==f1[i])
           r[i]=0;
        else
           r[i]=1;
    }
      for(i=0;i<4;i++)
          ip[i]=f2[i];
      for(i=0,j=4;i<4;i++,j++)
          ip[j]=r[i];
}


























7.TAKE A 64-BIT PLAIN TEXT AND ENCRYPT THE SAME USING DES ALGORITHM.

Aim:- To encrypt a 64-bit plain text using DES algorithm.


Program:-

int s00[4][4]={{1,0,3,2},{3,2,1,0},{0,2,1,3},{3,1,3,2}},s0,s1;
int s11[4][4]={{0,1,2,3},{2,0,1,3},{3,0,1,0},{2,1,0,3}};
int f1[4],f2[4],k1[8]={1,0,1,0,0,1,0,0},k2[8]={0,1,0,0,0,0,1,1};
int ip[8],ipi[8]={1,5,2,0,3,7,4,6},cp[8],epi[8]={3,0,1,2,1,2,3,0},ep[8];
int p[4],r1,c1,r2,c2,p4[4],p44[4]={1,3,2,0},i,j,k[10],r[4],c=0,rs[8];
int p[8]={1,0,0,0,1,1,0,1},ipii[8]={3,0,2,4,6,1,7,5};
main()
{
 clrscr();
 printf(“\n Enter the 8 bits of plain text:”);
 for(i=0;i<8;i++)
        scanf(“%d”,&p1[i]);
printf(“\n enter the 10-bit key:”);
        scanf(“%d”, &k[i]);
 for(i=0;i<8;i++)
      ip[i]=p1[ipi[i]];
fn();
c++;
fn();
for(i=0;i<4;i++)
      ip[i]=r[i];
for(i=0,j=4;i<4;i++,j++)
      ip[j]=f2[i];
for(i=0;i<8;i++)
      cp[i]=ip[ipii[i]];
printf(“\n The cipher text is \n”);
for(i=0;i<8;i++)
         printf(“%d,\t”,cp[i]);
getch();
}


int fn()
{
  for(i=0,j=0;i<4;i++,j++)
          f1[j]=ip[i];
  for(i=4,j=0;i<8;i++,j++)
          f2[j]=ip[i];
 for(i=0;i<8;i++)
ep[i]=f2[epi[i]];
if(c==0)
{
       for(i=0;i<8;i++)
        {
               if(k1[i]==ep[i])
                          rs[i]=0;
               else
                         rs[i]=1;
        }
 }
 else
  {
    for(i=0;i<8;i++)
        {
                  if(k2[i]==ep[i])
                    rs[i]=0;
                 else
                    rs[i]=1;
         }
  }
      r1=rs[0]*2+rs[3]*1;
      c1=rs[1]*2+rs[2]*1;
      r2=rs[4]*2+rs[7]*1;
      c2=rs[5]*2+rs[6]*1;
      s0=s00[r1][c1];
      s1=s11[r2][c2];
      i=0;
      p[i]=s0/2;i++;
      p[i]=s0%2;i++;
      p[i]=s1/2;i++;
      p[i]=s1%2;

     for(i=0;i<4;i++)
     p4[i]=p[p44[i]];
    for(i=0;i<4;i++)
     {
         if(p4[i]==f1[i])
           r[i]=0;
        else
           r[i]=1;
    }
      for(i=0;i<4;i++)
          ip[i]=f2[i];
      for(i=0,j=4;i<4;i++,j++)
          ip[j]=r[i];
}


























8. USING RSA ALGORITHM ENCRYPT A TEXT DATA AND DECRYPT THE SAME.

Aim:- To encrypt and decrypt a text RSA algorithm.


Program:-

#include
#include
char *r,en[10],de[10],b[10],g;
char a[26]={‘a’,’b’,’c’,’d’,’e’,’f’,’g’,’h’,’I’,’j’,’k’,’l’,’m’,’n’,’o’,’p’,’q’,’r’,’s’,’t’,’u’,’v’,’w’,’x’,’y’,’z’};
main()
{
    int q,l=0,j,cop[50],flag=0,f;
    int temp[10],p,e,d,I,n,z,m,g;
    unsigned long c;
    clrscr();
     printf(“\n Enter the plaintext at the end type”);
             while((b[i]=getchar())!=’@’)
        i++;
        r=b;
     printf(“\n Enter two prime numbers”);
              scanf(“%d%d”,&p&q);
     n=p*q;
     z=(p-1)*(q-1);
     i=0;j=0;
     for(f=3;f
     {
           if(f%2==0)
               flag=0;
           else
           {
               cop[i]=f;
                i++;j++;
            }
      }
      printf(“\n The coprimes for %d are”,z);
      for(i=0;i
      printf(“%d\t”,cop[i]);
      printf(“\n enter one coprime”);
       scanf(“%d”,&e);
      j=0;
      while(*r!=’@’)
      {
        for(i=0;i!=25&&flag==0;i++)
       {
           if(*r==a[i])
           {
             c=pow(I,e)
             g=c%n;
             temp[j]=g;
             flag=1;
             en[j]=toupper(a[g]);
              j++;
         }    }
        r++;
        flag=0; }
printf(“\n The cipher text is:”);
for(i=0;i
printf(“%c”,en[i]);
for(d=1;;d++)
{
   if((e*d)%z==1)
   break;
}
printf(“\n The decryption key is %d”,d);
printf(“\n Enter decryption key”);
scanf(“%d”,&d);
printf(“\n the decrypted text is:”);
for(i=0;i
{
   c=pow(temp[i],d);
         m=c%n;
printf(“%c”,a[m]);
}
getch();
}

కామెంట్‌లు లేవు:

కామెంట్‌ను పోస్ట్ చేయండి