Results 1 to 8 of 8
  1. #1
    zhaoyun333's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Posts
    396
    Reputation
    11
    Thanks
    1,125

    MPGH Decrypter [Challenge]

    To win you must decrypt this string: D3430373C3A3719191910343910301031303A393
    Post your code too!
    Winner gets a cookie.

    The implementation of the encryption to get this was:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void toBinary(int n,char* str,int d){
    	if(d<0)return;	
    	str[d]=n%2+'0';	
    	toBinary(n>>1,str,d-1);	
    }
    
    int toDecimal(char* str,int d){
    	if(d<=1)return str[0]-'0';
    	return ((str[0]-'0')<<(d-1))+toDecimal(str+1,d-1);
    }
    
    char* encrypt(char* str){
    
    	int i,j;
    	int len = (strlen(str))*8;
    	char* str2=new char[len+1];
    	
    	
    	for(i=0;i<(strlen(str))*8;i++){
    		str2[i]='0';
    	}
    	
    	for(i=0;i<strlen(str);i++){
    		int n = (int)str[i];
    		toBinary(n,str2+i*8,8);
    	}
             
            //edit
            str2[len]='\0';
    
    	char tstr[5];
    	memcpy(tstr,str2,4);
            //edit
            tstr[4]='\0';
    	
    
    	strcpy(str2,str2+4);
    	strcpy(str2+len-4,tstr);
    
    
    	char* encryptedStr=new char[len/4+1];
    	
    	char map[]="0123456789ABCDEF";
    	for(i=0;i<len/4;i++){
    		encryptedStr[i]=map[toDecimal(str2+4*i,4)];
    	}
    	encryptedStr[i]='\0';
    
    	
    	return encryptedStr;
    
    }
    
    
    int main(void){
    
    char str[256];
    gets(str);
    char* encrypterStr = encrypt(str);
    cout<<encrypterStr;
    
    
    }
    The most compact decimal to binary and vice versa functions you will ever see xD
    Last edited by zhaoyun333; 08-14-2011 at 08:53 AM.
    There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi

  2. #2
    Fovea's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    325
    Reputation
    101
    Thanks
    411
    My Mood
    Amused
    tstr is not null terminated. strcpy behavior will be undefined.
    str2 is not null terminated. strcpy behaviour will be undefined.
    Off by one error in toBinary, str2 will overflow by one as well as overwrite one "bit" of previous data.

  3. The Following User Says Thank You to Fovea For This Useful Post:

    Hell_Demon (08-14-2011)

  4. #3
    zhaoyun333's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Posts
    396
    Reputation
    11
    Thanks
    1,125
    Must have removed the null terminators while removing my debug stuff =3
    Added them back though
    toBinary will always write only to 8 characters
    There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi

  5. #4
    Fovea's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    325
    Reputation
    101
    Thanks
    411
    My Mood
    Amused
    Nope, you still have an off by one error. C arrays are zero based. Your base case for your recursive function checks for less than zero. The index parameter passed to toBinary is eight. Indices eight to zero are written to, which amounts to nine characters written.

    You should check your code and save some face next time you present a challenge.

  6. #5
    ғᴜᴋᴏᴊʀ's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Posts
    1,557
    Reputation
    87
    Thanks
    141
    My Mood
    Inspired
    Quote Originally Posted by Fovea View Post
    Nope, you still have an off by one error. C arrays are zero based. Your base case for your recursive function checks for less than zero. The index parameter passed to toBinary is eight. Indices eight to zero are written to, which amounts to nine characters written.

    You should check your code and save some face next time you present a challenge.
    Nigga got told


    [IMG]https://i186.photobucke*****m/albums/x253/Rypleys/MNC/biohazard2.jpg[/IMG]

    MPGH in 5 words:

    Quote Originally Posted by ZEROProJect View Post
    1 in a million community

  7. #6
    zhaoyun333's Avatar
    Join Date
    Apr 2009
    Gender
    male
    Posts
    396
    Reputation
    11
    Thanks
    1,125
    Shit ok the string is : A68606E78746E323232206872206020626074737

    New code:
    Code:
    #include <iostream>
    
    using namespace std;
    
    void toBinary(int n,char* str,int d){
    	if(d<0)return;	
    	str[d-1]=n%2+'0';	
    	toBinary(n>>1,str,d-1);	
    }
    
    int toDecimal(char* str,int d){
    	if(d<=1)return str[0]-'0';
    	return ((str[0]-'0')<<(d-1))+toDecimal(str+1,d-1);
    }
    
    char* encrypt(char* str){
    
    	int i,j;
    	int len = (strlen(str))*8;
    	char* str2=new char[len+1];
    	
    	
    	for(i=0;i<(strlen(str))*8;i++){
    		str2[i]='0';
    	}
    	
    	for(i=0;i<strlen(str);i++){
    		int n = (int)str[i];
    		toBinary(n,str2+i*8,8);
    	}
    
    	str2[len]='\0';
    	char tstr[5];
    	memcpy(tstr,str2,4);
    	tstr[4]='\0';
    	
    	strcpy(str2,str2+4);
    	strcpy(str2+len-4,tstr);
    
    
    	char* encryptedStr=new char[len/4+1];
    	
    	char map[]="0123456789ABCDEF";
    	for(i=0;i<len/4;i++){
    		encryptedStr[i]=map[toDecimal(str2+4*i,4)];
    	}
    	encryptedStr[i]='\0';
    
    	
    	return encryptedStr;
    
    }
    
    
    int main(void){
    
    char str[256];
    gets(str);
    char* encrypterStr = encrypt(str);
    cout<<encrypterStr;
    
    
    }
    Now how about actually decrypting it Fovea
    There are five possible operations for any army. If you can fight, fight; if you cannot fight, defend; if you cannot defend, flee; if you cannot flee, surrender; if you cannot surrender, die." - Sima Yi

  8. #7
    258456's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    ghjghj
    Posts
    1,222
    Reputation
    18
    Thanks
    300
    My Mood
    Relaxed
    Quote Originally Posted by Fovea View Post
    Nope, you still have an off by one error. C arrays are zero based. Your base case for your recursive function checks for less than zero. The index parameter passed to toBinary is eight. Indices eight to zero are written to, which amounts to nine characters written.

    You should check your code and save some face next time you present a challenge.
    I have never seen this side of you before, lol.

  9. #8
    Fovea's Avatar
    Join Date
    Mar 2011
    Gender
    male
    Posts
    325
    Reputation
    101
    Thanks
    411
    My Mood
    Amused
    You're still off by one. Here's how to fix it.
    Code:
    void toBinary(int n, char* str, int d) {
      if (d == 0)
        return;
        
      str[d - 1] = n % 2 + '0';
      toBinary(n >> 1, str, d - 1); 
    }

Similar Threads

  1. Replies: 143
    Last Post: 09-15-2010, 03:14 PM
  2. MPGH Logistic Programming Challenge
    By scimmyboy in forum C++/C Programming
    Replies: 32
    Last Post: 08-12-2010, 09:22 AM
  3. [Mpgh] Coders Challenge !!!
    By crossmadnez in forum CrossFire Hack Coding / Programming / Source Code
    Replies: 36
    Last Post: 08-10-2010, 03:20 AM
  4. MPGH Stock Challenge
    By arunforce in forum News & Announcements
    Replies: 11
    Last Post: 12-09-2009, 04:25 PM
  5. hey this is a challenge for all u mpgh coders out there!!!
    By prox32 in forum WarRock - International Hacks
    Replies: 3
    Last Post: 05-17-2007, 11:18 AM