blowfish decryption

D

d-fan

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:
 
K

Kevin Handy

d-fan said:
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?
 
F

Flash Gordon

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 ) ;
}

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

readfile( filebuf );

<snip>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top