Guest User

Untitled

a guest
Jul 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.17 KB | None | 0 0
  1. #ifndef MyCLassesH
  2. #define MyClassesH
  3.  
  4. typedef unsigned char column [4];
  5. typedef unsigned char  t_matr [4][4];
  6.  
  7. unsigned char mixmatr [4][4]={{0x02, 0x03, 0x01, 0x01},
  8.                               {0x01, 0x02, 0x03, 0x01,},
  9.                               {0x01, 0x01, 0x02, 0x03},
  10.                               {0x03, 0x01, 0x01, 0x02}};
  11.  
  12.  
  13. unsigned char invmixmatr [4][4]={{0x0e, 0x0b, 0x0d, 0x09},
  14.                               {0x09, 0x0e, 0x0b, 0x0d,},
  15.                               {0x0d, 0x09, 0x0e, 0x0b},
  16.                               {0x0b, 0x0d, 0x09, 0x0e}};
  17. class Matrix
  18. {   public:
  19.         t_matr m;
  20.  
  21.     public:
  22.         void Initialize(t_matr a)
  23.         {
  24.             for ( int i = 0; i < 4; i++)
  25.             {
  26.                 for ( int j = 0; j < 4; j++) this->m[i][j]=a[i][j];
  27.                    
  28.             }
  29.         }
  30.  
  31.         void ShiftOneLeft (int i)
  32.         {
  33.             unsigned char t;
  34.             t=this->m[i][0];
  35.             for (int j = 0; j < 3; j++) this->m[i][j]=this->m[i][j+1];
  36.             this->m[i][3] = t;
  37.         }
  38.  
  39.         void ShiftOneRight (int i)
  40.         {
  41.             unsigned char t;
  42.             t=this->m[i][3];
  43.             for (int j = 3; j > 0; j--)  this->m[i][j]=this->m[i][j-1];
  44.             this->m[i][0] = t;
  45.  
  46.         }
  47.  
  48.         void ShiftRows ()
  49.         {
  50.           ShiftOneLeft(1);
  51.           ShiftOneLeft(2);
  52.           ShiftOneLeft(2);
  53.           ShiftOneLeft(3);
  54.           ShiftOneLeft(3);
  55.           ShiftOneLeft(3);
  56.         }
  57.  
  58.         void InvShiftRows ()
  59.         {
  60.           ShiftOneRight(1);
  61.           ShiftOneRight(2);
  62.           ShiftOneRight(2);
  63.           ShiftOneRight(3);
  64.           ShiftOneRight(3);
  65.           ShiftOneRight(3);
  66.         }
  67.  
  68. };
  69.  
  70.  
  71.  
  72. class ChangesTable
  73. {
  74.     private:
  75.         int Sb [256];
  76.         int InvSb [256];
  77.  
  78. public:
  79.  
  80.         static ChangesTable& const Instance()
  81.         {
  82.                 static ChangesTable theSingleInstance;
  83.                 return theSingleInstance;
  84.         }
  85.  
  86.     void Initialize()
  87.     {
  88.         int tempb [256] = { 0x63,0x7c,0x77,0x7b,0xf2,0x6b,0x6f,0xc5,0x30,0x01,0x67,0x2b,0xfe,0xd7,0xab,0x76,
  89.         0xca,0x82,0xc9,0x7d,0xfa,0x59,0x47,0xf0,0xad,0xd4,0xa2,0xaf,0x9c,0xa4,0x72,0xc0,
  90.         0xb7,0xfd,0x93,0x26,0x36,0x3f,0xf7,0xcc,0x34,0xa5,0xe5,0xf1,0x71,0xd8,0x31,0x15,
  91.         0x04,0xc7,0x23,0xc3,0x18,0x96,0x05,0x9a,0x07,0x12,0x80,0xe2,0xeb,0x27,0xb2,0x75,
  92.         0x09,0x83,0x2c,0x1a,0x1b,0x6e,0x5a,0xa0,0x52,0x3b,0xd6,0xb3,0x29,0xe3,0x2f,0x84,
  93.         0x53,0xd1,0x00,0xed,0x20,0xfc,0xb1,0x5b,0x6a,0xcb,0xbe,0x39,0x4a,0x4c,0x58,0xcf,
  94.         0xd0,0xef,0xaa,0xfb,0x43,0x4d,0x33,0x85,0x45,0xf9,0x02,0x7f,0x50,0x3c,0x9f,0xa8,
  95.         0x51,0xa3,0x40,0x8f,0x92,0x9d,0x38,0xf5,0xbc,0xb6,0xda,0x21,0x10,0xff,0xf3,0xd2,
  96.         0xcd,0x0c,0x13,0xec,0x5f,0x97,0x44,0x17,0xc4,0xa7,0x7e,0x3d,0x64,0x5d,0x19,0x73,
  97.         0x60,0x81,0x4f,0xdc,0x22,0x2a,0x90,0x88,0x46,0xee,0xb8,0x14,0xde,0x5e,0x0b,0xdb,
  98.         0xe0,0x32,0x3a,0x0a,0x49,0x06,0x24,0x5c,0xc2,0xd3,0xac,0x62,0x91,0x95,0xe4,0x79,
  99.         0xe7,0xc8,0x37,0x6d,0x8d,0xd5,0x4e,0xa9,0x6c,0x56,0xf4,0xea,0x65,0x7a,0xae,0x08,
  100.         0xba,0x78,0x25,0x2e,0x1c,0xa6,0xb4,0xc6,0xe8,0xdd,0x74,0x1f,0x4b,0xbd,0x8b,0x8a,
  101.         0x70,0x3e,0xb5,0x66,0x48,0x03,0xf6,0x0e,0x61,0x35,0x57,0xb9,0x86,0xc1,0x1d,0x9e,
  102.         0xe1,0xf8,0x98,0x11,0x69,0xd9,0x8e,0x94,0x9b,0x1e,0x87,0xe9,0xce,0x55,0x28,0xdf,
  103.         0x8c,0xa1,0x89,0x0d,0xbf,0xe6,0x42,0x68,0x41,0x99,0x2d,0x0f,0xb0,0x54,0xbb,0x16
  104.         }  ;
  105.         int tempinvb [256]={ 0x52,0x09,0x6a,0xd5,0x30,0x36,0xa5,0x38,0xbf,0x40,0xa3,0x9e,0x81,0xf3,0xd7,0xfb,
  106.         0x7c,0xe3,0x39,0x82,0x9b,0x2f,0xff,0x87,0x34,0x8e,0x43,0x44,0xc4,0xde,0xe9,0xcb,
  107.         0x54,0x7b,0x94,0x32,0xa6,0xc2,0x23,0x3d,0xee,0x4c,0x95,0x0b,0x42,0xfa,0xc3,0x4e,
  108.         0x08,0x2e,0xa1,0x66,0x28,0xd9,0x24,0xb2,0x76,0x5b,0xa2,0x49,0x6d,0x8b,0xd1,0x25,
  109.         0x72,0xf8,0xf6,0x64,0x86,0x68,0x98,0x16,0xd4,0xa4,0x5c,0xcc,0x5d,0x65,0xb6,0x92,
  110.         0x6c,0x70,0x48,0x50,0xfd,0xed,0xb9,0xda,0x5e,0x15,0x46,0x57,0xa7,0x8d,0x9d,0x84,
  111.         0x90,0xd8,0xab,0x00,0x8c,0xbc,0xd3,0x0a,0xf7,0xe4,0x58,0x05,0xb8,0xb3,0x45,0x06,
  112.         0xd0,0x2c,0x1e,0x8f,0xca,0x3f,0x0f,0x02,0xc1,0xaf,0xbd,0x03,0x01,0x13,0x8a,0x6b,
  113.         0x3a,0x91,0x11,0x41,0x4f,0x67,0xdc,0xea,0x97,0xf2,0xcf,0xce,0xf0,0xb4,0xe6,0x73,
  114.         0x96,0xac,0x74,0x22,0xe7,0xad,0x35,0x85,0xe2,0xf9,0x37,0xe8,0x1c,0x75,0xdf,0x6e,
  115.         0x47,0xf1,0x1a,0x71,0x1d,0x29,0xc5,0x89,0x6f,0xb7,0x62,0x0e,0xaa,0x18,0xbe,0x1b,
  116.         0xfc,0x56,0x3e,0x4b,0xc6,0xd2,0x79,0x20,0x9a,0xdb,0xc0,0xfe,0x78,0xcd,0x5a,0xf4,
  117.         0x1f,0xdd,0xa8,0x33,0x88,0x07,0xc7,0x31,0xb1,0x12,0x10,0x59,0x27,0x80,0xec,0x5f,
  118.         0x60,0x51,0x7f,0xa9,0x19,0xb5,0x4a,0x0d,0x2d,0xe5,0x7a,0x9f,0x93,0xc9,0x9c,0xef,
  119.         0xa0,0xe0,0x3b,0x4d,0xae,0x2a,0xf5,0xb0,0xc8,0xeb,0xbb,0x3c,0x83,0x53,0x99,0x61,
  120.         0x17,0x2b,0x04,0x7e,0xba,0x77,0xd6,0x26,0xe1,0x69,0x14,0x63,0x55,0x21,0x0c,0x7d,
  121.         };
  122.         for ( int i = 0; i < 256; i++)
  123.             {
  124.                 this->Sb[i]=tempb[i];
  125.                 this->InvSb[i]=tempinvb[i];
  126.             }  
  127.         }
  128.  
  129.  
  130.     unsigned char GetSBoxValue(unsigned char n)
  131.     {
  132.      return (this->Sb[n]);
  133.     }
  134.  
  135.     unsigned char GetInvSBoxValue(unsigned char n)
  136.     {
  137.         return (this->InvSb[n]);
  138.     }
  139.  
  140.    
  141.     private:        
  142.         ChangesTable(){}
  143.         ChangesTable(ChangesTable& root){}
  144.         ChangesTable& operator=(ChangesTable&){}
  145. };
  146.  
  147.  
  148.  
  149. class Operations
  150. {   private:
  151.  
  152.     public:
  153.         unsigned  SubWord( unsigned  w)
  154.             {  
  155.                 unsigned  n,n1;
  156.                 unsigned  w2,w1;
  157.                 n=(w & 0xff000000)>>24;
  158.                 w1=ChangesTable.Instance().GetSBoxValue(n);
  159.                 n=(w & 0x00ff0000)>>16;
  160.                 w1=w1<<8;
  161.                 w1=w1|ChangesTable.Instance().GetSBoxValue(n);
  162.                 n=(w & 0x0000ff00)>>8;
  163.                 w1=w1<<8;
  164.                 w1=w1|ChangesTable.Instance().GetSBoxValue(n);
  165.                 n=(w & 0x000000ff);
  166.                 w1=w1<<8;
  167.                 w1=w1|ChangesTable.Instance().GetSBoxValue(n);
  168.                 return (w1);
  169.             }
  170.  
  171.         unsigned  MulX(unsigned char n)
  172.             {
  173.                 unsigned char n1,res=0;
  174.                n1=n & 0x80;
  175.                if (n1) {  res=(n<<1)^ 0x1b;}
  176.                else res=n<<1;
  177.                return res;
  178.             }
  179.  
  180.         unsigned Mul(unsigned x1, unsigned x2)
  181.             {  
  182.                 unsigned char stepeni_x [8],res,x3,i,mask=0x1;
  183.                 stepeni_x[0]=x1;
  184.                 res=0;
  185.                 for (i = 1; i < 8; i++)
  186.                 {
  187.                     stepeni_x[i]=MulX(stepeni_x[i-1]);
  188.                 }
  189.                 for (i = 0; i<8; i++)
  190.                 {  
  191.                     x3=(x2 & mask);
  192.                     if (x3!=0)
  193.                         {
  194.                             res=(res^stepeni_x[i]);
  195.                         }
  196.                     else {};
  197.                     mask=mask<<1;
  198.                     x3=0;
  199.                 }
  200.             return res;
  201.             }
  202.  
  203.          unsigned Rcon(int n)
  204.             {    
  205.                 unsigned  n1=0x1;
  206.                 unsigned w;
  207.                 int i;
  208.                 for (i = 1; i < n; i++)
  209.                 {
  210.                      n1=MulX(n1);
  211.                 }
  212.                 n1=(n1 << 24);
  213.                 return n1;
  214.             }
  215.  
  216.         unsigned RotWord(unsigned w)
  217.             {
  218.                 unsigned  n;
  219.                 unsigned w1;
  220.                 n=w >> 24;
  221.                 w1=w<<8;
  222.                 w1=w1 | n;
  223.                 return w1;
  224.             }
  225.  
  226. };
  227.  
  228. class AbstractSymmetricAlgorythm
  229. {
  230.     virtual void Encrypt() = 0;
  231.     virtual void Decrypt() = 0;
  232. };
  233.  
  234.  
  235. class AES: public AbstractSymmetricAlgorythm
  236. {   private:
  237.         unsigned char state [16];
  238.         unsigned  keys [44];
  239.         unsigned  key[4];
  240.         unsigned char key1 [16];
  241.         t_matr statematr;
  242.         Operations operations;
  243.         Matrix m;
  244.         column onecol;
  245.  
  246.     void Printstate()
  247.     {  
  248.         for (int i = 0; i < 16; i++)  printf(" %x",state[i]);
  249.         printf("\n");
  250.         printf("\n");
  251.     }
  252.  
  253.  
  254.     void SubBytes()
  255.     {
  256.         for ( int i = 0; i < 16; i++ )
  257.             this->state[i]=ChangesTable.Instance().GetSBoxValue(state[i]);
  258.     }
  259.  
  260.     void InvSubBytes()
  261.     {
  262.         for (int i = 0; i < 16; i++ )
  263.         {
  264.             state[i]=ChangesTable.Instance().GetInvSBoxValue(state[i]);
  265.         }
  266.     }
  267.  
  268.     void StateToMatr()
  269.     {
  270.         int i,j;
  271.         bool flag=true;
  272.         for (i = 0; i < 4; i++)
  273.         {
  274.             for (j = 0; j < 4; j++) this->statematr[j][i]=this->state[i*4+j];
  275.  
  276.         }
  277.     }
  278.  
  279.     void MatrToState()
  280.     {
  281.         for (int i = 0; i < 4; i++)
  282.         {
  283.             for (int j = 0; j < 4; j++) this->state[i*4+j]=this->statematr[j][i];
  284.         }
  285.     }
  286.  
  287.     void MulOneColumn(int num)
  288.     {  
  289.         column t,t1;
  290.         for (int i = 0; i < 4; i++)  t[i]=this->statematr[i][num];
  291.         for (int i = 0; i < 4; i++)
  292.         {
  293.             t1[i]=0;
  294.             for (int j = 0; j < 4; j++)
  295.             {
  296.                 t1[i]=t1[i]^(operations.Mul(t[j],mixmatr[i][j]));
  297.             }
  298.        }
  299.        for (int i = 0; i < 4; i++)  this->onecol[i]=t1[i];
  300.     }
  301.  
  302.  
  303.     void InvMulOneColumn(int num)
  304.     {  
  305.         column t,t1;
  306.         for (int i = 0; i < 4; i++)  t[i]=statematr[i][num];
  307.         for (int i = 0; i < 4; i++)
  308.         {
  309.             t1[i]=0;
  310.             for (int j = 0; j < 4; j++) t1[i]=t1[i]^(operations.Mul(t[j],invmixmatr[i][j]));
  311.        }
  312.        for (int i = 0; i < 4; i++) {
  313.            this->onecol[i]=t1[i];
  314.        }
  315.     }
  316.  
  317.     void MixColumns ()
  318.     {  
  319.         for (int i=0; i < 4; i++)
  320.         {
  321.           MulOneColumn(i);
  322.           for (int j = 0; j < 4; j++) {
  323.               this->statematr[j][i]=this->onecol[j];
  324.               this->onecol[j]=0;
  325.           }
  326.       }
  327.     }
  328.  
  329.     void InvMixColumns ()
  330.     {  
  331.         for (int i=0; i < 4; i++)
  332.         {
  333.           InvMulOneColumn(i);
  334.           for (int j = 0; j < 4; j++)
  335.           {
  336.               this->statematr[j][i]=this->onecol[j];
  337.               this->onecol[j]=0;
  338.           }
  339.       }
  340.     }
  341.  
  342.     void KeyExpansion()
  343.     {
  344.         unsigned  tmp,w1,r;
  345.         int i,j;
  346.         i=4;
  347.          while (i<44)
  348.          {
  349.             tmp=this->keys[i-1];
  350.             if ( (i%4)==0)
  351.             {
  352.                w1=operations.RotWord(tmp);
  353.                w1=operations.SubWord(w1);
  354.                tmp=w1 ^ operations.Rcon(i/4);
  355.             }
  356.             this->keys[i]=this->keys[i-4] ^ tmp;
  357.             i++;
  358.           }
  359.      
  360.         }
  361.  
  362.  
  363.     void AddRoundKey(int n)
  364.     {  
  365.        unsigned char  w,st1;
  366.        for (int i = 0; i < 4; i++)
  367.        {
  368.             w=(this->keys[n*4+i] >> 24) ;
  369.             this->key1[i*4]=w;
  370.             w=( (this->keys[n*4+i] & 0xFF0000)>>16);
  371.             this->key1[i*4+1]=w;
  372.             w=( (this->keys[n*4+i] )&0xFF00)>>8;
  373.             this->key1[i*4+2]=w;
  374.             w= this->keys[n*4+i]&0xFF;
  375.             this->key1[i*4+3]=w;
  376.             w=0x0;
  377.        }
  378.        for (int i = 0; i < 16; i++) this->state[i]=this->state[i] ^ this->key1[i];
  379.     }
  380.  
  381.  
  382.     public:
  383.  
  384.         void Encrypt()
  385.         {  
  386.             bool flag;
  387.             int k;
  388.             printf("Input text  \n");
  389.             for ( int i = 0; i < 16; i++) scanf("%x",&this->state[i]);
  390.             printf("Input key  \n");
  391.             for ( int i = 0; i < 4; i++) scanf("%x",&this->keys[i]);
  392.             KeyExpansion();
  393.             AddRoundKey(0);
  394.             printf("\n") ;
  395.             for (int i =1 ;i < 10; i++)
  396.             {
  397.                 printf("Round %i",i);
  398.                 printf("\n");
  399.                 SubBytes();
  400.                 StateToMatr();
  401.                 m.Initialize(this->statematr);
  402.                 m.ShiftRows();
  403.                 for ( int i = 0; i < 4; i++)
  404.                 {
  405.                     for ( int j = 0; j < 4; j++)
  406.                     {
  407.                         this->statematr[i][j]=m.m[i][j];
  408.                     }
  409.                 }
  410.                 MixColumns();
  411.                 MatrToState ();
  412.                 AddRoundKey(i);
  413.                 printf("\n");
  414.             }
  415.             SubBytes();
  416.             StateToMatr();
  417.             m.Initialize(this->statematr);
  418.             m.ShiftRows();
  419.             for ( int i = 0; i < 4; i++)
  420.             {
  421.                 for ( int j = 0; j < 4; j++)
  422.                 {
  423.                     this->statematr[i][j]=m.m[i][j];
  424.                 }
  425.             }
  426.             MatrToState();
  427.             AddRoundKey(10);
  428.             printf("\n");
  429.             printf("   Final State  \n");
  430.             Printstate();
  431.             scanf("%i",&k);
  432.         }
  433.  
  434.  
  435.         void Decrypt()
  436.         {
  437.             bool flag;
  438.             int k;
  439.             printf("Input coded text  \n");
  440.             for ( int i = 0; i < 16; i++) scanf("%x",&this->state[i]);
  441.             printf("Input key  \n");
  442.             for ( int i = 0; i < 4; i++) scanf("%x",&this->keys[i]);
  443.             KeyExpansion();
  444.             AddRoundKey(10);
  445.             Printstate();
  446.             printf("\n");
  447.             StateToMatr();
  448.             for ( int i=9; i > 0; i--)
  449.             {
  450.                 printf("\n");
  451.                 printf("Round %i",i);
  452.                 m.Initialize(this->statematr);
  453.                 m.InvShiftRows();
  454.                 for ( int i = 0; i < 4; i++)
  455.                 {
  456.                     for ( int j = 0; j < 4; j++)
  457.                     {
  458.                         this->statematr[i][j]=m.m[i][j];
  459.                     }
  460.                 }
  461.                 MatrToState();
  462.                 InvSubBytes();
  463.                 AddRoundKey(i);
  464.                 printf("\n");
  465.                 Printstate();
  466.                 StateToMatr();
  467.                 InvMixColumns();
  468.              }
  469.             printf("\n");
  470.               m.Initialize(this->statematr);
  471.               m.InvShiftRows();
  472.               for ( int i = 0; i < 4; i++)
  473.               {
  474.                 for ( int j = 0; j < 4; j++)
  475.                     {
  476.                         this->statematr[i][j]=m.m[i][j];
  477.                     }
  478.               }
  479.               MatrToState();
  480.               InvSubBytes();
  481.               AddRoundKey(0);
  482.               printf("\n");
  483.               printf("   Final State  \n");
  484.               Printstate();
  485.               scanf("%i",&k);
  486.             }
  487. };
  488.  
  489. #endif
Add Comment
Please, Sign In to add comment