please help, pointer problem

This is a discussion on please help, pointer problem within the C++ Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <string> #include <ctype.h> #include <iomanip> #include <iostream> #include <fstream> #define maxNUM 4000 ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    please help, pointer problem

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <string>
    #include <ctype.h>
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #define maxNUM 4000
    #define size 5
    #pragma hdrstop
    using namespace std;
    
    //---------------------------------------------------------------------------
    //tables
    int PC1(int , int);             //Permutation PC-1
    int PC2(int , int);             //Permutation PC-2
    int IPTable(int, int);               //IP = Initial Permutation
    //methods
    char *ConvertCharTo16(int );    //Conversion of characters to hex
    void Padding(int,int, char *);           //step 1
    char **SplitIntoBloxOf16(int, char *);   //step 2
    char *Block2Binary(char *);              //step 3
    char *GenerateKPlus(char *);             //step 4 using table PC-1
    void SplitString(char *,char *, char *); //step 5
    void BitShifting(char **,char **);       //step 6
    char *GetK (char *);                     //step 7 using table PC-2
    
    char *IPerm(char *);                     //do the mixup of the original message
    int ESelectTable(int , int );
    int PTable(int, int);
    int IPRevTable(int , int );              //IP-1
    char *GenerateE (char *);
    char **BBlocks(char *, char *);		 //Generate the B-Blocks
    int SBox(int, int , int );               //S-Boxes
    int Power(int x,int n);
    char *SBOperation(char **);
    char *PPerm(char *);
    char *XOR(char *, char *);
    char *IPRevPerm(char *);
    int method = 0;
    
    int menu() {
    	int method;
        cout<<"Please make a selection?"<<endl;
    	cout<<"1. Interactive Mode"<<endl; 
        cout<<"2. Non-interactive Mode"<<endl; 
    	cout<<"3. Exit"<<endl;
    	cin >> method;
    	system("CLS");
    	return method;
    }
    
    
    int main()
    { 
       	
       
       
       char *key,*Mes,*Message, *Blk;
       char *C[16],*D[16],*L[16],*R[16];
       
       char *IP;
       char **B; // the B Blocks
       Blk = (char *) malloc (1200);
       IP = (char *) malloc (1200);
       key = (char *) malloc (32);
       key = (char *) malloc (32);
       
       while (method != 3) {
    			method = menu();
    			switch (method) {
    					case 1: 
                             
       
       
       key = "133457799bbcdff1";    //encrypt-decrypt key
       Mes = "awdasdasdasdasdasd asdasdasdasdasdasdadasd xcfxdfsdfsdfsdfsdf";    //the message
    
       
       //cout<<"Enter the key :";
       //cin>>key;
       
       
       //cout<<"Enter the message to be encrypted: ";
       //cin>>Mes;
       
    
       //key = "133457799bbcdff1";    //encrypt-decrypt key
       //Mes = "awdasdasdasdasdasd asdasdasdasdasdasdadasd xcfxdfsdfsdfsdfsdf";    //the message
    
       Blk = Block2Binary(key);     //step 3
      Message = Block2Binary(Mes);
      cout<<"the binary string is =%s"<<Message;
      C[0] = (char *) malloc(strlen(Blk)+1);
      D[0] = (char *) malloc(strlen(Blk)+1);
      SplitString(GenerateKPlus(Blk), C[0], D[0]);
      BitShifting(C, D);
    
       printf("\n print K16 value %s",GetK(strcat(C[16],D[16])));
    
      //IP
      IP = IPerm(Message);
      L[0] = (char *) malloc(strlen(Blk)+1);
      R[0] = (char *) malloc(strlen(Blk)+1);
      SplitString(IP, L[0], R[0]);
      char *SBResult;
      char *f;
       int n;
       char *_getKStr;
      char *_getEoR;
      char *catOfRL;
      //encryption
      for(n = 1; n<=16; n++){ //16 iterations`using f(x)
         L[n] = (char *) malloc(strlen(Blk)+1);
         L[n] = R[n-1];
         _getKStr = GetK(strcat(C[n],D[n]));;
         _getEoR =  GenerateE(R[n-1]);
         B = BBlocks(_getKStr,_getEoR);
         SBResult =SBOperation(B);
        f = PPerm(SBResult);
        R[n] = (char *) malloc(strlen(Blk)+1);
        R[n]= XOR(L[n-1],f);
      }
        catOfRL = (char *) malloc((strlen(L[16])+1)*2);
        strcpy(catOfRL,"");
        strcat(catOfRL,R[16]);
        strcat(catOfRL,L[16]);
        catOfRL = IPRevPerm(catOfRL);
        printf("\n\nthe new encrypted string in binary = %s",catOfRL);
    
      
      
      break;
      
      case 2:
      ofstream outfile;
      ifstream Inputfile; 
      string filename;
      char a;
      key = "133457799bbcdff1";    //encrypt-decrypt key
       
       cout<<"Please enter filename for input (ex C://plain.txt)"<<endl;
    	cin>> filename;
    	Inputfile.open(filename.c_str(), ios :: in);
    	
    	if ( !Inputfile ){
    		cout << "PROBLEM! " << endl;
            system("pause");
    		return 0;
    	}
    
    	while ( Inputfile ) { 
    		Inputfile>>a;
    		*Mes=a;
    	}
    
    	Inputfile.close();
      
      Blk = Block2Binary(key);     //step 3
      Message = Block2Binary(Mes);
      //cout<<"the binary string is =%s"<<Message;
      C[0] = (char *) malloc(strlen(Blk)+1);
      D[0] = (char *) malloc(strlen(Blk)+1);
      SplitString(GenerateKPlus(Blk), C[0], D[0]);
      BitShifting(C, D);
    
       //printf("\n print K16 value %s",GetK(strcat(C[16],D[16])));
    
      //IP
      IP = IPerm(Message);
      L[0] = (char *) malloc(strlen(Blk)+1);
      R[0] = (char *) malloc(strlen(Blk)+1);
      SplitString(IP, L[0], R[0]);
      
      for(n = 1; n<=16; n++){ //16 iterations`using f(x)
         L[n] = (char *) malloc(strlen(Blk)+1);
         L[n] = R[n-1];
         _getKStr = GetK(strcat(C[n],D[n]));;
         _getEoR =  GenerateE(R[n-1]);
         B = BBlocks(_getKStr,_getEoR);
         SBResult =SBOperation(B);
        f = PPerm(SBResult);
        R[n] = (char *) malloc(strlen(Blk)+1);
        R[n]= XOR(L[n-1],f);
      }
        catOfRL = (char *) malloc((strlen(L[16])+1)*2);
        strcpy(catOfRL,"");
        strcat(catOfRL,R[16]);
        strcat(catOfRL,L[16]);
        catOfRL = IPRevPerm(catOfRL);
        //printf("\n\nthe new encrypted string in binary = %s",catOfRL);
    
      //while (InFile >> Mes)
        //OutFile<<catOfRL;
    
      	cout<<"Please enter filename for output (ex C://plain.txt)"<<endl;
    					cin>> filename;
    					outfile.open(filename.c_str());
    					
    					if (outfile.is_open()){
    						outfile <<"Encrypted message: "<<catOfRL<<endl;			
    					}
    					
    					else
    					cout<<"could not write to file"<<endl;
    					
    					break;	                                                                            	
    
      
     // default:
    		//			cout<<"Bad Selection"<<endl;
      
    }}
    free (B);
      
      system("pause");
      return 0;
    }
    
    //Permutation PC-1
    int PC1(int x, int y){
       static int Permutation[8][7] ={
          {57, 49, 41, 33, 25, 17, 9},
          {1, 58, 50, 42, 34, 26, 18},
          {10, 2, 59, 51, 43, 35, 27},
          {19, 11, 3, 60, 52, 44, 36},
          {63, 55, 47, 39, 31, 23, 15},
          {7, 62, 54, 46, 38, 30, 22},
          {14, 6, 61, 53, 45, 37, 29},
          {21, 13, 5, 28, 20, 12, 4}
       };
       
       return (Permutation[x][y]);
    }
    
    int PC2(int x, int y){
       static int Permutation[8][6] ={
          {14, 17, 11, 24, 1, 5},
          {3, 28, 15, 6, 21, 10},
          {23, 19, 12, 4, 26, 8},
          {16, 7, 27, 20, 13, 2},
          {41, 52, 31, 37, 47, 55},
          {30, 40, 51, 45, 33, 48},
          {44, 49, 39, 56, 34, 53},
          {46, 42, 50, 36, 29, 32}
       };
       return (Permutation[x][y]);
    }
    
    //Convert int to base 16
    char *ConvertCharTo16(int val){
            char *_str;
            char *_zeroStr;
            _str = (char *) malloc (sizeof(char));
            _zeroStr = (char *) malloc (sizeof(char));
            itoa(val,_str,16); //convert the number to base 16
    
            if (strlen (_str) < 2){
                strcpy(_zeroStr,"0");
                strcat(_zeroStr,_str);
                return (_zeroStr);
            }
            else{
                free(_zeroStr);
                
                return (_str);
                }
    }
    
    //padding
    void Padding(int lowerlim,int Upperlim, char hexArr[]){
       int i;
       for (i = lowerlim; i <=Upperlim ;i++ )
          hexArr[i] =  '0';
       hexArr[Upperlim +1] = '\0';  //please end the string
    }
    
    //split the strings into blocks of 16
    char **SplitIntoBloxOf16(int intCnt,char hexArr[]){
      int i,j;
            int chkCnt =0;
            char **DaSplit;
            char *Temp;
            Temp = (char *)malloc(sizeof (char));
            DaSplit = (char **)malloc (sizeof(char*));
             intCnt = (intCnt ) /16;      //calculate the number of blocks
            intCnt--;
            for (i = 0; i <= intCnt; i++){
                chkCnt = 0;
                for (j =(i*16);j <= (i *16)+15;j++){
                    Temp[chkCnt] = hexArr[j];
                    if (chkCnt == 15) {
                       Temp[chkCnt+1] = '\0';
                       DaSplit = (char **)realloc (DaSplit,(size+2)*sizeof(char *));
                       DaSplit[i] = (char *) malloc (strlen(Temp)+1*sizeof(char));
                       strcpy (DaSplit[i],Temp);
                       chkCnt = 0;
                    }
                    else chkCnt ++;
                }
            }
      free(Temp);
      
      return (DaSplit);
    }
    
    //Convert blocks to binary
    char *Block2Binary(char SingleBloc[]){
      char *temp;
      char *endptr;
      char *string;
      int lnumber;
      int i;
      int lenOStr;
      char buildStr[4];
      char *_rtnStr;
       _rtnStr =(char *) malloc (sizeof(char));
       temp = (char *) malloc (sizeof(char));
       string = (char *) malloc (sizeof(char));
    
       strcpy(_rtnStr,"");
       strcpy(buildStr,"");
       while (*SingleBloc != '\0'){
          *temp = *SingleBloc;
           lnumber = strtol(temp, &endptr, 16);
           itoa(lnumber, string, 2);
            lenOStr = strlen(string);
           if (lenOStr < 4){
              //Build a zero str
              for (i = 0 ;i < (4 - lenOStr);i++ && buildStr[i] != '0')
                 buildStr[i] = '0';
              buildStr[i] = '\0';
              strcat(_rtnStr,buildStr);   //put the zeros before
             // strcat(string," ");
              strcat(_rtnStr,string);     //Now join the strings
              _rtnStr =(char *) realloc(_rtnStr,(strlen(_rtnStr)+1) *2);
           }
           else{
             strcat(_rtnStr,string);
             _rtnStr =(char *) realloc(_rtnStr,(strlen(_rtnStr)+1) *2);
           }
           //  strcat(_rtnStr, " ");
    
          *SingleBloc++;
       }
    
       free(temp); free(string); free(endptr);
       
       return(_rtnStr);
    }
    
    char *GenerateKPlus (char *BinBit){               //step4
       char *KPlusBitStr;
       int i,j;
       KPlusBitStr= (char *) malloc ((strlen(BinBit)+1)*2);
       int k = 0;
       for (i = 0; i< 8; i++)
          for (j = 0; j < 7; j++)
          {
             KPlusBitStr[k] = BinBit[PC1(i,j)-1];  //substract 1 rember 0-63 Charax
             k++;
          }
       return(KPlusBitStr);
    }
    
    void SplitString(char *OriginalStr,char *Sub_A, char *Sub_B){
       int LenStr,midLen;
       int i; int j= 0;
       LenStr = strlen(OriginalStr);
       midLen = (LenStr /2) - 1;
    
       for (i = 0; i<= midLen; i++)
          Sub_A[i] = OriginalStr[i];
          Sub_A[i] = '\0';
    
       for (i = midLen+1; i<= LenStr-1; i++){
          Sub_B[j] = OriginalStr[i];
          j++;
       }
       Sub_B[j] = '\0';
    }
    
    void BitShifting(char **CPart, char **DPart){
       int i,j,nS;
       for (i = 1;i <= 16; i++){  //Repeat the operation 16 times
          CPart[i] = (char *) malloc(50);
          DPart[i] = (char *) malloc(50);
          if ((i ==1)||(i == 2)||(i ==9)||(i== 16))//find out the #-of shifts
              nS = 1;
          else nS = 2;
    
          if (nS == 1){
             for(j = 0; j <= 27;j++){   //ie the 56bits/2 - 1
                if (j < 27){
                   CPart[i][j] = CPart[i-1][j+1];
                   DPart[i][j] = DPart[i-1][j+1];
                }
                else{
                   CPart[i][j] = CPart[i-1][0];
                   DPart[i][j] = DPart[i-1][0];
                }
             }
           }
           else{
              for(j = 0; j <= 25;j++){   // 2 ........s for all other cases
                    CPart[i][j] = CPart[i-1][j+2];
                    DPart[i][j] = DPart[i-1][j+2];
              }
              CPart[i][26] = CPart[i-1][0];
              DPart[i][26] = DPart[i-1][0];
              CPart[i][27] = CPart[i-1][1];
              DPart[i][27] = DPart[i-1][1];
    
           }
          // CPart[i][28] = '\0';
          // DPart[i][28] = '\0';
          }
    }
    
    char *GetK (char *BinBit){               //step 7
       char *KPlusBitStr;
       int i,j;
       KPlusBitStr= (char *) malloc ((strlen(BinBit)+1)*2);
       int k = 0;
       for (i = 0; i< 8; i++)
          for (j = 0; j < 6; j++)
          {
             KPlusBitStr[k] = BinBit[PC2(i,j)-1];  //substract 1 rember 0-47 Charax
             k++;
          }
       
       return(KPlusBitStr);
    }
    
    
    
    //====================================================================
    int IPTable(int x, int y){               //IP = Initial Permutation
       static int IPTbl[8][8]= {
            {58, 50, 42, 34, 26, 18, 10, 2},
            {60, 52, 44, 36, 28, 20, 12, 4},
            {62, 54, 46, 38, 30, 22, 14, 6},
            {64, 56, 48, 40, 32, 24, 16, 8},
            {57, 49, 41, 33, 25, 17, 9 , 1},
            {59, 51, 43, 35, 27, 19, 11, 3},
            {61, 53, 45, 37, 29, 21, 13, 5},
            {63, 55, 47, 39, 31, 23, 15, 7}
       };
       
       return (IPTbl[x][y]);
    }
    
    int IPRevTable(int x, int y){               //IP-1
       static int IPRTbl[8][8]= {
            {40, 8, 48, 16, 56, 24, 64, 32},
            {39, 7, 47, 15, 55, 23, 63, 31},
            {38, 6, 46, 14, 54, 22, 62, 30},
            {37, 5, 45, 13, 53, 21, 61, 29},
            {36, 4, 44, 12, 52, 20, 60, 28},
            {35, 3, 43, 11, 51, 19, 59, 27},
            {34, 2, 42, 10, 50, 18, 58, 26},
            {33, 1, 41, 9, 49, 17, 57, 25}
       };
       
       return (IPRTbl[x][y]);
    }
    
    int ESelectTable(int x, int y){    //E Selection Table
       static int ETbl[8][6] ={
          {32, 1, 2, 3, 4, 5},
          {4, 5, 6, 7, 8, 9},
          {8, 9, 10, 11, 12, 13},
          {12, 13, 14, 15, 16, 17},
          {16, 17, 18, 19, 20, 21},
          {20, 21, 22, 23, 24, 25},
          {24, 25, 26, 27, 28, 29},
          {28, 29, 30, 31, 32, 1}
       };
       
       return (ETbl[x][y]);
    }
    
    int PTable(int x, int y){
      static int PTbl[8][4] ={
          {16, 7, 20, 21},
          {29, 12, 28, 17},
          {1, 15, 23, 26},
          {5, 18, 31, 10},
          {2, 8, 24, 14},
          {32, 27, 3, 9},
          {19, 13, 30, 6},
          {22, 11, 4, 25}
       };
       
       return (PTbl[x][y]);
    }
    
    char *IPerm(char *Msg){  //do the mixup..ie Permutation of the original Message
    char *IPMsg;
       int i,j;
       IPMsg= (char *) malloc ((strlen(Msg)+1)*2);
       int k = 0;
       for (i = 0; i< 8; i++)
          for (j = 0; j < 8; j++)
          {
             IPMsg[k] = Msg[IPTable(i,j)-1];  //substract 1 rember 0-63 Charax
             k++;
          }
       
       return(IPMsg);
    }
    
    char *GenerateE (char *Estr){               //step4
       char *EBitStr;
       int i,j;
       EBitStr= (char *) malloc ((strlen(Estr)+1)*2);
       int k = 0;
       for (i = 0; i< 8; i++)
          for (j = 0; j < 6; j++)
          {
             EBitStr[k] = Estr[ESelectTable(i,j)-1];  //substract 1 rember 0-47 Charax
             k++;
          }
       
       return(EBitStr);
    }
    
    char **BBlocks(char *K, char *EoR){
        char *_blk;
        char **B;
        char *tempStr;
        int lenOStr; int i,k;
        char *temp1;
        char *temp2;
        int tVal1,tVal2;
    
        lenOStr = strlen(K);    //Len of this string is 48 anyways
        _blk = (char *) malloc ((strlen(K)+1) * 2);
        tempStr = (char *) malloc (sizeof(char));
        temp1 = (char *)malloc(2);
        temp2 = (char *)malloc(2);
        B = (char **) malloc (sizeof(char *));
        i = 0;k = 0;
        strcpy(_blk,"");
    
        while (i < lenOStr){
            *temp1 =K[i];
            *temp2 =EoR[i];
            tVal1 = atoi(temp1);
            tVal2 = atoi(temp2);
            tVal1 = tVal1 ^ tVal2;
            itoa(tVal1,tempStr,10);
            strcat(_blk,tempStr);
            strcpy(tempStr,"");
    
            if (strlen(_blk)== 6){
               B = (char **)realloc (B,(size+2)*sizeof(char *));
               B[k] = (char *) malloc ((strlen(_blk)+1)*2);
               strcpy(B[k], _blk);
               strcpy(_blk,"");
               k++;
            }
            i++;
        }
        free(temp1); free(temp2); free(_blk);
        
        return (B);
    }
    
    // SBoxes
    int SBox(int whichBox, int x, int y){
       int val;
       switch(whichBox){
          case 0:{    //S-Box-1
                    static int STbl0[4][16] ={
                    {14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
                    {0, 15, 7, 4, 14, 2, 13, 1, 10, 6, 12, 11, 9, 5, 3, 8},
                    {4, 1, 14, 8, 13, 6, 2, 11, 15, 12, 9, 7, 3, 10, 5, 0},
                    {15, 12, 8, 2, 4, 9, 1, 7, 5, 11, 3, 14, 10, 0, 6, 13}
                    };
                    val = STbl0[x][y];  break;
                  }
          case 1:{    //S-Box-2
                    static int STbl1[4][16] ={
                    {15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
                    {3, 13, 4, 7, 15, 2, 8, 14, 12, 0, 1, 10, 6, 9, 11, 5},
                    {0, 14, 7, 11, 10, 4, 13, 1, 5, 8, 12, 6, 9, 3, 2, 15},
                    {13, 8, 10, 1, 3, 15, 4, 2, 11, 6, 7, 12, 0, 5, 14, 9}
                    };
                    val = STbl1[x][y];  break;
                 }
          case 2:{  //S-Box-3
                    static int STbl2[4][16] ={
                    {10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
                    {13, 7, 0, 9, 3, 4, 6, 10, 2, 8, 5, 14, 12, 11, 15, 1},
                    {13, 6, 4, 9, 8, 15, 3, 0, 11, 1, 2, 12, 5, 10, 14, 7},
                    {1, 10, 13, 0, 6, 9, 8, 7, 4, 15, 14, 3, 11, 5, 2, 12}
                    };
                    val = STbl2[x][y];    break;
                 }
          case 3:{  //S-Box-4
                    static int STbl3[4][16] ={
                    {7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
                    {13, 8, 11, 5, 6, 15, 0, 3, 4, 7, 2, 12, 1, 10, 14, 9},
                    {10, 6, 9, 0, 12, 11, 7, 13, 15, 1, 3, 14, 5, 2, 8, 4},
                    {3, 15, 0, 6, 10, 1, 13, 8, 9, 4, 5, 11, 12, 7, 2, 14}
                    };
                    val = STbl3[x][y];    break;
                 }
          case 4:{  //S-Box-5
                    static int STbl4[4][16] ={
                    {2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
                    {14, 11, 2, 12, 4, 7, 13, 1, 5, 0, 15, 10, 3, 9, 8, 6},
                    {4, 2, 1, 11, 10, 13, 7, 8, 15, 9, 12, 5, 6, 3, 0, 14},
                    {11, 8, 12, 7, 1, 14, 2, 13, 6, 15, 0, 9, 10, 4, 5, 3}
                    };
                    val = STbl4[x][y];     break;
                 }
          case 5:{  //S-Box-6
                    static int STbl5[4][16] ={
                    {12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
                    {10, 15, 4, 2, 7, 12, 9, 5, 6, 1, 13, 14, 0, 11, 3, 8},
                    {9, 14, 15, 5, 2, 8, 12, 3, 7, 0, 4, 10, 1, 13, 11, 6},
                    {4, 3, 2, 12, 9, 5, 15, 10, 11, 14, 1, 7, 6, 0, 8, 13}
                    };
                    val = STbl5[x][y];     break;
                }
          case 6:{   //S-Box-7
                    static int STbl6[4][16] ={
                    {4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
                    {13, 0, 11, 7, 4, 9, 1, 10, 14, 3, 5, 12, 2, 15, 8, 6},
                    {1, 4, 11, 13, 12, 3, 7, 14, 10, 15, 6, 8, 0, 5, 9, 2},
                    {6, 11, 13, 8, 1, 4, 10, 7, 9, 5, 0, 15, 14, 2, 3, 12}
                    };
                    val = STbl6[x][y];     break;
                 }
          case 7:{   //S-Box-8
                    static int STbl7[4][16] ={
                    {13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7},
                    {1, 15, 13, 8, 10, 3, 7, 4, 12, 5, 6, 11, 0, 14, 9, 2},
                    {7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13, 15, 3, 5, 8},
                    {2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11}
                    };
                    val = STbl7[x][y];     break;
                    }
                 }
       
       return (val);
    }
    
    //Useful function to compute the Power of 2...I don't know,Think it's useful
    //if higher powers are going to be used someday...think future!!
    int Power(int x,int n){
        int thePower;
        if (n < 1)
             thePower = 1;
        else
             thePower = Power(x,n-1)*x;
        
        return(thePower);
    }
    
    char *SBOperation(char **Blox){
       int i,j,k;
       int xlookup;
       int ylookup;
       char *tempStr1,*tempStr2;
      int SbxStrLen;
       char *_RtnString;
      char *SbXStr;
       char *aCh[2];
       char *bCh[4];
       char RightZeroPad[3];
    
      tempStr1 = (char *) malloc (8);
       tempStr2 = (char *) malloc (8);
       SbXStr = (char *) malloc ((strlen(Blox[0])+3)*2);
       //_RtnString = (char *)malloc((strlen(Blox[0])+1)*4);
       _RtnString = (char *)malloc(500);
       strcpy(_RtnString, "");
       strcpy(tempStr1,"");
       strcpy(tempStr2,"");
       strcpy(SbXStr,"");
       strcpy(RightZeroPad,"");
      // for (i = 0; i <sizeof(Blox)/sizeof(Blox[0]); i++){   //I know it's 8 elements
        for (i = 0; i < 8; i++){   //I know it's 8 elements
    
          tempStr1[0] = Blox[i][0];    //get the 1st bit of the block
          tempStr1[1] = Blox[i][5];    //get the last bit of the block
          tempStr1[2] = '\0';
          k = 0;
          for (j = 1; j < 5; j++){
               tempStr2[k] = Blox[i][j]; //get the mid 4 bits of a string
               k++;
          }
          tempStr2[k] = '\0';
    
          //now convert the string2 from binary to base 10 and lookup S-Box
          for (j = 0; j <2; j++){
             aCh[j] = (char *) malloc (sizeof(char));
          }
          *aCh[0] = tempStr1[0];
          *aCh[1] = tempStr1[1];
          xlookup = (atoi(aCh[0]) *2) + (atoi(aCh[1]) *1);
          for (j = 0;j < 4; j++){
             bCh[j] = (char *) malloc (sizeof(char));
             *bCh[j]= tempStr2[j];
          }
    
          k = 0;
          ylookup = 0;
          for (j = 3;j>=0;j--){
             ylookup += (atoi(bCh[j]) *Power(2,k));
             k++;
          }
    
          itoa(SBox(i,xlookup,ylookup),SbXStr,2);  //convert to base 2
          SbxStrLen = strlen(SbXStr);
          if (SbxStrLen < 4){      //if the length < 4 pad the string
             for (k = 0; k <(4-SbxStrLen);k++)
                RightZeroPad[k] = '0';
             RightZeroPad[k] = '\0';
          }
          strcat(RightZeroPad, SbXStr);
    
          strcat(_RtnString,RightZeroPad);
          strcpy(SbXStr,"");
          strcpy(RightZeroPad,"");
       }
    
       free(tempStr1); free(tempStr2); free(SbXStr);
       
       return (_RtnString);
    }
    
    char *PPerm(char *Msg){  //I guess we can use the one of the other permutations
    char *PMsg;              // & control i and j..Food for thought
       int i,j;
       PMsg= (char *) malloc ((strlen(Msg)+1)*2);
       int k = 0;
       for (i = 0; i< 8; i++)
          for (j = 0; j < 4; j++)
          {
             PMsg[k] = Msg[PTable(i,j)-1];  //substract 1 rember 0-63 Charax
             k++;
          }
       
       return(PMsg);
    }
    
    char *XOR(char *strA, char *strB){  //function similar to BBLocks..BBlocks can
        char *_blk;                     //split to 2 functions XOR and then create
        char *tempStr;                  //BBlocks...Figure it out
        int lenOStr; int i;
        char *temp1;
        char *temp2;
        int tVal1,tVal2;
    
        lenOStr = strlen(strA);    //Len of this string is 48 anyways
        _blk = (char *) malloc ((strlen(strA)+1) * 2);
        tempStr = (char *) malloc (sizeof(char));
        temp1 = (char *)malloc(2);
        temp2 = (char *)malloc(2);
        i = 0;
        strcpy(_blk,"");
    
        while (i < lenOStr){
            *temp1 =strA[i];
            *temp2 =strB[i];
            tVal1 = atoi(temp1);
            tVal2 = atoi(temp2);
            tVal1 = tVal1 ^ tVal2;
            itoa(tVal1,tempStr,10);
            strcat(_blk,tempStr);
            strcpy(tempStr,"");
            i++;
        }
        free(temp1); free(temp2); free(tempStr);
        
        return (_blk);
    }
    
    char *IPRevPerm(char *Msg){  //Final Permutation on the R16L16
    char *IPRevMsg;
       int i,j;
       IPRevMsg= (char *) malloc ((strlen(Msg)+1)*2);
       int k = 0;
       for (i = 0; i< 8; i++)
          for (j = 0; j < 8; j++)
          {
             IPRevMsg[k] = Msg[IPRevTable(i,j)-1];  //substract 1 rember 0-63 Charax
             k++;
          }
       
       return(IPRevMsg);
    }
    hello, please help me on this, my programming skills is very noob..
    i was trying to alter the code so that user can key key and the Mes instead of assigning it. and then user can upload the file as the Mes input...


    as u can see in the code,

    Code:
    key = "133457799bbcdff1";    //encrypt-decrypt key
       Mes = "awdasdasdasdasdasd asdasdasdasdasdasdadasd xcfxdfsdfsdfsdfsdf";    //the message
    i want to do it like this:
    Code:
    cout<<"Enter the key :";
       cin>>key;
       
       
       cout<<"Enter the message to be encrypted: ";
       cin>>Mes;
    the program running but then it will stopped..

    i think it is the pointer problem...

    anyone please help me...

    thank you in advance...

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    I didn't see any memory allocated for "mes"
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2
    is it like this:

    Code:
    Mes = (char *) malloc (32);
    nothing much, still the program ended after printing the K16 value...

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    Who wrote that code?

    It's awful - where's the indentation, off on holiday somewhere?

    > for(n = 1; n<=16; n++)
    Given anArray[16], you need to understand what the valid subscripts of an array are.
    Hint: <= 16 ISN'T IT.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,655
    Looks like it came from here.

  6. #6
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Looks like it came from here.
    Tehe. True. I like how xxxoooxxx gave us this hundred-line program that encrypts data and proceeds to tell us that his "programming skills is very noob..". If you're using someone else's code, that's fine. But give a link to the original. Or at least say you're trying to edit someone else's code.

    i was trying to alter the code so that user can key key and the Mes instead of assigning it. and then user can upload the file as the Mes input...
    Code:
    cout<<"Please enter filename for input (ex C://plain.txt)"<<endl;
    	cin>> filename;
    	Inputfile.open(filename.c_str(), ios :: in);
    	
    	if ( !Inputfile ){
    		cout << "PROBLEM! " << endl;
            system("pause");
    		return 0;
    	}
    
    	while ( Inputfile ) { 
    		Inputfile>>a;
    		*Mes=a;
    	}
    
    	Inputfile.close();
    Uh... looks like it's already in the code.

    And as for typing in a key, cin will work just fine.
    Last edited by bernt; 05-06-2010 at 04:29 PM.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 09:53 PM
  2. Another pointer problem
    By mikahell in forum C++ Programming
    Replies: 21
    Last Post: 07-20-2006, 08:37 PM
  3. Pointer problem
    By mikahell in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2006, 11:21 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 08:48 PM
  5. pointer problem
    By DMaxJ in forum C Programming
    Replies: 4
    Last Post: 06-11-2003, 01:14 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21