encrypt/decrypt strangeness

K.I.L.E.R

Retarded moron
Veteran
Code:
name[i] += 1;
Encrypts nicely.

Code:
name[i] -= 1;
Perfectly decrypts back to letters.
////////////////////////////////////////////////
Code:
name[i] *= 2;
encrypts nicely.

Code:
name[i] /= 2;
Decrypts into garbage and leaves a "WTF?" feeling in me.

Can someone "please explain?". Thanks.
 
"char" is an integer type and so division will cause truncation when you divide by something that isn't a factor.

BTW "encryption" is a strong term for what you are doing :)
 
Simon F said:
BTW "encryption" is a strong term for what you are doing :)

Yeah, he should at least add 13 to the Chars, and not just 1.
This encryption has been used so wildly it has to be secure :LOL:
 
The problem is you are overflowing on your multiplication. Quite likely, char is by default signed for you. This means that while it may appear that your multiplying is working problem, the reality is it isn't.

If the char value is 64 or greater, multiplying by 2 will cause an overflow when using signed chars. Since, upper case A has a value of 65, multipliying any letter will overflow.

If you were using unsigned chars, then it would work. But as Simon says:

"encryption" is a strong term for what you are doing
 
Code:
unsigned char name[6] = {0};
int i=0;

cout << "Enter your name: ";
cin.getline(name,6);

This part of my code is rough. The input function isn't working.

Gives me:

C:\Documents and Settings\Kruno\Desktop\en.cpp(17) : error C2664: 'class std::basic_istream<char,struct std::char_traits<char> > &__thiscall std::basic_istream<char,struct std::char_traits<char> >::getline(char *,int)' : cannot convert parameter 1 f

By that error, it's telling me it's rough in the first argument. Usigned char and char seem to be very different.
 
Colourless said:
The problem is you are overflowing on your multiplication.
OOPS! I got his order of operations back the front. :oops:

Anyway, Killer, you need to do operations that are work on a mathematical group. Since you seem keen to stay with bytes (which will be horribly insecure but who cares), you need things that are invertible over Z 256. Multiplication will work PROVIDED you use something that is not a factor of 256, however, you need to know the inverse of that multiplier.

For example, (if I've got this right), 3 and 171 are such a pair (i.e. 171 is "1/3" modulo 256).

"encrypt" would be
Code:
char a;
....
a = a * 3
and decrypt
Code:
a = a * 171
Still, this is all rather trivial and would not stand up to any sort of cryptanalysis. Strong cryptography is a deceptively very difficult subject.
 
Simon F, I'm confused to what you are saying. I asked my programming teacher today and he didn't understand your post either. Could you please dumb it down a little for me? Thanks. :)
 
K.I.L.E.R said:
Simon F, I'm confused to what you are saying. I asked my programming teacher today and he didn't understand your post either. Could you please dumb it down a little for me? Thanks. :)

It's simple modulo arithmetic - my apologies if I don't remember the correct terminology for the following but more info can be found here

When you are using computer 'integer' types (and we'll assume unsigned here for simplicity but it doesn't really matter much) be they char (usually 8bit), short (usually 16), int, long, or long long (64 bit), you are actually working in a finite set modulo N. (Arghh! I can't remember what the thing below a group is called)

For example, if we take numbers modulo 3, then there are 3 unique sets which we'll call 0', 1', and 2'. Any number X will belong to exactly one of those sets, S', such that (X - S) is divisible by 3.

We say that if (Y - K) is divisible by N, then Y is "congruent" to K modulo N

You can do maths (addition and multiplication) on these sets - the result just has to be taken modulo N. Many of the "normal" rules you expect, eg. (A + C) * B = A * B + C *B, work modulo N.
Since you are working with computer integers the results are automatically taken modulo 2^something; eg 256 for chars.

With multiplication, depending on N, you can sometimes find multiplicative inverses. If N is prime, all (except 0) have inverses but in general, not all are like that - in fact any number that has a common factor with N is not going to have a multiplicative inverse modulo N.

Anyway for my example, note that (3*171) = 513 and 513 is congruent to 1 modulo 256. Therefore if you do (A * 3) * 171 = A * ( 3 * 171) = A * 1 = A modulo 256, and you get back to where you started.
 
It's simple modulo arithmetic - my apologies if I don't remember the correct terminology for the following but more info can be found here

Ahh I've remembered what the correct mathematical term is. The integers modulo N form a Ring.
 
Thanks.
I think I understand what you're saying. More now than I have before anyway. :)
Some of your terms are confusing me.

Modulo = modulus?

congruent = modulus?
 
K.I.L.E.R said:
Thanks.
I think I understand what you're saying. More now than I have before anyway. :)
Some of your terms are confusing me.

Modulo = modulus?

congruent = modulus?

"BLAH Modulo N" means "take the result of BLAH and compute the 'remainder' when divided by N"

"X is congruent to Y" sort of means that numbers are 'equivalent', i.e. in these cases (X-Y) = 0 mod N
 
Back
Top