Code for DES :
Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: Code for DES :

  1. #1
    Junior Member
    Join Date
    Jul 2003
    Posts
    9

    Arrow Code for DES :

    Hello,
    I am a new member.I am pasting a C code for the DES encryption algorithm. Although I do not understand much of the mathematical discussions about cryptography and encryption, I go hope to study this field. This code accepts 64 bit binary data and a 56 bit binary key,since 8 bits out of the 64 bits are used for error checking.
    I only want the members to verify the code,because I once tried to verify it from an example from a book.The encrypted data my code gave was notconsistent with the book's answer.But the book did have some printing mistakes,so I hope its answer was incorrect and mine right.
    Thank you.
    #include<stdio.h>
    #include<conio.h>
    int n,r=1,s1[4][16],s2[4][16],s3[4][16],s4[4][16],s5[4][16],s6[4][16],s7[4][16],
    s8[4][16],k[56],k1[64],bl[32],br[32],b1[64],b2[64],s[48],b3[48],
    t[48],i,count=0,br1[32],k2[56],k3[48],br2[32];
    void main()
    {
    clrscr();
    int sbox(int,int,int,int),snum[8],btemp[32];
    int sr1,sr2,sr3,sr4,sr5,sr6,sr7,sr8,sc1,sc2,sc3,sc4,sc5,sc6,sc7,sc8;
    void sl();
    void rh();
    int row(int,int);
    void bindec(int);
    void p1();

    s1[0][0]=14;s1[0][1]=4;s1[0][2]=13;s1[0][3]=1;s1[0][4]=2;s1[0][5]=15;s1[0][6]=11;
    s1[0][7]=8;s1[0][8]=3;s1[0][9]=10;s1[0][10]=6;s1[0][11]=12;s1[0][12]=5;
    s1[0][13]=9;s1[0][14]=0;s1[0][15]=7;
    s1[1][0]=0;s1[1][1]=15;s1[1][2]=7;s1[1][3]=4;
    s1[1][4]=14;s1[1][5]=2;s1[1][6]=13;s1[1][7]=1;s1[1][8]=10;s1[1][9]=6;s1[1][10]=12;
    s1[1][11]=11;s1[1][12]=9;s1[1][13]=5;s1[1][14]=3;s1[1][15]=8;
    s1[2][0]=4;s1[2][1]=1;s1[2][2]=14;s1[2][3]=8;s1[2][4]=13;s1[2][5]=6;s1[2][6]=2;
    s1[2][7]=11;s1[2][8]=15;s1[2][9]=12;s1[2][10]=9;s1[2][11]=7;s1[2][12]=3;
    s1[2][13]=10;s1[2][14]=5;s1[2][15]=0;s1[3][0]=15;s1[3][1]=12;s1[3][2]=8;
    s1[3][3]=2;s1[3][4]=4;s1[3][5]=9;s1[3][6]=1;s1[3][7]=7;s1[3][8]=5;s1[3][9]=11;
    s1[3][10]=3;s1[3][11]=14;s1[3][12]=10;s1[3][13]=0;s1[3][14]=6;
    s1[3][15]=13;

    s2[0][0]=5;s2[0][1]=1;s2[0][2]=8;s2[0][3]=14;s2[0][4]=6;s1[0][5]=11;s2[0][6]=3;
    s2[0][7]=4;s2[0][8]=9;s2[0][9]=7;s2[0][10]=2;s2[0][11]=13;s2[0][12]=12;s2[0][13]=0;
    s2[0][14]=5;s2[0][15]=10;s2[1][0]=3;s2[1][1]=13;s2[1][2]=4;s2[1][3]=7;s2[1][4]=15;
    s2[1][5]=2;s2[1][6]=8;s2[1][7]=14;s2[1][8]=12;s2[1][9]=0;s2[1][10]=1;s2[1][11]=10;
    s2[1][12]=6;s2[1][13]=9;s2[1][14]=11;s2[1][15]=5;s2[2][0]=0;s2[2][1]=14;
    s2[2][2]=7;s2[2][3]=11;s2[2][4]=10;s2[2][5]=4;s2[2][6]=13;s2[2][7]=1;s2[2][8]=5;
    s2[2][9]=8;s2[2][10]=12;s2[2][11]=6;s2[2][12]=9;s2[2][13]=3;s2[2][14]=2;s2[2][15]=15;
    s2[3][0]=13;s2[3][1]=8;s2[3][2]=10;s2[3][3]=1;s2[3][4]=3;s2[3][5]=15;s2[3][6]=4;
    s2[3][7]=2;s2[3][8]=11;s2[3][9]=6;s2[3][10]=7;s2[3][11]=12;s2[3][12]=0;s2[3][13]=5;
    s2[3][14]=14;s2[3][15]=9;

    s3[0][0]=10;s3[0][1]=0;s3[0][2]=9;s3[0][3]=14;s3[0][4]=6;s3[0][5]=3;s3[0][6]=15;
    s3[0][7]=5;s3[0][8]=1;s3[0][9]=13;s3[0][10]=12;s3[0][11]=7;s3[0][12]=11;s3[0][13]=4;
    s3[0][14]=2;s3[0][15]=8;s3[1][0]=13;s3[1][1]=7;s3[1][2]=0;s3[1][3]=9;s3[1][4]=3;
    s3[1][5]=4;s3[1][6]=6;s3[1][7]=10;s3[1][8]=2;s3[1][9]=8;s3[1][10]=5;s3[1][11]=14;
    s3[1][12]=12;s3[1][13]=11;s3[1][14]=15;
    s3[1][15]=1;s3[2][0]=13;s3[2][1]=6;s3[2][2]=4;s3[2][3]=9;s3[2][4]=8;
    s3[2][5]=15;s3[2][6]=3;s3[2][7]=0;
    s3[2][8]=11;s3[2][9]=1;s3[2][10]=2;s3[2][11]=12;s3[2][12]=5;
    s3[2][13]=10;s3[2][14]=14;s3[2][15]=7;s3[3][0]=1;s3[3][1]=10;s3[3][2]=13;
    s3[3][3]=0;s3[3][4]=6;s3[3][5]=9;s3[3][6]=8;s3[3][7]=7;s3[3][8]=4;s3[3][9]=15;
    s3[3][10]=14;s3[3][11]=3;
    s3[3][12]=11;s3[3][13]=5;s3[3][14]=2;s3[3][15]=12;

    s4[0][0]=7;s4[0][1]=13;s4[0][2]=14;s4[0][3]=3;s4[0][4]=0;s4[0][5]=6;
    s4[0][6]=9;s4[0][7]=10;
    s4[0][8]=1;s4[0][9]=2;s4[0][10]=8;s4[0][11]=5;s4[0][12]=11;s4[0][13]=12;
    s4[0][14]=4;s4[0][15]=15;
    s4[1][0]=13;s4[1][1]=8;s4[1][2]=11;s4[1][3]=5;s4[1][4]=6;s4[1][5]=15;
    s4[1][6]=0;
    s4[1][7]=3;s4[1][8]=4;s4[1][9]=7;s4[1][10]=2;
    s4[1][11]=12;s4[1][12]=1;s4[1][13]=10;s4[1][14]=14;
    s4[1][15]=9;s4[2][0]=10;s4[2][1]=6;s4[2][2]=9;s4[2][3]=0;
    s4[2][4]=12;s4[2][5]=11;
    s4[2][6]=7;s4[2][7]=13;s4[2][8]=15;s4[2][9]=1;s4[2][10]=3;s4[2][11]=14;
    s4[2][12]=5;
    s4[2][13]=2;s4[2][14]=8;s4[2][15]=4;
    s4[3][0]=3;s4[3][1]=15;s4[3][2]=0;s4[3][3]=6;s4[3][4]=10;
    s4[3][5]=1;s4[3][6]=13;s4[3][7]=8;s4[3][8]=9;s4[3][9]=4;
    s4[3][10]=5;s4[3][11]=11;
    s4[3][12]=12;s4[3][13]=7;s4[3][14]=2;s4[3][15]=14;

    s5[0][0]=2;s5[0][1]=12;s5[0][2]=4;
    s5[0][3]=1;s5[0][4]=7;s5[0][5]=10;s5[0][6]=11;s5[0][7]=6;s5[0][8]=8;s5[0][9]=5;
    s5[0][10]=3;s5[0][11]=15;s5[0][12]=13;s5[0][13]=0;s5[0][14]=14;s5[0][15]=9;
    s5[1][0]=14;s5[1][1]=11;s5[1][2]=2;
    s5[1][3]=12;s5[1][4]=4;s5[1][5]=7;s5[1][6]=13;s5[1][7]=1;
    s5[1][8]=5;s5[1][9]=0;s5[1][10]=15;s5[1][11]=10;s5[1][12]=3;s5[1][13]=9;
    s5[1][14]=8;s5[1][15]=6;s5[2][0]=4;s5[2][1]=2;s5[2][2]=1;s5[2][3]=11;
    s5[2][4]=10;s5[2][5]=13;s5[2][6]=7;s5[2][7]=8;
    s5[2][8]=15;s5[2][9]=9;s5[2][10]=12;s5[2][11]=5;s5[2][12]=6;
    s5[2][13]=3;s5[2][14]=0;s5[2][15]=14;s5[3][0]=11;s5[3][1]=8;
    s5[3][2]=12;s5[3][3]=7;s5[3][4]=1;s5[3][5]=14;s5[3][6]=2;s5[3][7]=13;
    s5[3][8]=6;s5[3][9]=15;s5[3][10]=0;s5[3][11]=9;
    s5[3][12]=10;s5[3][13]=4;s5[3][14]=5;s5[3][15]=3;

    s6[0][0]=12;s6[0][1]=1;s6[0][2]=10;s6[0][3]=15;s6[0][4]=9;s6[0][5]=2;s6[0][6]=6;s6[0][7]=8;
    s6[0][8]=0;s6[0][9]=13;s6[0][10]=3;s6[0][11]=4;s6[0][12]=14;s6[0][13]=7;s6[0][14]=5;s6[0][15]=11;
    s6[1][0]=10;s6[1][1]=15;s6[1][2]=4;s6[1][3]=2;s6[1][4]=7;s6[1][5]=12;s6[1][6]=9;s6[1][7]=5;
    s6[1][8]=6;s6[1][9]=1;s6[1][10]=13;s6[1][11]=14;s6[1][12]=0;s6[1][13]=11;s6[1][14]=3;s6[1][15]=8;
    s6[2][0]=9;s6[2][1]=14;s6[2][2]=15;s6[2][3]=5;s6[2][4]=2;s6[2][5]=8;s6[2][6]=12;s6[2][7]=3;
    s6[2][8]=7;s6[2][9]=0;s6[2][10]=4;s6[2][11]=10;s6[2][12]=1;s6[2][13]=13;s6[2][14]=11;s6[2][15]=6;
    s6[3][0]=4;s6[3][1]=3;s6[3][2]=2;s6[3][3]=12;s6[3][4]=9;s6[3][5]=5;s6[3][6]=15;s6[3][7]=10;
    s6[3][8]=11;s6[3][9]=14;s6[3][10]=1;s6[3][11]=7;s6[3][12]=6;s6[3][13]=0;s6[3][14]=8;s6[3][15]=13;

    s7[0][0]=4;s7[0][1]=11;s7[0][2]=2;s7[0][3]=14;s7[0][4]=15;s7[0][5]=0;s7[0][6]=8;s7[0][7]=13;
    s7[0][8]=3;s7[0][9]=12;s7[0][10]=9;s7[0][11]=7;s7[0][12]=5;s7[0][13]=10;s7[0][14]=6;s7[0][15]=1;
    s7[1][0]=13;s7[1][1]=0;s7[1][2]=11;s7[1][3]=7;s7[1][4]=4;s7[1][5]=9;s7[1][6]=1;s7[1][7]=10;
    s7[1][8]=14;s7[1][9]=3;s7[1][10]=5;s7[1][11]=12;s7[1][12]=2;s7[1][13]=15;s7[1][14]=8;s7[1][15]=6;
    s7[2][0]=1;s7[2][1]=4;s7[2][2]=11;s7[2][3]=13;s7[2][4]=12;s7[2][5]=3;s7[2][6]=7;s7[2][7]=14;
    s7[2][8]=10;s7[2][9]=15;s7[2][10]=6;s7[2][11]=8;s7[2][12]=0;s7[2][13]=5;s7[2][14]=9;s7[2][15]=12;
    s7[3][0]=6;s7[3][1]=11;s7[3][2]=13;s7[3][3]=8;s7[3][4]=1;s7[3][5]=4;s7[3][6]=10;s7[3][7]=7;
    s7[3][8]=9;s7[3][9]=5;s7[3][10]=0;s7[3][11]=15;s7[3][12]=14;s7[3][13]=2;s7[3][14]=3;s7[3][15]=12;

    s8[0][0]=13;s8[0][1]=2;s8[0][2]=8;s8[0][3]=4;s8[0][4]=6;s8[0][5]=15;s8[0][6]=11;s8[0][7]=1;
    s8[0][8]=10;s8[0][9]=9;s8[0][10]=3;s8[0][11]=14;s8[0][12]=5;s8[0][13]=0;s8[0][14]=12;s8[0][15]=7;
    s8[1][0]=1;s8[1][1]=15;s8[1][2]=13;s8[1][3]=8;s8[1][4]=10;s8[1][5]=3;s8[1][6]=7;s8[1][7]=4;
    s8[1][8]=12;s8[1][9]=5;s8[1][10]=6;s8[1][11]=11;s8[1][12]=0;s8[1][13]=14;s8[1][14]=9;s8[1][15]=2;
    s8[2][0]=7;s8[2][1]=11;s8[2][2]=4;s8[2][3]=1;s8[2][4]=9;s8[2][5]=12;s8[2][6]=14;s8[2][7]=2;
    s8[2][8]=0;s8[2][9]=6;s8[2][10]=10;s8[2][11]=13;s8[2][12]=15;s8[2][13]=3;s8[2][14]=5;s8[2][15]=8;
    s8[3][0]=2;s8[3][1]=1;s8[3][2]=14;s8[3][3]=7;s8[3][4]=4;s8[3][5]=10;s8[3][6]=8;s8[3][7]=13;
    s8[3][8]=15;s8[3][9]=12;s8[3][10]=9;s8[3][11]=0;s8[3][12]=3;s8[3][13]=5;s8[3][14]=6;s8[3][15]=11;
    printf("Enter 64-bit binary data ");
    for(int i=0;i<64;i++)
    {
    g:
    {
    scanf("%d",&b1[i]);
    if(b1[i]>1||b1[i]<0)
    {
    printf("Enter binary data only : ");
    goto g;
    }}}
    printf("Enter the 56-bit key\n[Key length is 64 bits,but 8 are\nreserved for parity checking ");
    for(i=0;i<56;i++)
    {
    g1:
    {
    scanf("%d",&k[i]);
    if(k[i]>1||k[i]<0)
    {
    printf("Enter binary data only : ");
    goto g1;
    }}}
    for(i=0;i<64;i++)
    b2[i]=b1[i]; //Initial permutation of data.
    b1[0]=b2[57];b1[1]=b2[49];b1[2]=b2[41];b1[3]=b2[33];b1[4]=b2[25];b1[5]=b2[17];
    b1[6]=b2[9];b1[7]=b2[1];b1[8]=b2[59];b1[9]=b2[51];b1[10]=b2[43];b1[11]=b2[35];
    b1[12]=b2[27];b1[13]=b2[19];b1[14]=b2[11];b1[15]=b2[3];b1[16]=b2[61];
    b1[17]=b2[53];b1[18]=b2[45];b1[19]=b2[37];b1[20]=b2[29];b1[21]=b2[21];
    b1[22]=b2[13];b1[23]=b2[5];b1[24]=b2[63];b1[25]=b2[55];b1[26]=b2[47];
    b1[27]=b2[39];b1[28]=b2[31];b1[29]=b2[23];b1[30]=b2[15];b1[31]=b2[7];
    b1[32]=b2[56];b1[33]=b2[48];b1[34]=b2[40];b1[35]=b2[32];b1[36]=b2[24];
    b1[37]=b2[16];b1[38]=b2[8];b1[39]=b2[0];b1[40]=b2[58];b1[41]=b2[50];
    b1[42]=b2[42];b1[43]=b2[34];b1[44]=b2[26];b1[45]=b2[18];b1[46]=b2[10];
    b1[47]=b2[2];b1[48]=b2[60];b1[49]=b2[52];b1[50]=b2[44];b1[51]=b2[36];
    b1[52]=b2[28];b1[53]=b2[20];b1[54]=b2[12];b1[55]=b2[4];b1[56]=b2[62];
    b1[57]=b2[54];b1[58]=b2[46];b1[59]=b2[38];b1[60]=b2[30];b1[61]=b2[22];
    b1[62]=b2[14];b1[63]=b2[6];
    for(i=0;i<56;i++)
    k1[i]=k[i]; //Converting 64-bit key to 56-bit key.
    k2[0]=k1[57];k2[1]=k1[49];k2[2]=k1[41];k2[3]=k1[33];k2[4]=k1[25];k2[5]=k1[17];
    k2[6]=k1[9];k2[7]=k1[1];k2[8]=k1[58];k2[9]=k1[50];k2[10]=k1[42];k2[11]=k1[34];
    k2[12]=k1[26];k2[13]=k1[18];k2[14]=k1[10];k2[15]=k1[2];k2[16]=k1[59];k2[17]=k1[51];
    k2[18]=k1[43];k2[19]=k1[35];k2[20]=k1[27];k2[21]=k1[19];k2[22]=k1[11];k2[23]=k1[3];
    k2[24]=k1[60];k2[25]=k1[52];k2[26]=k1[44];k2[27]=k1[36];k2[28]=k1[63];k2[29]=k1[55];
    k2[30]=k1[47];k2[31]=k1[39];k2[32]=k1[31];k2[33]=k1[23];k2[34]=k1[15];k2[35]=k1[7];
    k2[36]=k1[62];k2[37]=k1[54];k2[38]=k1[46];k2[39]=k1[38];k2[40]=k1[30];k2[41]=k1[22];
    k2[42]=k1[14];k2[43]=k1[6];k2[44]=k1[61];k2[45]=k1[53];k2[46]=k1[45];k2[47]=k1[37];
    k2[48]=k1[29];k2[49]=k1[21];k2[50]=k1[13];k2[51]=k1[5];k2[52]=k1[28];k2[53]=k1[20];
    k2[54]=k1[12];k2[55]=k1[4];

    for(i=0;i<32;i++) //Breaking the initially permuted
    bl[i]=b1[i]; //data bits in right-half
    for(i=32;i<64;i++) //and left-half respectively
    br[i-32]=b1[i];

    for(r=1;r<=16;r++)
    {
    rh();
    sl();
    printf("Round %d :\n",r);
    for(i=0;i<56;i++)
    printf("%d ",k3[i]);
    for(i=0;i<48;i++)
    {
    s[i]=b3[i]+k3[i]; //X-OR of Expanded Right-side of data and compressed
    if(s[i]==2) //key.
    s[i]=0;
    }
    for(i=0;i<48;i++)
    t[i]=s[i];
    printf("\n\n");
    getch();
    sr1=row(t[0],t[5]);
    sr2=row(t[6],t[11]);
    sr3=row(t[12],t[17]);
    sr4=row(t[18],t[23]);
    sr5=row(t[24],t[29]);
    sr6=row(t[30],t[35]);
    sr7=row(t[36],t[41]);
    sr8=row(t[42],t[47]);
    sc1=sbox(1,2,3,4);
    sc2=sbox(7,8,9,10);
    sc3=sbox(13,14,15,16);
    sc4=sbox(19,20,21,22);
    sc5=sbox(25,26,27,28);
    sc6=sbox(31,32,33,34);
    sc7=sbox(37,38,39,40);
    sc8=sbox(43,44,45,46);
    snum[0]=s1[sr1][sc1];
    snum[1]=s2[sr2][sc2];
    snum[2]=s3[sr3][sc3];
    snum[3]=s4[sr4][sc4];
    snum[4]=s5[sr5][sc5];
    snum[5]=s6[sr6][sc6];
    snum[6]=s7[sr7][sc7];
    snum[7]=s8[sr8][sc8];
    bindec(snum[0]);
    bindec(snum[1]);
    bindec(snum[2]);
    bindec(snum[3]);
    bindec(snum[4]);
    bindec(snum[5]);
    bindec(snum[6]);
    bindec(snum[7]);
    p1();
    for(i=0;i<32;i++)
    {
    btemp[i]=bl[i]+br1[i];
    if(btemp[i]==2)
    btemp[i]=0;
    }
    for(i=0;i<32;i++)
    bl[i]=br[i];
    for(i=0;i<32;i++)
    br[i]=btemp[i];
    count=0;
    }
    for(i=0;i<32;i++)
    b1[i]=bl[i];
    for(i=0;i<32;i++)
    b1[i+32]=br[i];
    for(i=0;i<64;i++)
    b2[i]=b1[i];
    //Final permutation to obtain ciphertext
    b1[0]=b2[40];b1[1]=b2[8];b1[2]=b2[48];b1[3]=b2[16];b1[4]=b2[56];
    b1[5]=b2[24];b1[6]=b2[64];b1[7]=b2[32];b1[8]=b2[39];b1[9]=b2[7];
    b1[10]=b2[47];b1[11]=b2[15];b1[12]=b2[55];b1[13]=b2[23];b1[14]=b2[63];
    b1[15]=b2[31];b1[16]=b2[38];b1[17]=b2[6];b1[18]=b2[46];b1[19]=b2[14];
    b1[20]=b2[54];b1[21]=b2[22];b1[22]=b2[62];b1[23]=b2[30];b1[24]=b2[37];
    b1[25]=b2[5];b1[26]=b2[45];b1[27]=b2[13];b1[28]=b2[53];b1[29]=b2[21];
    b1[30]=b2[61];b1[31]=b2[29];b1[32]=b2[36];b1[33]=b2[4];b1[34]=b2[44];
    b1[35]=b2[12];b1[36]=b2[52];b1[37]=b2[20];b1[38]=b2[60];b1[39]=b2[28];
    b1[40]=b2[35];b1[41]=b2[3];b1[42]=b2[43];b1[43]=b2[11];b1[44]=b2[51];
    b1[45]=b2[19];b1[46]=b2[59];b1[47]=b2[27];b1[48]=b2[34];b1[49]=b2[2];
    b1[50]=b2[42];b1[51]=b2[10];b1[52]=b2[50];b1[53]=b2[18];b1[54]=b2[58];
    b1[55]=b2[26];b1[56]=b2[33];b1[57]=b2[1];b1[58]=b2[41];b1[59]=b2[9];
    b1[60]=b2[49];b1[61]=b2[17];b1[62]=b2[57];b1[63]=b2[25];
    printf("\n\n");
    for(i=0;i<32;i++)
    printf("%d ",bl[i]);
    printf("\n");
    for(i=0;i<32;i++)
    printf("%d ",br[i]);
    printf("\n\n***********\n");
    printf("The encrypted 64-bit data is as follows :\n");
    for(i=0;i<64;i++)
    printf("%d ",b1[i]);
    getch();
    }
    void rh()
    { //Expansion permutation of Right-half of data.
    b3[0]=br[31];b3[1]=br[0];b3[2]=br[1];b3[3]=br[2];b3[4]=br[3];b3[5]=br[4];
    b3[6]=br[3];b3[7]=br[4];b3[8]=br[5];b3[9]=br[6];b3[10]=br[7];b3[11]=br[8];
    b3[12]=br[7];b3[13]=br[8];b3[14]=br[9];b3[15]=br[10];b3[16]=br[11];
    b3[17]=br[12];b3[18]=br[11];b3[19]=br[12];b3[20]=br[13];b3[21]=br[14];
    b3[22]=br[15];b3[23]=br[16];b3[24]=br[15];b3[25]=br[16];b3[26]=br[17];
    b3[27]=br[18];b3[28]=br[19];b3[29]=br[20];b3[30]=br[19];b3[31]=br[20];
    b3[32]=br[21];b3[33]=br[22];b3[34]=br[23];b3[35]=br[24];b3[36]=br[23];
    b3[37]=br[24];b3[38]=br[25];b3[39]=br[26];b3[40]=br[27];b3[41]=br[28];
    b3[42]=br[27];b3[43]=br[28];b3[44]=br[29];b3[45]=br[30];b3[46]=br[31];
    b3[47]=br[0];
    }
    void sl()
    {
    int temp;
    temp=k2[0];
    for(i=0;i<27;i++)
    k2[i]=k2[i+1];
    k2[27]=temp;

    if(r==3||r==4||r==5||r==6||r==7||r==8||r==10||r==11||r==12||r==13||r==14||r==15)
    {
    temp=k2[0];
    for(i=0;i<27;i++)
    k2[i]=k2[i+1];
    k2[27]=temp;

    }
    temp=k2[28];
    for(i=28;i<55;i++)
    k2[i]=k2[i+1];
    k2[55]=temp;

    if(r==3||r==4||r==5||r==6||r==7||r==8||r==10||r==11||r==12||r==13||r==14||r==15)
    {
    temp=k2[28];
    for(i=28;i<55;i++)
    k2[i]=k2[i+1];
    k2[55]=temp;

    } //Compression permutation of the key.
    k3[0]=k2[13];k3[1]=k2[16];k3[2]=k2[10];k3[3]=k2[23];k3[4]=k2[0];k3[5]=k2[4];k3[6]=k2[2];
    k3[7]=k2[27];k3[8]=k2[14];k3[9]=k2[5];k3[10]=k2[20];k3[11]=k2[9];k3[12]=k2[22];
    k3[13]=k2[18];k3[14]=k2[11];k3[15]=k2[3];k3[16]=k2[25];k3[17]=k2[7];k3[18]=k2[15];
    k3[19]=k2[6];k3[20]=k2[26];k3[21]=k2[19];k3[22]=k2[12];k3[23]=k2[1];k3[24]=k2[40];
    k3[25]=k2[51];k3[26]=k2[30];k3[27]=k2[36];k3[28]=k2[46];k3[29]=k2[54];k3[30]=k2[29];
    k3[31]=k2[39];k3[32]=k2[50];k3[33]=k2[44];k3[34]=k2[32];k3[35]=k2[47];k3[36]=k2[43];
    k3[37]=k2[48];k3[38]=k2[38];k3[39]=k2[55];k3[40]=k2[33];k3[41]=k2[52];k3[42]=k2[45];
    k3[43]=k2[41];k3[44]=k2[49];k3[45]=k2[35];k3[46]=k2[28];k3[47]=k2[31];

    } //sl() ends.


    int row(int bitl,int bitr)
    {
    int rown;
    /**/
    t[bitr]=t[bitr]*1;
    t[bitl]=t[bitr]*2;
    rown=t[bitr]+t[bitl];
    return(rown);
    }
    int sbox(int p,int q,int r,int s)
    {
    int col;
    t[s]=t[s]*1;
    t[r]=t[r]*2;
    t[q]=t[q]*4;
    t[p]=t[p]*8;
    col=t[s]+t[r]+t[q]+t[p];
    /**/
    return(col);
    }
    void bindec(int dec)
    {
    /**/
    int vrble=3,dec1;
    while(vrble!=0)
    {
    dec1=dec%2;
    if(dec1==0)
    br1[count+vrble]=0;
    else
    br1[count+vrble]=1;
    vrble=vrble-1;
    dec=dec/2;
    }
    count=count+4;
    }
    void p1()
    {
    for(i=0;i<32;i++) //Permutation of 32-bit output of s-boxes
    br2[i]=br1[i];
    br1[0]=br2[15];
    br1[1]=br2[6];
    br1[2]=br2[19];
    br1[3]=br2[20];
    br1[4]=br2[28];
    br1[5]=br2[11];
    br1[6]=br2[27];
    br1[7]=br2[16];
    br1[8]=br2[0];
    br1[9]=br2[14];
    br1[10]=br2[22];
    br1[11]=br2[25];
    br1[12]=br2[4];
    br1[13]=br2[17];
    br1[14]=br2[30];
    br1[15]=br2[9];
    br1[16]=br2[1];
    br1[17]=br2[7];
    br1[18]=br2[23];
    br1[19]=br2[13];
    br1[20]=br2[31];
    br1[21]=br2[26];
    br1[22]=br2[2];
    br1[23]=br2[8];
    br1[24]=br2[18];
    br1[25]=br2[12];
    br1[26]=br2[29];
    br1[27]=br2[5];
    br1[28]=br2[21];
    br1[29]=br2[10];
    br1[30]=br2[3];
    br1[31]=br2[24];
    }
    From Abhishek Dwivedi.

  2. #2
    Senior Member
    Join Date
    May 2004
    Posts
    206
    That is the most un-readable piece of code I've ever seen. Use more comments use and code tags, that'd help a lot.
    It is better to die on your feet than to live on your knees.

  3. #3
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351

    Exclamation incoherent

    i'd help...but you have to rearrange the code so it's a bit easier to read....it is very very hard to follow all of the arrays.

    A_T

    PS - make alot of useful comments in your code as well, so as people can learn from it....you see my friend is writing a class that allows for numbers that are 4MB in length, and i want to write a corresponding alorithm, but I don't know enough about cryptology to do it.
    Geek isn't just a four-letter word; it's a six-figure income.

  4. #4
    Senior Member
    Join Date
    Oct 2001
    Posts
    786
    Your code isn't readable. And even if it was readable, I would be completely lost in the sea of arrays being initilized in the code...

    To be honest, I haven't worked with DES myself, so all I can really recommend is to use Google to find some code. Good luck.

    BTW, Export regulations aren't 100% clear, but I think DES may be allowed -- I simply haven't worked with it so I can't do much than tell you to search...

  5. #5
    Junior Member
    Join Date
    Jul 2003
    Posts
    9

    Post Reply to members

    Hello again,
    To the members who say that the code is unreadable,I must say that there is not much to "read" actually.The "sea of arrays" as described by TimAxe are the s-boxes,permutations[expansion and compression] etc.My only request was to the members to verify the code.
    Anyway, the logic of coding DES is extremely simple,which again makes renderless the idea of "reading" the code. One only has to read the algorithm,which I think the members who have replied have not. The tedium is in the typing of the s-boxes and permutations. Again, I would like it if someone could verify the code.I read the algorithm from a very good book,but which did not have any examples,and the one which had an example[also many printing mistakes],did not match the answer my code gave.
    Thank you.
    P.S. Jinx and swordfish went extra lengths to suggest me to use code tags,but I still dont know how to use them and did not find them in the options.I am sorry.
    From Abhishek Dwivedi.

  6. #6
    Senior Member PacketThirst's Avatar
    Join Date
    Aug 2004
    Posts
    258


    First of all .... You might wanna leave a few spaces after each block

    i.e.
    {
    ---->Start Here !
    }

    Learn to Indent your code. This is a good link
    http://www.cs.arizona.edu/people/mccann/indent_c.html

    As for the DES thing .... i don't have much idea abt it. Your code really looks hostile and no one would ever wanna get anywhere near the likes of it !!

  7. #7
    AntiOnline n00b
    Join Date
    Feb 2004
    Posts
    666
    Hi,

    First of all .... You might wanna leave a few spaces after each block

    i.e.
    {
    ---->Start Here !
    }
    PacketThirst He is leaving space in the Code .............. but this Forum is Removing them when he is pasting teh code here.

    P.S. Jinx and swordfish went extra lengths to suggest me to use code tags,but I still dont know how to use them and did not find them in the options.I am sorry.


    Try reading about teh Site Codes here The Code and PHP Tags.................Especially


    The Code and PHP Tags

    If you want to post some programming source code, or perhaps some ASCII art, which would require a non-proportional font, you can use the [code] tag to achieve this


    Section .


    --Good Luck--

  8. #8
    I got bored and decided to format the code. For the sake of not taking up an entire page with the source, I attached it. I hope this helps someone answer AbhishekDwivedi's question.

    AbhishekDwivedi:
    If I messed it up somehow, please let me know. I'm sorry I can't answer your question, I don't know anything about DES

  9. #9
    Senior Member
    Join Date
    Jan 2004
    Location
    Hawaii
    Posts
    351
    Generic Questions:
    What is "parity checking"?
    What are s1-s8 for?
    How/Why convert 64-bit number to 56-bit number?
    What is XOR, and how does it work? (don't use a truth table, I don't get it)

    A_T
    Geek isn't just a four-letter word; it's a six-figure income.

  10. #10
    Just Another Geek
    Join Date
    Jul 2002
    Location
    Rotterdam, Netherlands
    Posts
    3,401
    Originally posted here by AxessTerminated
    Generic Questions:
    What is "parity checking"?
    It basicly means you're counting the bits. With even parity you count all the 1 bits, if it's an even number the parity is 0, if it's an odd number the parity is 1. It's mostly used as a crude and simple errorcheck.


    What are s1-s8 for?
    These are permutation tables and are what makes DES 'work'.


    How/Why convert 64-bit number to 56-bit number?
    A 56-bit number with added parity (8 bits) makes 64-bits.


    What is XOR, and how does it work? (don't use a truth table, I don't get it)
    Lookup "Boolean Algebra". It's sort of digital math..
    Oliver's Law:
    Experience is something you don't get until just after you need it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •