Welcome Guest [Log In] [Register]
Welcome to Crypto. We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join our community, you'll be able to access member-only sections, and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.


Join our community!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Urrrgh! Help Plz; C programming Advice
Topic Started: Mar 7 2007, 01:36 AM (349 Views)
loki
Advanced Member
[ *  *  * ]
I am not grasping how to implement CBC whatsoever, I have tried and tried and just can't figure out where i am going wrong.

all that I am trying to do is implement an xor cipher with cbc, only for the learning purpose of it. But I am at my wits, I have zero idea what I am doing wrong.

Could someone have a quick glance at my code and kindly slap me in the face. Or just fix it?

I apologise, I should be doing this myself, but I need the help from higher experiance.
c(x) = 3x3 + x2 + x + 2; Find the inverse
Offline Profile Quote Post Goto Top
 
jdege
Member Avatar
Elite member
[ *  *  *  *  * ]
loki
Mar 7 2007, 01:36 AM
I am not grasping how to implement CBC whatsoever, I have tried and tried and just can't figure out where i am going wrong.

There's nothing wrong with your C.

Well, there's a lot wrong with your C - K&R bracing, screwy indents, use of obsolete modifiers like "register" and "inline", but none of that has anything to do with why your code isn't working.

Your problem is your use of the "last" variable in decrypt.

Remember your formulas:
Code:
 

C[0] = E(P[0]^IV)             P[0] = D(C[0])^IV
C[n] = E(P[n]^C[n-1])      P[n] = D(C[n])^C[n-1]


When you're encrypting, you have "last" storing the last output character, C[n-1].

When you're decrypting, you have "last" storing the last output character, P[n-1].

But in decrypt, you're supposed to XOR the previous ciphertext character, not the previous plaintext character.

In decrypt, "last" should be set to the "current_byte", after you've decrypted "current_byte" and before you do the next fgetc().
When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl.
Offline Profile Quote Post Goto Top
 
loki
Advanced Member
[ *  *  * ]
I removed static and inline, It was a mistake from an earlier project. Opps.
I prefer K&R bracing, I like it becuase its clean. Please do explain why thats not good.

I made a few adjustments and Its still not working. I find it very depressing, because to me the code is right. Clearly its not, otherwise I would giggling with self achievement.

I am really starting to think about making page on how to write a cipher for dummies by a dummy!

I appreciate the help, thanks.

I think I am going to have to printf every line until I see my error.

Included is what I have done.
c(x) = 3x3 + x2 + x + 2; Find the inverse
Offline Profile Quote Post Goto Top
 
jdege
Member Avatar
Elite member
[ *  *  *  *  * ]
loki
Mar 7 2007, 03:56 PM
I removed static and inline, It was a mistake from an earlier project. Opps.
I prefer K&R bracing, I like it becuase its clean. Please do explain why thats not good.

I made a few adjustments and Its still not working. I find it very depressing, because to me the code is right. Clearly its not, otherwise I would giggling with self achievement.

I am really starting to think about making page on how to write a cipher for dummies by a dummy!

I appreciate the help, thanks.

I think I am going to have to printf every line until I see my error.

Included is what I have done.

Static has it's place. It's good style to keep scope as tight as possible.

Register and inline were never more than hints to the optimizer, and modern optimizers don't need these kind of hints. So register and inline are no more than clutter.

As for K&R bracing, some prefer it, others don't. I like being able to see a vertical match between an opening and closing brace - makes it easier for me to identify the scope of a compound statement. It's the oldest, and most pointless, flame war in the history of the C language.

But enough of the style quibbles:

Code:
 

   unsigned int last;
   unsigned int output;
   unsigned int current_byte;
   int mode;
   int i;

  case decrypt:
       i = 0;
       current_byte = fgetc(plain_text);
       printf("C[%d] = '%02X'\n", i, current_byte);
       output = baldr_decrypt(current_byte,*user_key);
       printf("D(C[%d]) = '%02X'\n", i, output);
       output = output ^ iv;
       printf("P[%d] = '%02X'\n", i, output);
       fputc(output, cipher_text);

       last = current_byte;
       i += 1;
       printf("\n");

       /* while we are still reading data */
       while ((current_byte = fgetc(plain_text)) != EOF)
       {
           /* has user key expired ? if so reset it*/
           if (*user_key == '\0')
               user_key = argv[1];

           printf("C[%d] = '%02X'\n", i, current_byte);
           output = baldr_decrypt(current_byte, *user_key);
           printf("D(C[%d]) = '%02X'\n", i, output);
           output = output ^ last;
           printf("P[%d] = '%02X'\n", i, output);

           last = current_byte;
           user_key++;
           /* write current byte */
           fputc(output, cipher_text);

           i += 1;
           printf("\n");
       }
       break;

When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl.
Offline Profile Quote Post Goto Top
 
loki
Advanced Member
[ *  *  * ]
Speechless, thank you so much, I can not begin to express the gratitude.

I see my error, its becuase I was decrypting current_byte and updating it when I shouldn't have, the new varible output is used so this doesnt happen.
c(x) = 3x3 + x2 + x + 2; Find the inverse
Offline Profile Quote Post Goto Top
 
jdege
Member Avatar
Elite member
[ *  *  *  *  * ]
loki
Mar 7 2007, 11:27 PM
Speechless, thank you so much, I can not begin to express the gratitude.

You're welcome.

Can I ask for help here, the next time I get stuck?

When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Off-topic · Next Topic »
Add Reply