MPGH Decrypter [Challenge]

Posts 1–8 of 8 · Page 1 of 1
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
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.
Must have removed the null terminators while removing my debug stuff =3
Added them back though
toBinary will always write only to 8 characters
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.
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
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.
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); 
}
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
Posts 1–8 of 8 · Page 1 of 1

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us