#1
  1. WebDeveloper++;
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    New York City
    Posts
    85
    Rep Power
    12

    Blowfish problems


    Hey, I have a problem. I have two Blowfish classes: one in Python, and one in Java. Now, when I try to encrypt the following:

    key: [102, 111, 111, 108, 98, 97, 114, 122]
    data: [116, 101, 115, 116, 116, 101, 115, 116]

    In python I get a result of: [18, 196, 31, 187, 245, 170, 192, 170]

    In java I get a result of: [-50, 62, -123, 18, 42, 2, -72, -59] which translates to: [206, 62, 133, 18, 42, 2, 184, 197]

    Now, what I wonder is... what factors cause the two classes to get different results?? They are both blowfish classes, and have the same p boxes and s boxes. When I try to decrypt the results using the key again, I get the original data, so they do work fine. What could be the problem? I'm very new to blowfish.

    I'm trying to port a program from Java to Python, so its important I get the same result I get in Java for my Python program to work.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Which one do you consider gives the correct encryption and at which point in your code does the other version start to go wrong?
    Last edited by Grim Archon; June 30th, 2004 at 11:57 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    37
    Rep Power
    11
    I'm sorry I don't have the ansewers for you. I checked out the Blowfish FAQ to see if I could come up with something, but I'd like to see if I'm understand you right....

    key: [102, 111, 111, 108, 98, 97, 114, 122] -- Key for Both
    data: [116, 101, 115, 116, 116, 101, 115, 116] -- Plaindata
    [18, 196, 31, 187, 245, 170, 192, 170] -- encrypted on Python
    [206, 62, 133, 18, 42, 2, 184, 197] -- encrypted on Java

    So do they decrypt alright ? If so then what is the big deal? Unless you're trying to get your python program to pass the encrypted data to another program that the java program did.
  6. #4
  7. WebDeveloper++;
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    New York City
    Posts
    85
    Rep Power
    12
    Originally Posted by Grim Archon
    Which one do you consider gives the correct encryption and at which point in your code does the other version start to go wrong?
    The Java version is the output I am looking for. The python version gives a completely different output. I'm trying to build a program that decrypts a file from the filesystem. It's important that python gets the same result as the java version or it won't be able to decrypt it to its plaintext.

    Originally Posted by Noah_C
    I'm sorry I don't have the ansewers for you. I checked out the Blowfish FAQ to see if I could come up with something, but I'd like to see if I'm understand you right....

    key: [102, 111, 111, 108, 98, 97, 114, 122] -- Key for Both
    data: [116, 101, 115, 116, 116, 101, 115, 116] -- Plaindata
    [18, 196, 31, 187, 245, 170, 192, 170] -- encrypted on Python
    [206, 62, 133, 18, 42, 2, 184, 197] -- encrypted on Java

    So do they decrypt alright ? If so then what is the big deal? Unless you're trying to get your python program to pass the encrypted data to another program that the java program did.
    Yes, they decrypt alright if encrypted from the same class, but its imporant that the code is portable. Like I said above, the python program needs to be able to open a file that was already encrypted with an output similar to the java program.

    I still don't know what could cause this. Isn't the output suppose to be the same regardless of the class as long as its blowfish?
  8. #5
  9. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    Maybe one is enciphering in Electronic Code Book Mode (ECB) and the other is enciphering in Cipherblock Chaining (CBC) mode?
    http://www.openbsd.org/cgi-bin/man.c...86&format=html

    I whipped up some C code that does the encryption in ECB mode. The output is identical to your python code.
    Code:
    #include <stdio.h>
    #include <blf.h>
    
    int main(void)
    {
      u_int8_t key[] = {102, 111, 111, 108, 98, 97, 114, 122};
      u_int8_t data[] = {116, 101, 115, 116, 116, 101, 115, 116};
      blf_ctx state;
      int i;
    
      blf_key(&state, key, sizeof(key));
      blf_ecb_encrypt(&state, data, sizeof(data));
    
      for (i = 0; i < sizeof(data); i++)
        printf("%d\n", (unsigned int) data[i]);
    
      return 0;
    }
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  10. #6
  11. WebDeveloper++;
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    New York City
    Posts
    85
    Rep Power
    12
    Yes. In the Python version, I am using ECB mode. However, I don't know what mode the Java version uses. I tried every single mode available in the Crypto.Cipher.Blowfish module except CTR and I still don't get the same outout:

    Code:
    Mode #1 (ECB)
            key: [102, 111, 111, 108, 98, 97, 114, 122]
            before: [116, 101, 115, 116, 116, 101, 115, 116]
            after: [18, 196, 31, 187, 245, 170, 192, 170]
    Mode #2 (CBC)
            key: [102, 111, 111, 108, 98, 97, 114, 122]
            before: [116, 101, 115, 116, 116, 101, 115, 116]
            after: [18, 196, 31, 187, 245, 170, 192, 170]
    Mode #3 (CFB)
            key: [102, 111, 111, 108, 98, 97, 114, 122]
            before: [116, 101, 115, 116, 116, 101, 115, 116]
            after: [76, 148, 247, 187, 72, 120, 27, 140]
    Mode #4 (PGP)
            key: [102, 111, 111, 108, 98, 97, 114, 122]
            before: [116, 101, 115, 116, 116, 101, 115, 116]
            after: [76, 113, 104, 158, 33, 198, 91, 112]
    Mode #5 (OFB) 
            key: [102, 111, 111, 108, 98, 97, 114, 122]
            before: [116, 101, 115, 116, 116, 101, 115, 116]
            after: [76, 113, 104, 158, 33, 198, 91, 112]
    Below is the Java file with the BlowfishEngine class. Any idea what mode this might be?

    http://www.raineri.ws/code/BlowfishEngine.java
  12. #7
  13. (not) Banned
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2004
    Location
    Toronto, CANADA
    Posts
    598
    Rep Power
    11
    Are the variables that you are using the same type as in the python script? I'm not sure if that would make a difference, but hey, there's my two cents.
  14. #8
  15. WebDeveloper++;
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2003
    Location
    New York City
    Posts
    85
    Rep Power
    12
    Originally Posted by obi_wonton
    Are the variables that you are using the same type as in the python script? I'm not sure if that would make a difference, but hey, there's my two cents.
    They both get transformed from string into an array/list.
  16. #9
  17. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    How about trying a different API instead, for instance one from Sun.
    http://java.sun.com/products/jce/doc...ers_guide.html

    Right now, we know that the python and C API coincide. Matter of fact, I got the same output with the C program and CBC mode (initial array was set to 0)
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo