<div dir="ltr"><div><div>I am new on openSSl and run  into a issue need some help.<br><br><br>In our application, the client and server perform a Diffie Hellman Key exchange and then encrypt the data  The client is written in C++(using openSSL), and server is in java. <br><br> Most of time, it is running correctly, but occasionally the server(java) throw a  "Given final block not properly padded" exception.<br><br>I added more log on the both side. When the exception happen,  the keys are offset by one(for the working case, they are the same) <br><br><br>Server -- java  get from getEncoded()<br><br>DES Key  size (8)    (1,-<span style="background-color:rgb(255,0,0)">83,-113,-74,-77,109,84,88</span>)<br><br>Client -- openSSL  get from des_cblock struct<br><br>DES Key  size (8)   (<span style="background-color:rgb(255,0,0)">-83,-113,-74,-77,109,84,88</span>,8) <br><br><br></div>Thanks<br></div>Jason<br><div><div><br>Here is the C++ code<br><br>void DiffieHellmanCipher::init(const std::string &Y){<br>    if (Y.length() == 0) {<br>        return;<br>    }<br>    if (m_DH == NULL) {<br>        return;<br>    }<br><br>    // convert the Y to BIGNUM<br>    BIGNUM *bnY = NULL;<br>    // Memory for bnY is allocated in BN_dec2bn call.<br>    if (!BN_dec2bn(&bnY, Y.c_str())) {<br>        if (bnY)<br>            BN_free(bnY);<br>        printf("Could not convert Diffie-Hellman Y value to BIGNUM");<br>    }<br><br>    // compute the secret key<br>    int dhSize = DH_size(m_DH);<br>    unsigned char *secretKey = (unsigned char*) new char[dhSize + 1];<br>    int secretKeyLen = DH_compute_key(secretKey, bnY, m_DH);<br>    BN_free(bnY);<br><br>    if (secretKeyLen < 8) {<br>        delete [] secretKey;<br>        printf("Error computing secret key: key length is too short");<br>    }<br><br>    // convert from raw form to odd parity DES key<br>    des_cblock desKey;<br>    memcpy(desKey, secretKey, 8);<br>    delete [] secretKey;<br>    DES_set_odd_parity(&desKey);<br><br>    <span style="background-color:rgb(230,145,56)">  //just print out des_cblock<br>    secretKeyString="(";<br>    char ch[10]="\0";<br>    for(int i=0;i<8;i++){<br>        sprintf(ch,"%d",(char)desKey[i]);<br>      secretKeyString+=ch;<br>      if(i != 7){<br>        secretKeyString+=",";<br>      }<br>    }<br>    secretKeyString+=")";<br></span><br><br>    int skRet;<br>    if ((skRet = DES_set_key(&desKey, &m_DESKey)) != 0) {<br>        delete [] secretKey;<br>        printf("Error computing secret key: generated key is weak");<br>    }<br><br>    m_bInited = true;<br>}<br></div></div></div>