Hai
this is  the openssl shell command i'm using to decrypt a binary file...
openssl enc -d -K
3834373532303435333730323834383132373330393233343531323330383839 -iv
36363333383630393433313132323031 -aes-256-cbc-in final2.bin -out test.zip
As my target doesnot support openssl commd i was asked to write decryption
code using some API's and create a binary ...so I decided to write it using
AES API's 
Here is my code please tell me where I went wrong...thanks in advance.
int main()
    {

    unsigned long lSize;
    FILE * inFile;
    FILE * outFile;
    size_t n,m;
    char * pTemp;
    char * buffer;
    const AES_KEY *key;
    inFile = fopen("SB2.bin", "rb");
    outFile = fopen("finalPackResult", "wb+");
    
    unsigned char uKey[]=
"3834373532303435333730323834383132373330393233343531323330383839";
    unsigned char iv[] = "36363333383630393433313132323031"; 
    /* Larger key length will make the program very slower
     * in AES CBC mode encryption/decryption for more info check
     * http://www.mail-archive.com/openssl-...@openssl.org/msg23863/aes.pod
     */
    int keyLength = sizeof(uKey);
    int ivLength  = sizeof(iv);
    printf("key length:%d.\n",keyLength);
    printf("Initialisation vector length:%d.\n",ivLength);

    /* get file size and allocate memory to buffer */
    fseek (inFile , 0,SEEK_END);
    lSize = ftell (inFile);
    rewind (inFile);
    printf("File size is: %ld.\n",lSize);
    unsigned char pInBuff[lSize];
    unsigned char pOutBuff[lSize];

    /* Allocate memory */
    memset(pInBuff,0,lSize * sizeof(char));
    memset(pOutBuff,0,lSize * sizeof(char));
    buffer = (char *)malloc(sizeof(char)*lSize);

    /* Read the input signature package which contains 
     * encrypted data. The AES encryption technique 
     * with CBC mode is followed 
     */
    if ((n = fread (pInBuff, 1, lSize, inFile)) == -1)
           {
           printf ("read error");
           return -1;
           }
    printf("Data read from input binary is :%d\n", n);
    if (n == 0)
        {
        printf ("Data read error from input file.\n");
        return -1;
        }
    printf("LINE :%d.\n",__LINE__);


    /* The AES_cbc_encrypt() function implements the Cipher Block Chaining
(CBC)
     * mode of operation.  It encrypts or decrypts B<in> using the key
schedule 
     * B<schedule> and places the result in B<out>.  The value of B<length>
is 
     * the length of B<in> in bytes
     */
    AES_set_decrypt_key(uKey, 8*sizeof(uKey), &key);
    printf("LINE :%d.\n",__LINE__);

    AES_cbc_encrypt(pInBuff, pOutBuff, lSize, key, iv, AES_DECRYPT);
    printf("LINE :%d.\n",__LINE__);
    pTemp = AES_options();
    printf("AES function to which pointed:%s.\n", pTemp);
    
    /* Write decrypted data into a file */
    if((m=fwrite(pOutBuff, 1, lSize, outFile)) == -1)
      {
      printf ("Write error.\n");
      }
    if(m != n)
      {
      printf("Didn't write the data properly.%dbytes info missed",n-m);
      }
    printf("size of data written is:%d.\n",m);
    return 0;
    }

-- 
View this message in context: 
http://old.nabble.com/help-me-decrypting-a-binary-file-which-is-encrypted-by-aes-256-cbc-mode-tp26387750p26387750.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