Go Back   Computer Forums > General Computing > Programming
Click Here to Login
Join Computer forums Today


Reply
 
Thread Tools Search this Thread Display Modes
 
Old 04-06-2007, 06:30 PM   #1
Beta Member
 
Join Date: Jul 2006
Posts: 1
Question Blowfish Help in ANSI-C

I am trying to implement Blowfish in an ANSI-C program I am writing but am not getting desirable results from the code I have been provided.

I am using code from Paul Kocker and Bill Gatliff. Pretty much everything works fine except that when I run the program, this happens:
Code:
Plaintext message string is: Blowfish cryptology
Encrypted message string is: 622694e5a125c1dc
Decrypted message string is: ogy
I need help making my string decrypt correctly or encrypt correctly if that is the case. Any help would be greatly appreciated.

The code I am using is as followed:

blowfish_test.c
Code:
#include <stdio.h>
#include <string.h>
#include "blowfish.h"

int main (void) {
   BLOWFISH_CTX ctx;

   /* must be less than 448-bit */
   char *key = "8gdlas8fx34kl1x9a0s";
   int keylen = (int)strlen(key);

   unsigned char *plaintext_string = "Blowfish cryptology";
   int plaintext_len = (int)strlen(plaintext_string);

   unsigned char ciphertext_buffer[256];
   unsigned char *ciphertext_string = &ciphertext_buffer[0];
   int ciphertext_len = 0;

   unsigned long message_left;
   unsigned long message_right;
   int block_len;

   Blowfish_Init(&ctx, key, keylen);

   printf("Plaintext message string is: %s\n", plaintext_string);

   /* encrypt the plaintext message string */
   printf("Encrypted message string is: ");

   while (plaintext_len) 
   {
     message_left = message_right = 0UL;

   /* crack the message string into a 64-bit block (ok, really two 32-bit blocks); pad with zeros if necessary */
     for (block_len = 0; block_len < 4; block_len++) 
     {
       message_left = message_left << 8;
       if (plaintext_len) 
       {
           message_left += *plaintext_string++;
           plaintext_len--;
       }
       else message_left += 0;
     }
     for (block_len = 0; block_len < 4; block_len++) 
     {
       message_right = message_right << 8;
       if (plaintext_len) 
       {
           message_right += *plaintext_string++;
           plaintext_len--;
       }
       else message_right += 0;
     }
   }
   /* encrypt and print the results */
     Blowfish_Encrypt(&ctx, &message_left, &message_right);
     printf("%lx%lx", message_left, message_right);

   /* save the results for decryption below */
     *ciphertext_string++ = (unsigned char)(message_left >> 24);
     *ciphertext_string++ = (unsigned char)(message_left >> 16);
     *ciphertext_string++ = (unsigned char)(message_left >> 8);
     *ciphertext_string++ = (unsigned char)message_left;
     *ciphertext_string++ = (unsigned char)(message_right >> 24);
     *ciphertext_string++ = (unsigned char)(message_right >> 16);
     *ciphertext_string++ = (unsigned char)(message_right >> 8);
     *ciphertext_string++ = (unsigned char)message_right;
     ciphertext_len += 8;
printf("\n");

   /* reverse the process */
     printf("Decrypted message string is: ");

     ciphertext_string = &ciphertext_buffer[0];
     while(ciphertext_len) 
     {
        message_left = message_right = 0UL;

        for (block_len = 0; block_len < 4; block_len++) 
        {
          message_left = message_left << 8;
          message_left += *ciphertext_string++;
          if (ciphertext_len)
           ciphertext_len--;
        }
        for (block_len = 0; block_len < 4; block_len++) 
        {
           message_right = message_right << 8;
           message_right += *ciphertext_string++;
           if (ciphertext_len)
           ciphertext_len--;
        }

        Blowfish_Decrypt(&ctx, &message_left, &message_right);

   /* if plaintext message string padded, extra zeros here */

        printf("%c%c%c%c%c%c%c%c", 
        (int)(message_left >> 24), (int)(message_left >> 16),
        (int)(message_left >> 8), (int)(message_left),
        (int)(message_right >> 24), (int)(message_right >> 16),
        (int)(message_right >> 8), (int)(message_right));
}

printf("\n");

return 0;
}
The source code files blowfish.c and blowfish.h are available in http://www.schneier.com/code/bfsh-koc.zip.
__________________

Matrinix is offline   Reply With Quote
Old 04-07-2007, 11:43 AM   #2
Fully Optimized
 
UK31337's Avatar
 
Join Date: Feb 2005
Posts: 2,776
Default Re: Blowfish Help in ANSI-C

So what you're saying is you're not getting the full message decrypted? Hmm... let me think about this.

It looks suspiciously like there's a problem with your decryption loop not looping properly through all your data, as there's obviously something being cut off. Have you tried debugging? I'm too busy to try it just now but I might be able to have a look later on for you.
UK31337 is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off



All times are GMT -5. The time now is 08:20 PM.


Powered by vBulletin® Version 3.8.8 Beta 4
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Friendly URLs by vBSEO 3.6.0