Problem decrypting [Solved]

Posts 1–15 of 17 · Page 1 of 2
Problem decrypting [Solved]
Hi all, i'm trying to do a simple crypter for strings. With the Crypt function i have no problem:
Code:
int lunghezza;               //As globals
double valori[20];           

char* AltCrypt(char* string)
{
	lunghezza = strlen(string);
	char* tempstring = new char[lunghezza + 1];

	for (int j=0; j<lunghezza; j++)
	{	 valori[j] = atof(&string[j]);
		
		tempstring[j] = string[j] + string[j+1];
		j++;
	}

	tempstring[lunghezza/2] = 0;
	return tempstring;
}
In the console i print the crypted string and it's all good. When i try to Decrypt it, the console doesn't print me anything. That's my function to Decrypt:

Code:
char* AltDecrypt(char* string)
{
	lunghezza = strlen(string);
	
	char* tempstring = new char[(lunghezza*2) + 1];

	for (int j=0; j<lunghezza; j++)
	{	
		tempstring[j] = valori[j];
		tempstring[j+1] = string[j] - valori[j];
		
	}

	tempstring[lunghezza*2] = 0;
	return tempstring;
}
And finally this is how i call them in the main:

Code:
        cout<<"Alt-crypting: Hi man!"<<endl;
	char* done = AltCrypt("Hi man!");
	cout<<done;
	cout<<"\n\nAlt Decrypting: "<<AltDecrypt(done);
That's the result:

I tryed to follow the debug step by step with the breakpoints but when the compiler arrives at the Decrypt function it stops and asks me for a file(ftol2.asm). I tryed to google it but all says that it's not a problem, u can easily avoid it by put the breakpoint after that instruction. So, can anyone help me? ty in advance
Re-read over those two functions and try doing them in your head with say 2 numbers, they don't make sense .-.
Quote Originally Posted by Hell_Demon View Post
Re-read over those two functions and try doing them in your head with say 2 numbers, they don't make sense .-.
for me it has sense .-. i only add the j element with the next one so i can have a crypted string with half size of the original. maybe i have to explain the whole code :\ so u all can understand better
Quote Originally Posted by Sixx93 View Post
for me it has sense .-. i only add the j element with the next one so i can have a crypted string with half size of the original. maybe i have to explain the whole code :\ so u all can understand better
But you change the number of the next thing so if you decrypt it, it uses another number.
Quote Originally Posted by Nico View Post
But you change the number of the next thing so if you decrypt it, it uses another number.
no, because the original one is stored in the array
Quote Originally Posted by Hell_Demon View Post
crypt has 2x j++
yes, because the j+1 element is added with the j's one, so i have to jump of 2 steps
Make an array of a few numbers and check the array values after each step, you'll probably figure out whats going wrong then..
Quote Originally Posted by Hell_Demon View Post
Make an array of a few numbers and check the array values after each step, you'll probably figure out whats going wrong then..
i worked on my 2 functions, i changed something that could be wrong, now i get a decrypted string, but it's not right, it isn't the original. these are the new ones:

Code:
int lunghezza;
int valori[20];

char* AltCrypt(char* string)
{
	lunghezza = strlen(string);
	char* tempstring = new char[lunghezza + 1];

	for (int j=0; j<lunghezza; j+=2)
	{	
		valori[j] = atoi(&string[j]);
		tempstring[j] = string[j] + string[j+1];
	}

	tempstring[lunghezza/2] = 0;
	return tempstring;
}

char* AltDecrypt(char* string)
{
	char* tempstring = new char[lunghezza+1];

	for (int j=0; j<lunghezza; j+=2)
	{	
		itoa(valori[j],&tempstring[j],10);
		tempstring[j+1] = string[j] - tempstring[j];
	
	}

	tempstring[lunghezza] = 0;
	return tempstring;
}
Quote Originally Posted by Sixx93 View Post
i worked on my 2 functions, i changed something that could be wrong, now i get a decrypted string, but it's not right, it isn't the original. these are the new ones:

Code:
int lunghezza;
int valori[20];

