Like Tree1Likes
  • 1 Post By stahta01

3DES ECB MODE IN OPENSSL Can't get expected Result

This is a discussion on 3DES ECB MODE IN OPENSSL Can't get expected Result within the C Programming forums, part of the General Programming Boards category; Hi All, I am using the OPENSSL to encrypt the plain text, but the output is wrong, can anyone give ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    49

    3DES ECB MODE IN OPENSSL Can't get expected Result

    Hi All,

    I am using the OPENSSL to encrypt the plain text, but the output is wrong, can anyone give me hints or solution, many thanks!!

    Code:
    unsigned char out[8];
        char back[bufsize],workvec[bufsize];
        const char *temp="
    0592789FFFEDCBA9
    "; des_cblock key1,key3,seed = {0x13,0x13,0x23,0x23,0x23,0x23,0x13,0x13}; des_cblock key2 = {0x67,0x89,0x67,0x89,0x67,0x89,0x67,0x89}; des_key_schedule ks1,ks2,ks3; char master1c[8]; char c1[16]; int length = strlen(temp); int i; char buf = 0; int z=0; for(i = 0; i < length; i++){ if(i % 2 != 0){ //change hex to ascii sprintf(master1c+z, "%c", hex_to_ascii(buf, c1[i])); z++; }else{ buf = c1[i]; } } char *in =master1c; DES_set_key((C_Block *)_k1,&ks1); DES_set_key((C_Block *)_k2,&ks2); DES_set_key((C_Block *)_k3,&ks3); DES_ecb3_encrypt((C_Block *)in,out,&ks1,&ks2,&ks3, DES_ENCRYPT); for (i=0; i<8; i++) { printf("0x%02X, ", out[i]); }
    My Wrong Result=0xAD, 0x62, 0xDC, 0x19, 0xBB, 0xFE, 0x61, 0x52

    Expected Result=0x
    90, 0xDE, 0xBD, 0x82, 0x7C, 0xEE, 0x4F, 0xDD
    Last edited by homoon; 05-20-2012 at 03:20 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    How are we supposed to work anything out when half the code and declarations are missing?

    What is hex_to_ascii(), does it get the byte the right way round?

    What are _k1 etc?

    You can simplify the sprintf to
    master1c[z] = hex_to_ascii(buf, c1[i]);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    Hi, whole code as below, _k1,_k2, _k3 change to key1, key2, key3
    please give me some hints, many thanks!!


    Code:
    unsigned char out[8];    
    const char *temp="0592789FFFEDCBA9";
     des_cblock key1,key3,seed = {0x13,0x13,0x23,0x23,0x23,0x23,0x13,0x13};   
     des_cblock key2 =           {0x67,0x89,0x67,0x89,0x67,0x89,0x67,0x89};        
    des_key_schedule ks1,ks2,ks3;
    char master1c[8];    
    char c1[16];
        int length = strlen(temp); 
       int i;
        char buf = 0;  
      int z=0;
        for(i = 0; i < length; i++)
        {
            if(i % 2 != 0)
           {
                 //change hex to ascii            
                 sprintf(master1c+z, "%c", hex_to_ascii(buf, c1[i])); 
               z++;        
           }else
           {            
                 buf = c1[i];        
            }    
        }    
        char *in =master1c;
        DES_set_key((C_Block *)key1,&ks1);
         DES_set_key((C_Block *)key2,&ks2);
        DES_set_key((C_Block *)key3,&ks3);
            DES_ecb3_encrypt((C_Block *)in,out,&ks1,&ks2,&ks3, DES_ENCRYPT);    
    
         for (i=0; i<8; i++)    
        {        
               printf("0x%02X, ", out[i]);    
        }
    Code:
    int hex_to_int(char c){
        if(c>=97)
            c=c-32;
        int first = c / 16 - 3;
        int second = c % 16;
        int result = first*10 + second;
        if(result > 9) result--;
        return result;
    }
    
    
    int hex_to_ascii(char c, char d){
        int high = hex_to_int(c) * 16;
        int low = hex_to_int(d);
        return high+low;
    }
    Last edited by homoon; 05-20-2012 at 01:41 PM.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    please help

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    Where do you set key1 and key3 to a valid value?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    Here is the key valuekey1, key3= 1313232323231313key2 = 6789678967896789===============================
    Code:
    des_cblock key1,key3,seed = {0x13,0x13,0x23,0x23,0x23,0x23,0x13,0x13};   des_cblock key2 =           {0x67,0x89,0x67,0x89,0x67,0x89,0x67,0x89};

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    Quote Originally Posted by homoon View Post
    Here is the key valuekey1, key3= 1313232323231313key2 = 6789678967896789===============================
    Code:
    des_cblock key1,key3,seed = {0x13,0x13,0x23,0x23,0x23,0x23,0x13,0x13};   des_cblock key2 =           {0x67,0x89,0x67,0x89,0x67,0x89,0x67,0x89};
    I see you setting the seed to a value; NO WHERE are you setting key1 or key3 to a initial value in the code posted!!

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        typedef des_cblock[8];
        int i;
    
        des_cblock key1,key3,seed = {0x13,0x13,0x23,0x23,0x23,0x23,0x13,0x13};
        des_cblock key2 =           {0x67,0x89,0x67,0x89,0x67,0x89,0x67,0x89};
    
        for (i=0; i< 8; i++)
        {
            printf("%d\n", seed[i]);
        }
    
        for (i=0; i< 8; i++)
        {
            printf("%d\n", key1[i]); // Prints out garbage values
        }
    
        return 0;
    }
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    49
    Oh,...yes, there is some mistake, and I have modifed it...
    I want to ask a question, the plain text pass in to the
    DES_ecb3_encrypt function, is it a "hex string"? or I need to change it to ascii code?

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    Quote Originally Posted by homoon View Post
    I want to ask a question, the plain text pass in to the
    DES_ecb3_encrypt function, is it a "hex string"? or I need to change it to ascii code?
    I have no idea; I suggest reading the documentation or looking for example usage.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  11. #11
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    Code:
    printf("10 hex:= %d dec\n", hex_to_int("10"));
    Any reason you function hex_to_int() only returns the value of 5 for any of the input I tested it with?

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    2,770
    This is an example of code that can be compiled; you are really more likely to get help. When you post a short but complete program. Or at least a complete function.

    FYI: You helper functions are junk; you need to document them and test them.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #include "openssl/des.h"
    
    int main() {
    
        unsigned char out[8]={0};
        des_cblock key1 = {0x13,0x13,0x23,0x23,0x23,0x23,0x13,0x13};
        des_cblock key3 = {0x13,0x13,0x23,0x23,0x23,0x23,0x13,0x13};
        des_cblock key2 = {0x67,0x89,0x67,0x89,0x67,0x89,0x67,0x89};
        des_key_schedule ks1,ks2,ks3;
        unsigned char in[8]= {0x05, 0x92, 0x78, 0x9F, 0xFF, 0xED, 0xCB, 0xA9};
        int i=0;
    
        DES_set_key((C_Block *)key1,&ks1);
        DES_set_key((C_Block *)key2,&ks2);
        DES_set_key((C_Block *)key3,&ks3);
    
        for (i=0; i<7; i++) {
            printf("0x%02X, ", in[i]);
        }
        printf("0x%02X\n", in[i]);
    
        DES_ecb3_encrypt((C_Block *)in,(C_Block *)out,&ks1,&ks2,&ks3, DES_ENCRYPT);
    
        for (i=0; i<7; i++) {
            printf("0x%02X, ", out[i]);
        }
        printf("0x%02X\n", out[i]);
    
        return 0;
    }
    My output.
    Code:
    0x05, 0x92, 0x78, 0x9F, 0xFF, 0xED, 0xCB, 0xA9
    0x90, 0xDE, 0xBD, 0x82, 0x7C, 0xEE, 0x4F, 0xDD
    Salem likes this.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About 3DES CBC in OpenSSL
    By homoon in forum C Programming
    Replies: 3
    Last Post: 04-09-2012, 11:04 AM
  2. Replies: 1
    Last Post: 04-24-2010, 02:35 PM
  3. Need help with OpenSSL
    By Ricardo_R5 in forum C Programming
    Replies: 0
    Last Post: 05-07-2007, 07:18 PM
  4. Replies: 4
    Last Post: 09-16-2006, 08:11 PM
  5. Using OpenSSL with Dev-C++
    By Smiley10 in forum C Programming
    Replies: 2
    Last Post: 07-08-2006, 11:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21