Thread: decrypt/encrypt

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    41

    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. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote 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. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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. #5
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  6. #6
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote 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.
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  8. #8
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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[2];
        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?
    Last edited by maxorator; 10-17-2006 at 06:36 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  10. #10
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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?
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote 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.
    Hope is the first step on the road to disappointment.

  12. #12
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    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.
    Last edited by maxorator; 10-18-2006 at 10:20 AM.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  13. #13
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  14. #14
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    Code:
    int main(){
        char buffer[2];
        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. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    41
    Quote 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 subscribe to a feed