<div dir="ltr"><div>Thanks,<br></div>Jason<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Mar 18, 2016 at 4:23 PM, Scott Neugroschl <span dir="ltr"><<a href="mailto:scott_n@xypro.com" target="_blank">scott_n@xypro.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">





<div link="blue" vlink="purple" lang="EN-US">
<div>
<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.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></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:<a href="mailto:openssl-users-bounces@openssl.org" target="_blank">openssl-users-bounces@openssl.org</a>]
<b>On Behalf Of </b>Jason Qian<br>
<b>Sent:</b> Friday, March 18, 2016 1:19 PM<br>
<b>To:</b> <a href="mailto:openssl-users@openssl.org" target="_blank">openssl-users@openssl.org</a><br>
<b>Subject:</b> [openssl-users] help on des_cblock<u></u><u></u></span></p>
<p class="MsoNormal"><u></u> <u></u></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>
<u></u><u></u></p>
</div>
<p class="MsoNormal">Thanks<u></u><u></u></p>
</div>
<p class="MsoNormal">Jason<u></u><u></u></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>
}<u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>

<br>--<br>
openssl-users mailing list<br>
To unsubscribe: <a href="https://mta.openssl.org/mailman/listinfo/openssl-users" rel="noreferrer" target="_blank">https://mta.openssl.org/mailman/listinfo/openssl-users</a><br>
<br></blockquote></div><br></div>