Thanks for your help so far, but still cannot get this working...

Here is my latest code.

I am trying to emulate something that should be simple, which can be
decrypted using
openssl des3 -d -nosalt -k "1" -in <source filename> -out <destination
> filename>



    EVP_CIPHER_CTX ctx;
    EVP_CIPHER_CTX_init(&ctx);
    const EVP_CIPHER* cipher=EVP_des_ede3_cbc();
    const EVP_MD *dgst=EVP_sha1();

    u_char pass[8]; 
//    char salt[8]; 

    unsigned char key[EVP_MAX_KEY_LENGTH]; 
    unsigned char iv[EVP_MAX_IV_LENGTH]; 



//    printf("key_len: %d\n", cipher->key_len); 
  //  printf(" iv_len: %d\n", cipher->iv_len); 

        strcpy((char*)pass, "1"); 
//    strcpy((char*)salt, "ATHENA.MIT.EDUraeburn"); 


        EVP_BytesToKey(cipher,dgst,NULL,pass, 1, 3, key, iv);

        unsigned char* desBuf = new unsigned char[numbytes];

        EVP_EncryptInit_ex(&ctx, cipher, NULL,key,iv);

        if(!EVP_EncryptUpdate(&ctx,desBuf,&outlen,input,numbytes))
        {
                AfxMessageBox("error 1");
        }

        if(!EVP_EncryptFinal_ex(&ctx, desBuf+ outlen, &tmplen))
        {
                AfxMessageBox("error 2");
        /* Error */     
        }

        EVP_CIPHER_CTX_cleanup(&ctx);

        outlen += tmplen;

        fwrite(desBuf, 1, outlen, outfile);
        fclose(outfile);
        fclose(infile);




Dr. Stephen Henson wrote:
> 
> On Mon, Aug 10, 2009, MusicAndy wrote:
> 
>> 
>> OK, so after the last post I made; I was trying to use the DES library,
>> and
>> was advised to use the higher level EVP library, I have now used this and
>> getting the same type of error! %-|
>> 
>> So where do I go from here.  The command line that i wish to use for
>> decrypting the file that i am encrypting is 
>>  openssl des3 -d -nosalt -k "1" -in <source filename> -out <destination
>> filename>
>> 
>> The code that i am using to test these has two file streams, one for the
>> input and one for the output and is shown below.  This is test code only. 
>> What am i doing wrong here?  Is it the key that is at fault.  Please
>> help... 
>> I also wish to use salt in the final implementation on this.  Please
>> advise...
>> 
>> 
>>      if(infile == NULL)
>>              AfxMessageBox("File load error");
>>  
>>      fseek(infile, 0L, SEEK_END);
>>      numbytes = ftell(infile);
>>  
>>      fseek(infile, 0L, SEEK_SET);    
>> 
>>      buffer = (char*)calloc(numbytes, sizeof(char));         
>> 
>>      fread(buffer, sizeof(char), numbytes, infile);
>>  
>> 
>>      unsigned char* input = new unsigned char[numbytes];
>>      input = (unsigned char*)buffer;
>> 
>> 
>>      if(buffer == NULL)
>>              AfxMessageBox("Error with buffer");
>>  
>>      unsigned char password[] = {'1'};
>>      EVP_CIPHER_CTX ctx;
>>      EVP_CIPHER_CTX_init(&ctx);
>> 
>>      unsigned char* desBuf = new unsigned char[numbytes];
>> 
>>      EVP_EncryptInit_ex(&ctx, EVP_des_ede3_cbc(), NULL,password,NULL);
>> 
> 
> Well that's the problem right there. You have to pass a key and IV of the
> correct length to EVP_EncryptInit_ex(). You are passing 1 byte and
> whatever
> garbage follows it in memory.
> 
> You need to derive the correct key using EVP_BytestToKey() and pass the
> derived key to EVP_EncryptInit_ex().
> 
> Steve.
> --
> Dr Stephen N. Henson. OpenSSL project core developer.
> Commercial tech support now available see: http://www.openssl.org
> ______________________________________________________________________
> OpenSSL Project                                 http://www.openssl.org
> User Support Mailing List                    openssl-users@openssl.org
> Automated List Manager                           majord...@openssl.org
> 
> 

-- 
View this message in context: 
http://www.nabble.com/EVP-errors%21-tp24898590p24900649.html
Sent from the OpenSSL - User mailing list archive at Nabble.com.

______________________________________________________________________
OpenSSL Project                                 http://www.openssl.org
User Support Mailing List                    openssl-users@openssl.org
Automated List Manager                           majord...@openssl.org

Reply via email to