blowfish decryption

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • d-fan

    blowfish decryption

    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(vo id); */


    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(&key Struct, strlen(key), key);
    BF_cfb64_encryp t(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(&key Struct, strlen(key), key);
    BF_cfb64_encryp t(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_b ase64() ) ;
    bio = BIO_new(BIO_s_m em()) ;
    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_pendin g(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(bi o) ;
    //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(decodebu f, 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(decodebu f),
    decodebuf ) ;
    decodebio( filebuf, decodebuf, sizeof(decodebu f) ) ;

    len = strlen( decodebuf ) ;

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

    decrypt(decodeb uf, dec, len, key2);

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

    printf("decrypt ed: '%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 :o
  • Kevin Handy

    #2
    Re: blowfish decryption

    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(vo id); */
    >
    >
    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(&key Struct, strlen(key), key);
    BF_cfb64_encryp t(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(&key Struct, strlen(key), key);
    BF_cfb64_encryp t(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_b ase64() ) ;
    bio = BIO_new(BIO_s_m em()) ;
    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_pendin g(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(bi o) ;
    //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(decodebu f, 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(decodebu f),
    decodebuf ) ;
    decodebio( filebuf, decodebuf, sizeof(decodebu f) ) ;
    >
    len = strlen( decodebuf ) ;
    >
    enc = malloc(len+1);
    enc[len] = '\0';
    dec = malloc(len+1);
    dec[len] = '\0';
    >
    decrypt(decodeb uf, dec, len, key2);
    >
    printf("key1: '%s'\n", key1);
    printf("ley2: '%s'\n", key2);
    >
    printf("decrypt ed: '%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 =---

    Comment

    • Flash Gordon

      #3
      Re: blowfish decryption

      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_gre ater_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

      Comment

      Working...