char* AltCrypt(char* string)
{
	lunghezza = strlen(string);
	char* tempstring = new char[lunghezza + 1];

	for (int j=0; j<lunghezza; j+=2)
	{	
		valori[j] = atoi(&string[j]);
		tempstring[j] = string[j] + string[j+1];
	}

	tempstring[lunghezza/2] = 0;
	return tempstring;
}

char* AltDecrypt(char* string)
{
	char* tempstring = new char[lunghezza+1];

	for (int j=0; j<lunghezza; j+=2)
	{	
		itoa(valori[j],&tempstring[j],10);
		tempstring[j+1] = string[j] - tempstring[j];
	
	}

	tempstring[lunghezza] = 0;
	return tempstring;
}
I suggest just listening to Wesley, he knows what he's talking about.
Many problems in your code.
First off
Code:
valori[j] = atoi(&string[j]);
This dosen't do anything at all. Try outputting it in your code and you'll get 0 everytime. Do you know atoi does? It changes a STRING to an integer and I dont even know what you're trying to do.

2nd issue
Code:
itoa(valori[j],&tempstring[j],10);
This piece of code is jsut as useless. itoa takes the value of the first parameter and stores it in a string. However, you're taking valori[j] and trying to store it in the address of the character

Perhaps if you could try to explain your encryption method we can help you
Quote Originally Posted by zhaoyun333 View Post
Many problems in your code.
First off
Code:
valori[j] = atoi(&string[j]);
This dosen't do anything at all. Try outputting it in your code and you'll get 0 everytime. Do you know atoi does? It changes a STRING to an integer and I dont even know what you're trying to do.

2nd issue
Code:
itoa(valori[j],&tempstring[j],10);
This piece of code is jsut as useless. itoa takes the value of the first parameter and stores it in a string. However, you're taking valori[j] and trying to store it in the address of the character

Perhaps if you could try to explain your encryption method we can help you
i used atoi to get the ascii code of each char but i see that this isn't the right method... anyway my idea is to convert all the chars in the string to the ascii code and create a new (crypted) string wich will have as first char the ascii value equal to the sum of the first and second char of the original; as second the sum of the 3rd and the 4th of the original, etc etc... i used itoa to try to convert the number to the char equivalent in ascii. hope is all clear.

@flameswor10 ty for your 2 functions, they inspired me for making my ones ( not this ones xD )
Code:
struct Command
{	public:
		char* string;
		 bool a_crypted;
		void Crypt();
		void Decrypt();

	private:
		int* RandomNum(int);
		int* numb;
		
};



int* Command::RandomNum(int times)
{	int lowest = 0, highest = 65;
	int* numbers = new int[times+1];
	for(int j=0; j<times; j++)
	{
    int random_integer; 
    int range=(highest-lowest)+1; 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
		numbers[j] = random_integer;
	}
	return numbers;
}

void Command::Crypt()
{	a_crypted = true;
	int length = strlen(string);
	char* tempstring = new char[length + 1];
	numb = new int[length+1];
	numb = RandomNum(length);

	for (int j=0; j<length; j++)
	{
		int KeyStep = numb[j] + j;
		tempstring[j] = string[j] + KeyStep;
		
	}

	tempstring[length] = 0;
	string = tempstring;
}
void Command::Decrypt()
{	
	int length = strlen(string);
	char* tempstring = new char[length + 1];
if(a_crypted == false)
{
	numb = new int[length+1];
	numb = RandomNum(length);
}
	for (int j=0; j<length; j++)
	{
		int KeyStep = numb[j] + j;
		tempstring[j] = string[j] - KeyStep;
		
	}

	tempstring[length] = 0;
	string = tempstring;
}
for a bug of your (again xD) RandomNum function, it works very well i mean, that function works, but it generate every time the same random numbers

After i did this encryption method i thought i wanted to write a function to crypt a string modifying the length of it, so here they are xD the only problem is that they don't work xD
You need to seed the random function:

Code:
#include <time.h>

int main(void){

srand(time(NULL));

/*
blahblah
*/

}
Quote Originally Posted by zhaoyun333 View Post
You need to seed the random function:

Code:
#include <time.h>

int main(void){

srand(time(NULL));

/*
blahblah
*/

}
no it works very well .-.
Posts 1–15 of 17 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us