YAY! I made my first encryption algorithm :D

K.I.L.E.R

Retarded moron
Veteran
Code:
void main(void)
{
	char type[10], test;
	int counter, *enc;

	cin >> type;

	test = strlen(type);

	for (counter=0; counter < test; counter++)
	{
		enc += test;
		enc++;
	}

	cout << enc << endl;
}
 
Hmm, you appear to be writing to a random location in memory.

you have not set enc to point to a buffer before using it.

Also I assume that you are wanting to add your string lenght to each plain-text character, however you are adding it to your pointer.

CC
 
This is officially confusing.

Prone to buffer overflows... how so?

I will try to fix it up. Thanks guys.
 
You have allocated room for 10 characters in 'type', but the 'cin >> type' statement doesn't check that the input to cin is limited to 10 characters, so if you run the program and I write "MACOUMBAH2003!!" (15 characters) cin will happily write past the end of your array into memory areas normally used for other purposes. This is a so-called buffer overflow. What makes buffer overflows dangerous is that a determined attacker can not only write garbage into your memory and thus crash your program, but modify the return address of your function to point to code that is embedded in the string and that way take control of your computer. This may sound far-fetched, but once you have made a buffer overflow attack once, it is easy to modify your attack to exploit other buffer overflows as well; there are hundreds of known buffer overflows in various programs for which attacks do exist.

To protect your code from this particular buffer overflow, replace
Code:
cin >> type;
with
Code:
cin.getline(type, 10);
 
Thanks. Sometimes I'm pretty hopeless. :oops:
I also found out ( by myself :oops: ) that I am assigning an int to a char. :oops:
 
K.I.L.E.R said:
Thanks. Sometimes I'm pretty hopeless. :oops:
I also found out ( by myself :oops: ) that I am assigning an int to a char. :oops:
That's not necessarily a problem. Chars form a mathematical group and so you can add a "larger number" (it'll be truncated modulo 256 (or equiv)) and then subtract it off again and get back to where you started.

BTW I wouldn't call this 'encryption' - it'd be almost as secure to use ROT-13 :)
 
Simon F said:
K.I.L.E.R said:
Thanks. Sometimes I'm pretty hopeless. :oops:
I also found out ( by myself :oops: ) that I am assigning an int to a char. :oops:
That's not necessarily a problem. Chars form a mathematical group and so you can add a "larger number" (it'll be truncated modulo 256 (or equiv)) and then subtract it off again and get back to where you started.

BTW I wouldn't call this 'encryption' - it'd be almost as secure to use ROT-13 :)

ROT-13?

I'm actually very interested in being able to make encryption software. :)
You never know, I could be the RIAA's next [incompetent] CD encrypter. :LOL: ;)

There are so many things I want to make. Soo many. I could sink a ship with the amount of thought brainwaves coming out of me. Programming gets me rapped up and eccentric.
 
Look here for an explanation of the ROT-13 encryption algorithm. It's really simple; with some exercise you may even be able to encrypt/decrypt ROT-13 in your head at a reasonable speed.
 
The Amazon book I can't buy as I don't have any credit cards and converting money from Aus to US is a pain and I will end up paying more than double the suggested price. I'm not working either. :/

The second link is very enlightning. :)

Thanks
 
Now, my improved encryption/decryption algprithm. :)

This is a rough version. In future I will make a real algorithm. Right now I am only interested in text I can convert to gibberish and convert that gibberish back to into its original form.

Code:
void encrypt(char text[10], int code);
//int decrypt();

void main()
{
	int code=0, counter=0, option=0;
    char text[10], answer=' ';

	cout << "1. Encrypt" << endl;
	cout << "2. Decrypt" << endl;
	cout << "Will you decrypt or encrypt?";
	cin >> option;

	if(option == 1)
	{
	cout << "Enter some text: ";
	cin >> text;

	cout << "Enter code: ";
	cin >> code;

	encrypt(text, code);
	}

	if(option == 2)
	{
	}
}

void encrypt(char text[10], int code)
{
	int counter=0, textLength=0;
    char * enc;

	//text encode 
	enc = text;

	enc += code;

	cout << enc << endl;
}
/*
int decrypt()
{
	int code=0, counter=0, textLength=0;
    char text[10], * enc=0;

}
*/
 
I don't think you have got the hang of pointers yet.



In function encrypt
you are assigning enc to be a pointer to array of chars text.

You are then adding code to the pointer not to the data it is pointing to. To access the data use the * symbol.
So you should be doing
Code:
*enc=*enc+code
This will only operate on the character currently pointed at, so you need to do this for all the characteres in your array.

e.g.

Code:
enc=text;
for (counter=0;counter<10;counter++) 
{
    *enc=*enc+code;
    enc++;
}

It is worth thinking about the affect of encrypting the NULL character which is at the end of a string.


CC
 
Although it's worthwhile (and important too) to understand pointer manipulation, for applications like this, it's better to use std::string for string handling. You can avoid many buffer overflow problems. Of course, you'll need a "real" C++ compiler for this :)
 
K.I.L.E.R said:
The Amazon book I can't buy as I don't have any credit cards and converting money from Aus to US is a pain and I will end up paying more than double the suggested price. I'm not working either. :/

The second link is very enlightning. :)

Thanks
It'll probably go over your head (like 99% of us) but the "Handbook of Cryptograpy" is on the net somewhere and it is excellent
 
It is actually aimed at the layman rather than computer geeks so is quite an easy read.

The point I was making that you should stop the encryption before the null character as encrypting it would potentially cause problems.

CC
 
Captain Chickenpants said:
It is actually aimed at the layman rather than computer geeks so is quite an easy read.

The point I was making that you should stop the encryption before the null character as encrypting it would potentially cause problems.

CC

I don't think I have any NULL characters. I can't see one anyway. I will look harder. I need to learn to read code much better than I currently can if I am to succeed.
 
Back
Top