Advertisement
Guest User

PC4 Encryption cipher algorithm for DMR RADIO

a guest
Apr 26th, 2024
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 17.79 KB | Source Code | 0 0
  1. /* PC4 Cipher */
  2. /* 49-bit block cipher in ECB Mode for DMR Radio*/
  3. /* by Alexander PUKALL 2015 */
  4. /* Code free for all, even for commercial software */
  5. /* No restriction to use. Public Domain */
  6.  
  7. /* Use MD2-II */
  8. /* Use Arc4 */
  9. /* Use Splitmix64 */
  10.  
  11. /* PC4 encryption uses key-dependent S-boxes */
  12.  
  13. /* Key can be hexadecimal or user password */
  14. /* Key size can vary from 8 bits to 2112 bits */
  15.  
  16. /* PC4 uses 253 encryption rounds */
  17. /* The number of round can be reduced */
  18. /* if the DMR processor is too slow */
  19.  
  20. /* how to compile : gcc pc4.c -o pc4 */
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <stdint.h>
  25.  
  26. #define nbround 254
  27. #define n1 264
  28. short bits[49],temp[49];
  29. uint8_t ptconvert;
  30. uint8_t convert[7];
  31. uint8_t perm[16][256];
  32. uint8_t new1[256];
  33. uint8_t array[49];
  34. uint8_t array2[49];
  35. uint8_t decal[nbround];
  36. uint8_t rngxor[nbround][3];
  37. uint8_t rngxor2[nbround][3];
  38. uint8_t rounds;
  39. uint8_t tab[256];
  40. uint8_t inv[256];
  41. uint8_t permut[3][3];
  42. uint64_t bb;
  43. uint64_t x;
  44. uint8_t tot[3];
  45. uint8_t l[2][3],r[2][3];
  46. uint8_t y,totb;
  47. uint32_t result;
  48. uint8_t xyz, count;
  49. uint8_t keys[16];
  50. unsigned char array_arc4[256];
  51. int i_arc4,j_arc4;
  52.  
  53. int x1,x2,i;
  54. unsigned char h2[n1];
  55. unsigned char h1[n1*3];
  56.  
  57. uint64_t next() {
  58.  
  59.     uint64_t z = (x += 0x9e3779b97f4a7c15);
  60.     z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
  61.     z = (z ^ (z >> 27)) * 0x94d049bb133111eb;
  62.  
  63.     return z ^ (z >> 31);
  64. }
  65.  
  66.  
  67. void arc4_init(unsigned char key[])
  68.  
  69. {
  70.        int tmp;
  71.            
  72.        for (i_arc4=0;i_arc4<256;i_arc4++)
  73.        {
  74.           array_arc4[i_arc4]=i_arc4;
  75.        }
  76.        
  77.        
  78.        j_arc4=0;
  79.                  
  80.         for (i_arc4=0;i_arc4<256;i_arc4++)
  81.        {
  82.          j_arc4=(j_arc4+array_arc4[i_arc4]+key[i_arc4%256])%256;
  83.          tmp=array_arc4[i_arc4];
  84.          array_arc4[i_arc4]=array_arc4[j_arc4];
  85.          array_arc4[j_arc4]=tmp;
  86.        }
  87.  
  88.  i_arc4=0;
  89.  j_arc4=0;
  90. }
  91.        
  92. unsigned char arc4_output()
  93.  
  94. {
  95.        uint8_t rndbyte,decal;
  96.        int tmp, t;
  97.  
  98.     i_arc4=(i_arc4+1)%256;
  99.     j_arc4=(j_arc4+array_arc4[i_arc4])%256;
  100.     tmp=array_arc4[i_arc4];
  101.     array_arc4[i_arc4]=array_arc4[j_arc4];
  102.     array_arc4[j_arc4]=tmp;
  103.     t=(array_arc4[i_arc4]+array_arc4[j_arc4])%256;
  104.  
  105.        if (xyz==0) bb=next();
  106.        decal=56-(8*xyz);  
  107.        rndbyte=(bb>>decal)& 0xff;
  108.        xyz++;
  109.        if (xyz==8) xyz=0;
  110.  
  111.    
  112.     if (count==0)
  113.      {
  114.        rndbyte=rndbyte^array_arc4[t];
  115.        count=1;
  116.      }
  117.      else
  118.      {
  119.        rndbyte=rndbyte+array_arc4[t];
  120.        count=0;
  121.      }
  122.      
  123.      
  124.     return(rndbyte);
  125.  
  126.  
  127. }
  128.  
  129.  
  130. void md2_init()
  131. {
  132.    
  133.    x1 = 0;
  134.    x2 = 0;
  135.     for (i = 0; i < n1; i++)
  136.         h2[i] = 0;
  137.     for (i = 0; i < n1; i++)
  138.         h1[i] = 0;
  139. }
  140.  
  141. void md2_hashing(unsigned char t1[], size_t b6)
  142. {
  143.     static unsigned char s4[256] =
  144.     {   13, 199,  11,  67, 237, 193, 164,  77, 115, 184, 141, 222,  73,
  145.         38, 147,  36, 150,  87,  21, 104,  12,  61, 156, 101, 111, 145,
  146.        119,  22, 207,  35, 198,  37, 171, 167,  80,  30, 219,  28, 213,
  147.        121,  86,  29, 214, 242,   6,   4,  89, 162, 110, 175,  19, 157,
  148.          3,  88, 234,  94, 144, 118, 159, 239, 100,  17, 182, 173, 238,
  149.         68,  16,  79, 132,  54, 163,  52,   9,  58,  57,  55, 229, 192,
  150.        170, 226,  56, 231, 187, 158,  70, 224, 233, 245,  26,  47,  32,
  151.         44, 247,   8, 251,  20, 197, 185, 109, 153, 204, 218,  93, 178,
  152.        212, 137,  84, 174,  24, 120, 130, 149,  72, 180, 181, 208, 255,
  153.        189, 152,  18, 143, 176,  60, 249,  27, 227, 128, 139, 243, 253,
  154.         59, 123, 172, 108, 211,  96, 138,  10, 215,  42, 225,  40,  81,
  155.         65,  90,  25,  98, 126, 154,  64, 124, 116, 122,   5,   1, 168,
  156.         83, 190, 131, 191, 244, 240, 235, 177, 155, 228, 125,  66,  43,
  157.        201, 248, 220, 129, 188, 230,  62,  75,  71,  78,  34,  31, 216,
  158.        254, 136,  91, 114, 106,  46, 217, 196,  92, 151, 209, 133,  51,
  159.        236,  33, 252, 127, 179,  69,   7, 183, 105, 146,  97,  39,  15,
  160.        205, 112, 200, 166, 223,  45,  48, 246, 186,  41, 148, 140, 107,
  161.         76,  85,  95, 194, 142,  50,  49, 134,  23, 135, 169, 221, 210,
  162.        203,  63, 165,  82, 161, 202,  53,  14, 206, 232, 103, 102, 195,
  163.        117, 250,  99,   0,  74, 160, 241,   2, 113};
  164.        
  165.     int b1,b2,b3,b4,b5;
  166.    
  167.     b4=0;
  168.     while (b6) {
  169.    
  170.         for (; b6 && x2 < n1; b6--, x2++) {
  171.             b5 = t1[b4++];
  172.             h1[x2 + n1] = b5;
  173.             h1[x2 + (n1*2)] = b5 ^ h1[x2];
  174.  
  175.             x1 = h2[x2] ^= s4[b5 ^ x1];
  176.         }
  177.  
  178.         if (x2 == n1)
  179.         {
  180.             b2 = 0;
  181.             x2 = 0;
  182.            
  183.             for (b3 = 0; b3 < (n1+2); b3++) {
  184.                 for (b1 = 0; b1 < (n1*3); b1++)
  185.                     b2 = h1[b1] ^= s4[b2];
  186.                 b2 = (b2 + b3) % 256;
  187.             }
  188.            }
  189.           }
  190.         }
  191.  
  192. void md2_end(unsigned char h4[n1])
  193. {
  194.    
  195.     unsigned char h3[n1];
  196.     int i, n4;
  197.    
  198.     n4 = n1 - x2;
  199.     for (i = 0; i < n4; i++) h3[i] = n4;
  200.     md2_hashing(h3, n4);
  201.     md2_hashing(h2, sizeof(h2));
  202.     for (i = 0; i < n1; i++) h4[i] = h1[i];
  203. }
  204.  
  205. int mixy(int nn2)
  206. {
  207. int maxo;
  208. maxo = arc4_output();
  209. return (maxo%nn2);
  210. }
  211.  
  212. void mixer(uint8_t *mixu, int nn)
  213. {
  214.    int ii, jj, tmmp;
  215.       for (ii = nn - 1; ii > 0; ii--)
  216.        {
  217.           jj = mixy(ii + 1);
  218.           tmmp = mixu[jj];
  219.           mixu[jj] = mixu[ii];
  220.           mixu[ii] = tmmp;
  221.        }
  222. }
  223.  
  224. void create_keys(unsigned char key1[], size_t size1)
  225. {
  226.    
  227.      int i,w,k;
  228.      unsigned char h4[n1];
  229.        
  230.          md2_init();
  231.        md2_hashing(key1, size1);
  232.        md2_end(h4);
  233.        
  234.        for (i=0;i<16;i++) keys[i]=h4[i];
  235.        
  236.        arc4_init(h4);
  237.        
  238.        x=0;
  239.        for (i=0;i<8;i++) x=(x<<8)+(h4[256+i]&0xff);
  240.      
  241.        xyz=0;
  242.        count=0;
  243.              
  244.      for (i=0;i<20000;i++) arc4_output();
  245.  
  246.       uint8_t numbers[256];
  247.        
  248.      for (w=0;w<16;w++)
  249.       {
  250.          
  251.        k=arc4_output()+256;  
  252.        for (i=0;i<k;i++) arc4_output();
  253.        
  254.         for (i = 0; i < 256; i++) {numbers[i]= i;}
  255.  
  256.        mixer(numbers, 256);
  257.  
  258.         for ( i = 0; i < 256; i++)
  259.         {
  260.           perm[w][i]=numbers[i];
  261.         }
  262.  
  263.       }
  264.    
  265.        k=arc4_output()+256;  
  266.        for (i=0;i<k;i++) arc4_output();
  267.    
  268.        for (int i = 0; i < 256; i++) {numbers[i]= i;}
  269.  
  270.         mixer(numbers, 256);
  271.  
  272.         for (int i = 0; i < 256; i++)
  273.         {
  274.           new1[i]=numbers[i];
  275.         }
  276.  
  277.        k=arc4_output()+256;  
  278.        for (i=0;i<k;i++) arc4_output();
  279.  
  280.       for (int i = 0; i < 49; i++) {numbers[i]= i;}
  281.  
  282.         mixer(numbers, 49);
  283.  
  284.            for (int i = 0; i < 49; i++)
  285.            {
  286.               array[i]=numbers[i];
  287.            }
  288.    
  289.       k=arc4_output()+256;  
  290.       for (i=0;i<k;i++) arc4_output();
  291.        
  292.     for (int i=0;i<nbround;i++)
  293.     {
  294.       decal[i]=(arc4_output()%23)+1;
  295.     }
  296.    
  297.       k=arc4_output()+256;  
  298.        for (i=0;i<k;i++) arc4_output();
  299.        
  300.     for (w=0;w<3;w++)
  301.     {
  302.         for (int i=0;i<nbround;i++)
  303.         {
  304.             rngxor[i][w]=arc4_output();
  305.         }
  306.     }
  307.  
  308.   k=arc4_output()+256;  
  309.        for (i=0;i<k;i++) arc4_output();
  310.        
  311. for (int i = 0; i < 49; i++) {numbers[i]= i;}
  312.  
  313.         mixer(numbers, 49);
  314.  
  315.            for (int i = 0; i < 49; i++)
  316.            {
  317.               array2[i]=numbers[i];
  318.            }
  319.    
  320.      k=arc4_output()+256;  
  321.        for (i=0;i<k;i++) arc4_output();
  322.        
  323.     for (int i = 0; i < 256; i++) {numbers[i]= i;}
  324.  
  325.         mixer(numbers, 256);
  326.  
  327.         for (int i = 0; i < 256; i++)
  328.         {
  329.           tab[i]=numbers[i];
  330.           inv[tab[i]] = (unsigned char)i;
  331.         }
  332.    
  333.      k=arc4_output()+256;  
  334.        for (i=0;i<k;i++) arc4_output();
  335.        
  336.         for (w=0;w<3;w++)
  337.       {
  338.    
  339.        k=arc4_output()+256;  
  340.        for (i=0;i<k;i++) arc4_output();
  341.        
  342.         for (i = 0; i < 3; i++) {numbers[i]= i;}
  343.  
  344.        mixer(numbers, 3);
  345.  
  346.         for ( i = 0; i < 3; i++)
  347.         {
  348.           permut[w][i]=numbers[i];
  349.         }
  350.       }
  351.      
  352.          k=arc4_output()+256;  
  353.        for (i=0;i<k;i++) arc4_output();
  354.        
  355.     for (w=0;w<3;w++)
  356.     {
  357.         for (int i=0;i<nbround;i++)
  358.         {
  359.             rngxor2[i][w]=arc4_output();
  360.         }
  361.     }
  362.        
  363.        
  364.   }
  365.  
  366. uint32_t ror(uint32_t x,int shift,int bits)
  367.     {
  368.                      
  369.     uint32_t m0=(1<<(bits-shift))-1;  
  370.                                  
  371.     uint32_t m1=(1<<shift)-1;        
  372.                                    
  373.     return ((x>>shift)&m0) | ((x&m1)<<(bits-shift));
  374.     }
  375.    
  376. uint32_t rol(uint32_t x,int shift,int bits)
  377.     {
  378.    
  379.                      
  380.     uint32_t m0=(1<<(bits-shift))-1;  
  381.                                    
  382.     uint32_t m1=(1<<shift)-1;          
  383.                                    
  384.     return ((x&m0)<<shift) | ((x>>(bits-shift))&m1);
  385.     }
  386.  
  387. void compute(uint8_t *tab1, uint8_t round)
  388. {
  389.  tot[0]=(perm[round][tab1[permut[0][0]]]+perm[round][tab1[permut[0][1]]])^perm[round][tab1[permut[0][2]]];
  390.  tot[0]=tot[0]+new1[tot[0]];
  391.  tot[1]=(perm[round][tab1[permut[1][0]]]+perm[round][tab1[permut[1][1]]])^perm[round][tab1[permut[1][2]]];
  392.  tot[1]=tot[1]+new1[tot[1]];
  393.  tot[2]=(perm[round][tab1[permut[2][0]]]+perm[round][tab1[permut[2][1]]])^perm[round][tab1[permut[2][2]]];
  394.  tot[2]=tot[2]+new1[tot[2]];
  395. }
  396.  
  397. void pc4encrypt()
  398. {
  399.  int i;
  400.  totb=0;
  401.    
  402.   for (int i=0;i<3;i++)
  403.   {
  404.     l[0][i]=convert[i];
  405.     r[0][i]=convert[i+3];
  406.   }
  407.  
  408.  
  409.   for (int i=1;i<=rounds;i++)
  410.   {
  411.        totb=totb^r[(i-1)%2][0];
  412.        totb=totb^r[(i-1)%2][1];
  413.        totb=totb^r[(i-1)%2][2];
  414.        
  415.        r[(i-1)%2][0]=r[(i-1)%2][0] + ((unsigned char)~rngxor2[rounds-i][0]);
  416.        r[(i-1)%2][1]=r[(i-1)%2][1] ^ ((unsigned char)~rngxor2[rounds-i][1]);
  417.        r[(i-1)%2][2]=r[(i-1)%2][2] + ((unsigned char)~rngxor2[rounds-i][2]);
  418.        
  419.        result=0;
  420.        result=result+(r[(i-1)%2][0]<<16);
  421.        result=result+(r[(i-1)%2][1]<<8);
  422.        result=(result+r[(i-1)%2][2]);
  423.        
  424.        result=rol(result,decal[i-1],24);
  425.    
  426.                
  427.        r[(i-1)%2][0]=result>>16;
  428.        r[(i-1)%2][1]=(result>>8)& 0xff;
  429.        r[(i-1)%2][2]=result & 0xff;
  430.        
  431.        
  432.        r[(i-1)%2][0]=tab[(r[(i-1)%2][0])];
  433.        r[(i-1)%2][0]=r[(i-1)%2][0] ^ rngxor[i-1][0];
  434.        
  435.        r[(i-1)%2][1]=inv[(r[(i-1)%2][1])];
  436.        r[(i-1)%2][1]=r[(i-1)%2][1] - rngxor[i-1][1];
  437.        
  438.        r[(i-1)%2][2]=tab[(r[(i-1)%2][2])];
  439.        r[(i-1)%2][2]=r[(i-1)%2][2] ^ rngxor[i-1][2];
  440.        
  441.        
  442.        compute(r[(i-1)%2],(i-1)%16);
  443.  
  444.          
  445.        l[i%2][0]=r[(i-1)%2][0];
  446.        r[i%2][0]=l[(i-1)%2][0] -tot[0];
  447.      
  448.        l[i%2][1]=r[(i-1)%2][1];
  449.        r[i%2][1]=l[(i-1)%2][1] ^tot[1];
  450.        
  451.        l[i%2][2]=r[(i-1)%2][2];
  452.        r[i%2][2]=l[(i-1)%2][2] -tot[2];
  453.              
  454.   }
  455.  
  456.  
  457.   for (i=0;i<3;i++)
  458.   {
  459.     convert[i+3]=l[(rounds-1)%2][i];
  460.     convert[i]=r[(rounds-1)%2][i];
  461.   }
  462.  
  463.  
  464.  totb=totb%2;
  465.  
  466. }
  467.  
  468. void pc4decrypt()
  469.  
  470. {
  471.     int i;
  472.   totb=0;
  473.    
  474.      for (i=0;i<3;i++)
  475.   {
  476.     l[0][i]=convert[i];
  477.     r[0][i]=convert[i+3];
  478.   }
  479.  
  480.   y=(rounds-1)%16;
  481.   if (y==0) y=16;  
  482.          
  483.   for (i=1;i<=rounds;i++)
  484.   {
  485.        y--;
  486.        compute(r[(i-1)%2],y);
  487.        if (y==0) y=16;
  488.      
  489.        result=0;
  490.        
  491.        l[(i-1)%2][0]=l[(i-1)%2][0] ^ rngxor[rounds-i][0];
  492.        l[(i-1)%2][0]=inv[(l[(i-1)%2][0])];
  493.        
  494.        l[(i-1)%2][1]=l[(i-1)%2][1] + rngxor[rounds-i][1];
  495.        l[(i-1)%2][1]=tab[(l[(i-1)%2][1])];
  496.        
  497.        l[(i-1)%2][2]=l[(i-1)%2][2] ^ rngxor[rounds-i][2];
  498.        l[(i-1)%2][2]=inv[(l[(i-1)%2][2])];
  499.        
  500.            
  501.        
  502.        result=result+(l[(i-1)%2][0]<<16);
  503.        result=result+(l[(i-1)%2][1]<<8);
  504.        result=(result+l[(i-1)%2][2]);
  505.  
  506.        result=ror(result,decal[rounds-i],24);
  507.  
  508.          
  509.        l[(i-1)%2][0]=result>>16;
  510.        l[(i-1)%2][1]=(result>>8)& 0xff;
  511.        l[(i-1)%2][2]=result & 0xff;
  512.        
  513.        
  514.        l[(i-1)%2][0]=l[(i-1)%2][0] - ((unsigned char)~rngxor2[i-1][0]);
  515.        l[(i-1)%2][1]=l[(i-1)%2][1] ^ ((unsigned char)~rngxor2[i-1][1]);
  516.        l[(i-1)%2][2]=l[(i-1)%2][2] - ((unsigned char)~rngxor2[i-1][2]);
  517.      
  518.        totb=totb^l[(i-1)%2][0];
  519.        totb=totb^l[(i-1)%2][1];
  520.        totb=totb^l[(i-1)%2][2];
  521.        
  522.        l[i%2][0]=r[(i-1)%2][0];
  523.        r[i%2][0]=l[(i-1)%2][0] + tot[0];
  524.        
  525.        l[i%2][1]=r[(i-1)%2][1];
  526.        r[i%2][1]=l[(i-1)%2][1] ^ tot[1];
  527.        
  528.        l[i%2][2]=r[(i-1)%2][2];
  529.        r[i%2][2]=l[(i-1)%2][2] + tot[2];
  530.      
  531.  
  532.   }
  533.  
  534.  
  535.   for (i=0;i<3;i++)
  536.   {
  537.     convert[i+3]=l[(rounds-1)%2][i];
  538.     convert[i]=r[(rounds-1)%2][i];
  539.   }
  540.  
  541.  
  542.   totb=totb%2;
  543.  
  544. }
  545.    
  546. void binhex(short *z, int length){
  547.  
  548.     short *b;
  549.   b=(short *)z;
  550.  
  551.     uint8_t i,j;
  552.  
  553.     for(i = 0; i < length; i = j){
  554.         uint8_t a = 0;
  555.         for(j = i; j < i+8; ++j){
  556.             a |= b[((7-(j%8))+j)-(j%8)]<<(j-i);
  557.         }
  558.        convert[ptconvert]=a;
  559.        ptconvert++;
  560.        
  561.     }          
  562. }
  563.  
  564. void hexbin(short *q, uint8_t w, uint8_t hex) {
  565.    
  566.        short *bits;
  567.   bits=(short *)q;
  568.  
  569.     for (uint8_t i = 0; i < 8; ++i) {
  570.         bits[(7+w)-i] = (hex >> i) & 1;
  571.        
  572.     }
  573. }
  574.  
  575. void main()
  576. {
  577.     rounds=254;
  578.    
  579.    
  580.     // Plaintext frame //
  581.     uint8_t frame1[49]={
  582.     0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  583.     0,0,0,0,0,0,0,0,0};
  584.    
  585.     uint8_t i,w;
  586.      
  587.        
  588.     // KEY 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
  589.     unsigned char data[16]={1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  590.    
  591.     /* The key creation procedure is very slow, it only needs to be done once */
  592.     /* as long as the user does not change the key. You can encrypt and decrypt */
  593.     /* as many frames as you want without having to recreate the key. */
  594.    
  595.     create_keys(data,16); // KEY creation : key, length of key
  596.    
  597.    printf("\nENCRYPTION PROCESS with key 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01\n");
  598.    
  599.      
  600.    for (i=0;i<49;i++) bits[i]=frame1[i];
  601.    
  602.    printf("Plaintext Frame :\n");
  603.    for (i=0;i<49;i++) printf("%d",bits[i]);
  604.    
  605.    for (i=0; i<49; i++) temp[i]=bits[array[i]];
  606.    for (i=0; i<49; i++) bits[i]=temp[i];
  607.  
  608.     ptconvert=0;
  609.   binhex(bits, 48);
  610.  
  611.     pc4encrypt();
  612.  
  613.     for (int q=0;q<6;q++)
  614.     {
  615.     w=q*8;
  616.     hexbin(bits, w,convert[q]);
  617.     }
  618.  
  619.    bits[48]=bits[48]^totb;
  620.    
  621.    for (i=0; i<49; i++) temp[array2[i]]=bits[i];
  622.    for (i=0; i<49; i++) bits[i]=temp[i];
  623.  
  624.  
  625.    printf("\nEncrypted Frame :\n");
  626.    for (i=0;i<49;i++) printf("%d",bits[i]);
  627.    
  628.    printf("\n\nDECRYPTION PROCESS with key 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01\n");
  629.    
  630.      
  631.     printf("Encrypted Frame :\n");
  632.    for (i=0;i<49;i++) printf("%d",bits[i]);
  633.    
  634.    
  635.    for (i=0; i<49; i++) temp[i]=bits[array2[i]];
  636.    for (i=0; i<49; i++) bits[i]=temp[i];
  637.    
  638.    ptconvert=0;
  639.    binhex(bits, 48);
  640.  
  641.    pc4decrypt();
  642.  
  643.   for (int q=0;q<6;q++)
  644.     {
  645.     w=q*8;
  646.     hexbin(bits, w,convert[q]);
  647.     }
  648.    
  649.    bits[48]=bits[48]^totb;
  650.    
  651.    for (i=0; i<49; i++) temp[array[i]]=bits[i];
  652.    for (i=0; i<49; i++) bits[i]=temp[i];
  653.  
  654.    printf("\nDecrypted Frame :\n");
  655.    for (i=0;i<49;i++) printf("%d",bits[i]);
  656.  
  657.  
  658.      /*     USER PASSWORD               */
  659.      
  660.      // plaintext frame
  661.       uint8_t frame2[49]={
  662.     0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,
  663.     0,0,1,0,0,0,0,0,1};
  664.      
  665.     /* The key creation procedure is very slow, it only needs to be done once */
  666.     /* as long as the user does not change the key. You can encrypt and decrypt */
  667.     /* as many frames as you want without having to recreate the key. */
  668.    
  669.     create_keys("My Top Secret Password!",23); // KEY creation : password, length of password
  670.    
  671.    printf("\n\nENCRYPTION PROCESS with user password 'My Top Secret Password!'\n");
  672.    
  673.    for (i=0;i<49;i++) bits[i]=frame2[i];
  674.    
  675.    printf("Plaintext Frame :\n");
  676.    for (i=0;i<49;i++) printf("%d",bits[i]);
  677.    
  678.      
  679.    for (i=0; i<49; i++) temp[i]=bits[array[i]];
  680.    for (i=0; i<49; i++) bits[i]=temp[i];
  681.  
  682.     ptconvert=0;
  683.   binhex(bits, 48);
  684.  
  685.     pc4encrypt();
  686.  
  687.     for (int q=0;q<6;q++)
  688.     {
  689.     w=q*8;
  690.     hexbin(bits, w,convert[q]);
  691.     }
  692.  
  693.    bits[48]=bits[48]^totb;
  694.    
  695.    for (i=0; i<49; i++) temp[array2[i]]=bits[i];
  696.    for (i=0; i<49; i++) bits[i]=temp[i];
  697.  
  698.  
  699.    printf("\nEncrypted Frame :\n");
  700.    for (i=0;i<49;i++) printf("%d",bits[i]);
  701.    
  702.    printf("\n\nDECRYPTION PROCESS with user password 'My Top Secret Password!'\n");
  703.    
  704.      
  705.     printf("Encrypted Frame :\n");
  706.    for (i=0;i<49;i++) printf("%d",bits[i]);
  707.    
  708.    
  709.    for (i=0; i<49; i++) temp[i]=bits[array2[i]];
  710.    for (i=0; i<49; i++) bits[i]=temp[i];
  711.    
  712.    ptconvert=0;
  713.    binhex(bits, 48);
  714.  
  715.    pc4decrypt();
  716.  
  717.   for (int q=0;q<6;q++)
  718.     {
  719.     w=q*8;
  720.     hexbin(bits, w,convert[q]);
  721.     }
  722.    
  723.    bits[48]=bits[48]^totb;
  724.    
  725.    for (i=0; i<49; i++) temp[array[i]]=bits[i];
  726.    for (i=0; i<49; i++) bits[i]=temp[i];
  727.  
  728.    printf("\nDecrypted Frame :\n");
  729.    for (i=0;i<49;i++) printf("%d",bits[i]);
  730.  
  731.     printf("\n");
  732.  
  733.  
  734. }
  735.  
  736. /*  
  737. ENCRYPTION PROCESS with key 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
  738. Plaintext Frame :
  739. 0000000000000000000000000000000000000000000000000
  740. Encrypted Frame :
  741. 1000110101001100000100001000111011010010001001101
  742.  
  743. DECRYPTION PROCESS with key 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01
  744. Encrypted Frame :
  745. 1000110101001100000100001000111011010010001001101
  746. Decrypted Frame :
  747. 0000000000000000000000000000000000000000000000000
  748.  
  749. ENCRYPTION PROCESS with user password 'My Top Secret Password!'
  750. Plaintext Frame :
  751. 0010000001000000001000000000000010000000001000001
  752. Encrypted Frame :
  753. 0000011100110010010110111010110010110100000110100
  754.  
  755. DECRYPTION PROCESS with user password 'My Top Secret Password!'
  756. Encrypted Frame :
  757. 0000011100110010010110111010110010110100000110100
  758. Decrypted Frame :
  759. 0010000001000000001000000000000010000000001000001
  760. */
  761.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement