Hi there..
When I'm using BIO_s_file() everything is ok but if I use BIO_s_mem()
the last encrypted block does not want to be decrypted.. Right now I'm
just adding 15 spaces to the end of text but I'm sure there is more
right way to do it... Here are my enc & dec procedures (I know they're
crap, but I'm a very beginner to c++ :) ):

BOOL Enc(LPSTR PubFileName,BOOL isFile,LPSTR intext, int intextsize, LPSTR 
outtext, LPSTR filename){
        BIO *in=NULL,*out=NULL, *pub=NULL, *b64=NULL, *benc=NULL;
        EC_KEY *hispub=NULL;
        char commonkey[KDF1_SHA512_len]={0};
        unsigned char *buff={0};
        int bsize=8*1024*1024,inl,bw;
        unsigned char 
key[SHA512_DIGEST_LENGTH]={0},iv[EVP_MAX_IV_LENGTH]={0},salt[PKCS5_SALT_LEN]={0};
        EVP_CIPHER_CTX *ctx=NULL;
        char mydir[300]={0};
        HANDLE hf;
        LPOFSTRUCT fs=NULL;

        GetModuleFileName(GetModuleHandle(NULL),mydir,300);
        PathToDir ((char *)mydir);

        InitEcKey (hispub);
        

        pub=BIO_new(BIO_s_file());
        BIO_read_filename(pub,strcat(strcat(mydir,"\\Keyz\\"),PubFileName));
        hispub=PEM_read_bio_EC_PUBKEY(pub,NULL,NULL,NULL);
        if (!EC_KEY_check_key(hispub)/* || Mypr==NULL*/) {
                MessageBox(0,"Public keyfile format error\n(check the leading 
and trailing spaces)","Error",MB_ICONERROR);                                    
                          
                return false;
        }
        BIO_free_all(pub);
        GetCommonKey (hispub,Mypr,&commonkey);

        if (isFile) {
                hf = 
CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL
 | FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY | 
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_SEQUENTIAL_SCAN,NULL);
                if (hf) {
                        bsize = GetFileSize (hf,NULL);
                        CloseHandle(hf);
                }
                buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
                in=BIO_new(BIO_s_file());
                BIO_read_filename(in,filename);
                inl=BIO_read(in,buff,bsize);
                out=BIO_new(BIO_s_file());
                strcat((char *)filename,".enc");
                BIO_write_filename(out,(char *)filename);

        } else{
                intext=strcat(intext,"               ");
                bsize=intextsize+15;
                inl=intextsize+15;
                out=BIO_new(BIO_s_mem());
        }
        
        b64=BIO_new(BIO_f_base64());
        if (!isFile) {
                BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
        }
        RAND_pseudo_bytes(salt, PKCS5_SALT_LEN); 
        out=BIO_push(b64,out);

        BIO_write(out,magic,sizeof magic-1); //writing the word "slt"
        BIO_write(out,(char *)salt, PKCS5_SALT_LEN); //writing random salt
        EVP_BytesToKey(EVP_aes_256_cbc(),EVP_sha512(),salt,(unsigned char 
*)commonkey,KDF1_SHA512_len,21,key,iv);// generating encryption key and iv from 
our common key
        benc=BIO_new(BIO_f_cipher());
        BIO_get_cipher_ctx(benc, &ctx);
        EVP_CipherInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv, 1);// We'll 
encrypt our message with aes-256 bits in cbc mode
        out=BIO_push(benc,out); // Add this to BIO chain
        if (isFile) {
                bw=BIO_write(out,(char *)buff,inl); // Write out our message. 
It will be encrypted, than added to our salt and base64 encoded
        } else{
                bw=BIO_write(out,intext,inl);
        }

        
        BIO_flush(out);
        if (!isFile) {
                out=BIO_pop(b64);
                out=BIO_pop(benc);
                BIO_seek(out,0);
                BIO_read(out,outtext,65536);
                if (buff) CRYPTO_free(buff);
        }

        if (intext) {
                ZeroMemory(intext,intextsize);
        }
        EVP_CIPHER_CTX_cleanup(ctx);
        if (out) BIO_free_all(out);
        if (in) BIO_free_all(in);
        EC_KEY_free(hispub);
        CRYPTO_cleanup_all_ex_data();
        return TRUE;
}

