<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 14 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Tahoma;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman","serif";}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Calibri","sans-serif";
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri","sans-serif";}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D">I suspect the use of std::string and c_str().  Use a std::vector<char> instead.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1F497D"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif"">From:</span></b><span style="font-size:10.0pt;font-family:"Tahoma","sans-serif""> openssl-users [mailto:openssl-users-bounces@openssl.org]
<b>On Behalf Of </b>Jason Qian<br>
<b>Sent:</b> Friday, March 18, 2016 1:19 PM<br>
<b>To:</b> openssl-users@openssl.org<br>
<b>Subject:</b> [openssl-users] help on des_cblock<o:p></o:p></span></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<div>
<p class="MsoNormal" style="margin-bottom:12.0pt">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:red">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:red">-83,-113,-74,-77,109,84,88</span>,8)
<br>
<br>
<o:p></o:p></p>
</div>
<p class="MsoNormal">Thanks<o:p></o:p></p>
</div>
<p class="MsoNormal">Jason<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal"><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:#E69138">  //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>
}<o:p></o:p></p>
</div>
</div>
</div>
</div>
</body>
</html>