blowfish decryption

Discussion in 'C Programming' started by d-fan, May 23, 2008.

  1. d-fan

    d-fan Guest

    I am including a sample of the "C" coding that I am using to decode a
    file. The file was encrypted with a component name Cryptocx in the
    windows platform. I need to be able to decrypt the file and poarse
    the resulting string into my program. I am using Openssl version
    0.9.6. The windows component states that it is using a 448 Bit
    keysize and that it automatically base64 encodes the resulting output.

    for my information, does the 448 bit key size mean that the key is 14
    chars long?

    Does anybody see anything wrong with this code? When i try to execute
    it I get garbage to the screen as output.

    #include <stdlib.h>
    #include <string.h>
    #include <openssl/blowfish.h>
    #include <openssl/bio.h>
    #include <openssl/evp.h>
    #include <openssl/sha.h>
    #include <openssl/hmac.h>
    #include <openssl/buffer.h>

    /* BIO_METHOD * BIO_f_base64(void); */


    void decrypt(char *inbuf, char *outbuf, int blklen, char *key)
    {
    int counter = 0;
    char iv[8];
    BF_KEY keyStruct;

    memset(iv, 0, 8);
    BF_set_key(&keyStruct, strlen(key), key);
    BF_cfb64_encrypt(inbuf, outbuf, blklen, &keyStruct,
    iv, &counter, BF_DECRYPT);
    }

    void encrypt(char *inbuf, char *outbuf, int blklen, char *key)
    {
    int counter = 0;
    char iv[8];
    BF_KEY keyStruct;

    memset(iv, 0, 8);
    BF_set_key(&keyStruct, strlen(key), key);
    BF_cfb64_encrypt(inbuf, outbuf, blklen, &keyStruct,
    iv, &counter, BF_ENCRYPT);
    }

    void readfile( char * buf ) {
    FILE * inf ;
    inf = fopen( "locale.txt", "rb" ) ;
    printf( "\n\r before read of file \n\r" ) ;

    fread(buf, sizeof(buf), 1024, inf) ;
    printf( "\n\r after read of file \n\r" ) ;

    fclose( inf ) ;
    }

    void decodebio( unsigned char *encbuf, unsigned char * decbuf, int
    destbuf ) {


    /* Read Base64 encoded data from standard input and write the
    decoded data to standard output: */

    BIO *b64, *bio ;
    long i ;
    char buffer[512] ;
    memset(decbuf, 0, 1024) ;

    b64 = BIO_new(BIO_f_base64() ) ;
    bio = BIO_new(BIO_s_mem()) ;
    bio = BIO_push(b64, bio) ;

    i=BIO_write(bio, encbuf, strlen(encbuf)) ;
    printf( "enc bio i = %d data is %s\n\r", i, encbuf) ;
    //i = BIO_ctrl_pending(bio);
    i = BIO_read(bio, decbuf, 1024) ;

    printf( "the old buffer size is %d %d %s\n\r", i, strlen(decbuf),
    decbuf) ;

    BIO_set_mem_eof_return(bio,0) ;
    BIO_free_all(bio) ;
    //printf( "the resulting data is Length %d size %d text %s \n\r" ,
    strlen(decbuf), sizeof(decbuf), decbuf) ;
    }
    int main(int argc, char **argv)
    {
    char *plain ;
    unsigned char filebuf[1024] ;
    unsigned char decodebuf[1024] ;
    unsigned char *res ;
    char *enc;
    char *dec;
    int len;

    char *key1 = "The Password\0" ;
    char *key2 = "The Password\0" ;


    memset(filebuf, 0, 1024) ;
    memset(decodebuf, 0, 1024) ;
    readfile( filebuf );

    //strcat( filebuf, "\0") ;
    printf( "after reading file size %d %s\n\r", strlen(filebuf),
    filebuf ) ;
    printf( "before decoding file size %d %s\n\r", sizeof(decodebuf),
    decodebuf ) ;
    decodebio( filebuf, decodebuf, sizeof(decodebuf) ) ;

    len = strlen( decodebuf ) ;

    enc = malloc(len+1);
    enc[len] = '\0';
    dec = malloc(len+1);
    dec[len] = '\0';

    decrypt(decodebuf, dec, len, key2);

    printf("key1: '%s'\n", key1);
    printf("ley2: '%s'\n", key2);

    printf("decrypted: '%s'\n", dec);


    }

    This is the data from the file. You mighe be able to make use of it.

    +¢´iÌ®:¶±9žf7ó ÝÆx8†ÆHüW–þƒtU'?-uëí Z, ó;Kì¯ôð>Å#Ûi¹d) ¼UÅ·î
    ´IÑå#½J*0T)¼aÎ3‚|$hŸ®O
    GpÎ[0A¯þ
    ƒµ»¤A%µ¨Eº¿9X­ÜÇM\ï‹ óAMµ¬ƒÔÌš¡|jÂí¸=×gx‰¸.áv½ƒ õ¯“—©Ó³š
    ‰ck?¹Q·v·v>vÿò¬w0xâ¯ñã-#–jU&Ê’\²P•
    šQT_?ÿý‰¸Á>P¼ Ö‘‘^T¹¯€áN‘¸†k6:eek:
     
    d-fan, May 23, 2008
    #1
    1. Advertising

  2. d-fan

    Kevin Handy Guest

    d-fan wrote:
    > I am including a sample of the "C" coding that I am using to decode a
    > file. The file was encrypted with a component name Cryptocx in the
    > windows platform. I need to be able to decrypt the file and poarse
    > the resulting string into my program. I am using Openssl version
    > 0.9.6. The windows component states that it is using a 448 Bit
    > keysize and that it automatically base64 encodes the resulting output.
    >
    > for my information, does the 448 bit key size mean that the key is 14
    > chars long?
    >
    > Does anybody see anything wrong with this code? When i try to execute
    > it I get garbage to the screen as output.
    >
    > #include <stdlib.h>
    > #include <string.h>


    The following are not part of the C standard, and so
    are useless to discuss here. Go to another newsgroup
    where they understand this stuff.

    > #include <openssl/blowfish.h>
    > #include <openssl/bio.h>
    > #include <openssl/evp.h>
    > #include <openssl/sha.h>
    > #include <openssl/hmac.h>
    > #include <openssl/buffer.h>
    >
    > /* BIO_METHOD * BIO_f_base64(void); */
    >
    >
    > void decrypt(char *inbuf, char *outbuf, int blklen, char *key)
    > {
    > int counter = 0;
    > char iv[8];
    > BF_KEY keyStruct;
    >
    > memset(iv, 0, 8);
    > BF_set_key(&keyStruct, strlen(key), key);
    > BF_cfb64_encrypt(inbuf, outbuf, blklen, &keyStruct,
    > iv, &counter, BF_DECRYPT);
    > }
    >
    > void encrypt(char *inbuf, char *outbuf, int blklen, char *key)
    > {
    > int counter = 0;
    > char iv[8];
    > BF_KEY keyStruct;
    >
    > memset(iv, 0, 8);
    > BF_set_key(&keyStruct, strlen(key), key);
    > BF_cfb64_encrypt(inbuf, outbuf, blklen, &keyStruct,
    > iv, &counter, BF_ENCRYPT);
    > }
    >
    > void readfile( char * buf ) {
    > FILE * inf ;
    > inf = fopen( "locale.txt", "rb" ) ;
    > printf( "\n\r before read of file \n\r" ) ;
    >
    > fread(buf, sizeof(buf), 1024, inf) ;
    > printf( "\n\r after read of file \n\r" ) ;
    >
    > fclose( inf ) ;
    > }
    >
    > void decodebio( unsigned char *encbuf, unsigned char * decbuf, int
    > destbuf ) {
    >
    >
    > /* Read Base64 encoded data from standard input and write the
    > decoded data to standard output: */
    >
    > BIO *b64, *bio ;
    > long i ;
    > char buffer[512] ;
    > memset(decbuf, 0, 1024) ;
    >
    > b64 = BIO_new(BIO_f_base64() ) ;
    > bio = BIO_new(BIO_s_mem()) ;
    > bio = BIO_push(b64, bio) ;
    >
    > i=BIO_write(bio, encbuf, strlen(encbuf)) ;
    > printf( "enc bio i = %d data is %s\n\r", i, encbuf) ;
    > //i = BIO_ctrl_pending(bio);
    > i = BIO_read(bio, decbuf, 1024) ;
    >
    > printf( "the old buffer size is %d %d %s\n\r", i, strlen(decbuf),
    > decbuf) ;
    >
    > BIO_set_mem_eof_return(bio,0) ;
    > BIO_free_all(bio) ;
    > //printf( "the resulting data is Length %d size %d text %s \n\r" ,
    > strlen(decbuf), sizeof(decbuf), decbuf) ;
    > }
    > int main(int argc, char **argv)
    > {
    > char *plain ;
    > unsigned char filebuf[1024] ;
    > unsigned char decodebuf[1024] ;
    > unsigned char *res ;
    > char *enc;
    > char *dec;
    > int len;
    >
    > char *key1 = "The Password\0" ;
    > char *key2 = "The Password\0" ;
    >
    >
    > memset(filebuf, 0, 1024) ;
    > memset(decodebuf, 0, 1024) ;
    > readfile( filebuf );
    >
    > //strcat( filebuf, "\0") ;
    > printf( "after reading file size %d %s\n\r", strlen(filebuf),
    > filebuf ) ;
    > printf( "before decoding file size %d %s\n\r", sizeof(decodebuf),
    > decodebuf ) ;
    > decodebio( filebuf, decodebuf, sizeof(decodebuf) ) ;
    >
    > len = strlen( decodebuf ) ;
    >
    > enc = malloc(len+1);
    > enc[len] = '\0';
    > dec = malloc(len+1);
    > dec[len] = '\0';
    >
    > decrypt(decodebuf, dec, len, key2);
    >
    > printf("key1: '%s'\n", key1);
    > printf("ley2: '%s'\n", key2);
    >


    > printf("decrypted: '%s'\n", dec);
    >
    >
    > }
    >
    > This is the data from the file. You mighe be able to make use of it.

    [snipped]

    It's the notice of a hyperspace bypass being built.

    Do you really expect binary data to survive passage
    through a text newsgroup? Or that it won't mess up peoples
    screens?


    ----== Posted via Pronews.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.pronews.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= - Total Privacy via Encryption =---
     
    Kevin Handy, May 23, 2008
    #2
    1. Advertising

  3. d-fan

    Flash Gordon Guest

    d-fan wrote, On 23/05/08 06:15:

    <snip>

    Someone else pointed out that most of your code is not topical here, but
    I did spot some topical problems.

    > void readfile( char * buf ) {
    > FILE * inf ;
    > inf = fopen( "locale.txt", "rb" ) ;


    What if fopen fails?

    > printf( "\n\r before read of file \n\r" ) ;


    Why the \r's?

    > fread(buf, sizeof(buf), 1024, inf) ;


    sizeof(buf) is the size of a pointer to char, a value that in all
    probability is not 1 on your system. You are asking fread to read 1024
    objects of that size, yet down below you allocate a buffer of only 1024
    chars. 1024*number_greater_than_1 does not fit in 1024!

    Also you don't check if fread succeeded. It returns a value for a reason!

    > printf( "\n\r after read of file \n\r" ) ;
    >
    > fclose( inf ) ;
    > }


    <snip>

    > int main(int argc, char **argv)
    > {
    > char *plain ;
    > unsigned char filebuf[1024] ;


    <snip>

    > readfile( filebuf );


    <snip>
    --
    Flash Gordon
     
    Flash Gordon, May 23, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Blowfish hash

    , May 25, 2004, in forum: Java
    Replies:
    2
    Views:
    1,338
    Rajesh Tihari
    May 26, 2004
  2. Claus Wanner

    BlowFish decrypting

    Claus Wanner, Nov 3, 2005, in forum: C++
    Replies:
    2
    Views:
    1,154
  3. Damon Getsman
    Replies:
    0
    Views:
    420
    Damon Getsman
    Jun 4, 2008
  4. rumz

    Blowfish and Rails

    rumz, Dec 6, 2007, in forum: Ruby
    Replies:
    0
    Views:
    116
  5. James Keener

    Blowfish hash

    James Keener, Oct 27, 2010, in forum: Ruby
    Replies:
    1
    Views:
    182
    Luis Parravicini
    Oct 28, 2010
Loading...

Share This Page