BOOL Dec(LPSTR PubFileName,BOOL isFile,LPSTR intext, int intextsize, LPSTR 
outtext, LPSTR filename){
        BIO *in=NULL,*out=NULL, *pub=NULL, *b64=NULL, *benc=NULL;
        EC_KEY *hispub=NULL;
        char commonkey[KDF1_SHA512_len]={0};
        unsigned char *buff=NULL;
        int bsize=8*1024*1024;
        unsigned char 
key[SHA512_DIGEST_LENGTH]={0},iv[EVP_MAX_IV_LENGTH]={0},salt[PKCS5_SALT_LEN]={0};
        EVP_CIPHER_CTX *ctx=NULL;
        char mydir[300]={0};
        HANDLE hf=0;
        LPOFSTRUCT fs=NULL;
        char mbuf[3]={0};
        int br, errcode;


        GetModuleFileName(GetModuleHandle(NULL),mydir,300);
        PathToDir ((char *)mydir);


        InitEcKey (hispub);

        pub=BIO_new(BIO_s_file());
        BIO_read_filename(pub,strcat(strcat(mydir,"\\Keyz\\"),PubFileName));
        hispub=PEM_read_bio_EC_PUBKEY(pub,NULL,NULL,NULL);
        if (!EC_KEY_check_key(hispub)/* || Mypr==NULL*/) {
                MessageBox(0,"Public keyfile format 
error","Error",MB_ICONERROR);                                                   
            
                return FALSE;
        }
        BIO_free_all(pub);

        GetCommonKey (hispub,Mypr,&commonkey);

        if (isFile) {

                hf = 
CreateFile(filename,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL
 | FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_READONLY | 
FILE_ATTRIBUTE_SYSTEM | FILE_FLAG_SEQUENTIAL_SCAN,NULL);
                if (hf) {
                        bsize = GetFileSize (hf,NULL);
                        CloseHandle(hf);
                }
                buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
                in=BIO_new(BIO_s_file());
                BIO_read_filename(in,filename);
        } else{
                buff=(unsigned char *)OPENSSL_malloc(intextsize);
                in=BIO_new(BIO_s_mem());
                BIO_write(in,(char *)intext,intextsize-1);
                BIO_seek(in,0);
        }

        b64=BIO_new(BIO_f_base64());
        if (!isFile) {
                BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
        }
        in=BIO_push(b64,in);
        BIO_read(in, mbuf, 3);
        BIO_read(in, salt, PKCS5_SALT_LEN);
        benc=BIO_new(BIO_f_cipher());
        BIO_get_cipher_ctx(benc, &ctx);
        EVP_BytesToKey(EVP_aes_256_cbc(),EVP_sha512(),salt,(const unsigned char 
*)commonkey,KDF1_SHA512_len,21,key,iv);
        EVP_CipherInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv, 0);
        in=BIO_push(benc,in);
        br=BIO_read(in,(char *)buff,bsize);
        errcode=ERR_get_error();
        if(errcode){
                return FALSE;
        }
        if (isFile) {
                out=BIO_new(BIO_s_file());
                filename[strlen(filename)-4]=0;
                BIO_write_filename(out,filename);
                br=BIO_write(out,buff,br);
                BIO_flush(out);
        } else if(br>0) {
                memcpy(outtext,(const char *)buff,br);
                TrimString(outtext);
        }

        EVP_CIPHER_CTX_cleanup(ctx);
        OPENSSL_free(buff);
        BIO_free_all(in);
        BIO_free_all(out);
        return TRUE;
}

thanks in advance for any help

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           [EMAIL PROTECTED]

Reply via email to