1. ## decrypt/encrypt

Code:
```    sum1 = ((num % 10) / 1);
rm1 = ((sum1 + 7) % 10);
sum2 = ((num % 100) / 10);
rm2 = ((sum2 + 7) % 10);
sum3 = ((num % 1000) / 100);
rm3 = ((sum3 + 7) % 10);
sum4 = ((num % 10000) / 1000);
rm4 = ((sum4 + 7) % 10);
printf("%d %d %d %d\n", rm1, rm2, rm3, rm4);```
I am at a loss here, how would i decrypt this back? 2. Work through it backwards. The code your teach gave you breaks down the number starting at the tens place and working its way up. You'll have to reconstruct the numbers in the reverse order. 3. Originally Posted by Kennedy
Work through it backwards. The code your teach gave you breaks down the number starting at the tens place and working its way up. You'll have to reconstruct the numbers in the reverse order.
I think I tried that, a few differnet ways. I still decrypts, just in a reverse order 4. This requires you to THINK about the problem.

All you are doing is adding 7 to each digit. Undo what you have done. Reassemble the numbers in the correct order and your through. The only problem I see with it, however, is that you'd never be able to decern between 0-6 and 10-16. 5. In certain cases, this encryption can't be decrypted.
If one number is 3 and you add 7 to it, you will end up with 0, you cant decrypt 0, because you don't know where they were and how many of them were there.

Of course, only if it is stored as an integer. 6. Originally Posted by maxorator
In certain cases, this encryption can't be decrypted.
If one number is 3 and you add 7 to it, you will end up with 0, you cant decrypt 0, because you don't know where they were and how many of them were there.

Of course, only if it is stored as an integer.
Huh? I think you're incorrect. 7. Originally Posted by Rashakil Fol
Huh? I think you're incorrect.
That's par for the course with regards to maxorator. He's pretty much always wrong.

Quzah. 8. Am I?
Let's say 533.
This will be made into 002, which is 2 as an integer, now when you decrypt it, it will be 5.

Next time, you shouldn't miss reading such lines:
Of course, only if it is stored as an integer.
This should show you exactly what I mean:
Code:
```#include <stdio.h>

int encrypt(int *data){
int res=0,x=*data,num;
while(x>0){
res*=10;
num=x%10;
res+=(num+7)%10;
x/=10;
}
*data=res;
return 0;
}

int decrypt(int *data){
int res=0,x=*data,num;
while(x>0){
res*=10;
num=x%10;
res+=(num+3)%10;
x/=10;
}
*data=res;
return 0;
}

int main(){
char buffer;
int num=5631;
printf("Number: %i\n",num);
encrypt(&num);
printf("Encrypted: %i\n",num);
decrypt(&num);
printf("Decrypted: %i\n",num);
num=646579856;
printf("Number: %i\n",num);
encrypt(&num);
printf("Encrypted: %i\n",num);
decrypt(&num);
printf("Decrypted: %i\n",num);
fgets(buffer,2,stdin);
return 0;
}```
Got it? 9. Next time you shouldn't make arbitrary rules. No one said it was going to be stored in a single integer. Look at the first example and you can see it's been split into digits. Furthermore, you can decode it just fine if you store the length of the number. Hey, if you get to make up your own rules to prove your point, I get to do the same.
Code:
```int enc(int *data){
int res=0,x=*data,num;
int len = 0;
while( x ){
++len;
res*=10;
num=x%10;
res+=(num+7)%10;
x/=10;
}
*data=res;
return len;
}

int dec(int *data, int len){
int res=0,x=*data,num;
while( len-- ){
res*=10;
num=x%10;
res+=(num+3)%10;
x/=10;
}
*data=res;
return 0;
}```
Did I ever mention you write ugly code, which is ... bad? I can't figure out what on earth is going through your head when you write most of it. Anyway, that works just fine. Store the length. See? My made up rules say your made up rules are wrong.

Quzah. 10. Did I ever mention you write ugly code, which is ... bad? I can't figure out what on earth is going through your head when you write most of it.
Did I ever mention it about you?
What's wrong with my code? You want a newline between functions? Fine, you got it. What else? 11. Originally Posted by maxorator
Did I ever mention it about you?
That was a copy-paste of your code, with a minor edit to get the point across. Here.
Code:
```int encrypt(int *data){
...snip...
return 0;
}```
Here's just one example. I didn't feel like going through all of your posts. Your function returns an int, but there's no real point in doing so. You never use the return value. You never return anything other than zero. So why return anything at all? This is just one of may things wierd I see you do all the time. It's pointless, plus you seem to have an aversion to white-space. Which makes your code harder to read.

One should try to make their code clear and readable above all else, in my opinion, unless you're intentionally trying to confuse people, such as with handing out homework answers. But that's just my opinion.

Quzah. 12. My functions always have "return", I think this is a good thing to do.
I also play around with assembly, where "ret" is necessary. I think it does no harm to anyone.

I've never liked white-spaces.
With white-spaces the code seems to spattered all around the screen, which I don't like.
This is my opinion. 13. If your function has a return value, then users of your function might think that it indicates something and check it, when in fact it's always the same.

I've never liked white-spaces.
With white-spaces the code seems to spattered all around the screen, which I don't like.
This is my opinion.
Hmm, most of us hold the opposite opinion. But then, that's why they're opinions. 14. Code:
```int main(){
char buffer;
int num=5631;
printf("Number: %i\n",num);
encrypt(&num);
printf("Encrypted: %i\n",num);
decrypt(&num);
printf("Decrypted: %i\n",num);
num=646579856;
printf("Number: %i\n",num);
encrypt(&num);
printf("Encrypted: %i\n",num);
decrypt(&num);
printf("Decrypted: %i\n",num);
fgets(buffer,2,stdin);
return 0;
}```
Why? do you use a "return 0;" in a main() ? Where the hex is that returning to....?

i like the straight down code look though. 15. Originally Posted by quzah
Next time you shouldn't make arbitrary rules. No one said it was going to be stored in a single integer. Look at the first example and you can see it's been split into digits. Furthermore, you can decode it just fine if you store the length of the number. Hey, if you get to make up your own rules to prove your point, I get to do the same.
Code:
```int enc(int *data){
int res=0,x=*data,num;
int len = 0;
while( x ){
++len;
res*=10;
num=x%10;
res+=(num+7)%10;
x/=10;
}
*data=res;
return len;
}

int dec(int *data, int len){
int res=0,x=*data,num;
while( len-- ){
res*=10;
num=x%10;
res+=(num+3)%10;
x/=10;
}
*data=res;
return 0;
}```
Did I ever mention you write ugly code, which is ... bad? I can't figure out what on earth is going through your head when you write most of it. Anyway, that works just fine. Store the length. See? My made up rules say your made up rules are wrong.

Quzah.
Thanks, but we aren't there yet (next chapter). I did a program with arrays, which is 3 chapters ahead and he told me to change it. Popular pages Recent additions 