Blowfish Help in ANSI-C

Matrinix

Beta member
Messages
1
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.
 
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.
 
Back
Top Bottom