In message <[EMAIL PROTECTED]> on Fri, 14 Feb 2003 21:18:45
+0100 (MET), " via RT" <[EMAIL PROTECTED]> said:
rt>
rt> Hi,
rt>
rt> I'm using the latest (0.9.7) Crypto lib to encrypt a string of clear
rt> text, by using CBC mode with IV preset to 8 bytes of 0x00.
rt> When the encrypted text was decrypted back to plain text, the first 8
rt> bytes are bad text. I believe there are something to do with the IV
rt> parameter, or I call the functions incorrectly. Please help!!
rt>
rt> Here are the source code and the output:
rt> =============================================
rt>
rt> #include <iostream.h>
rt> #include <stdio.h>
rt> #include <openssl/des.h>
rt>
rt> #include "HubUtil.h"
rt>
rt> int main() {
rt>
rt> char mKey[8];
rt> mKey[0] = 0xb1;
rt> mKey[1] = 0xa5;
rt> mKey[2] = 0x38;
rt> mKey[3] = 0x58;
rt> mKey[4] = 0xb2;
rt> mKey[5] = 0x60;
rt> mKey[6] = 0xd7;
rt> mKey[7] = 0x38;
rt>
rt> char myIV[8];
rt> myIV[0] = 0x00;
rt> myIV[1] = 0x00;
rt> myIV[2] = 0x00;
rt> myIV[3] = 0x00;
rt> myIV[4] = 0x00;
rt> myIV[5] = 0x00;
rt> myIV[6] = 0x00;
rt> myIV[7] = 0x00;
rt>
rt> DES_cblock cbKey;
rt> DES_cblock cbIV;
rt> DES_key_schedule kSchedule;
rt>
rt> char inS[] = "Hello c++ World!!!!";
rt> unsigned char input[19];
rt>
rt> int i;
rt> for(i=0; i<strlen(inS); i++) {
rt> input[i] = (unsigned) inS[i];
rt> }
rt>
rt> char inHex[39];
rt> HubUtil::bytesToHex(input, inHex, 19);
rt>
rt> cout << "Input hex: " << inHex << endl;
rt>
rt> unsigned char output[24];
rt>
rt> DES_string_to_key(mKey, &cbKey);
rt> DES_string_to_key(myIV, &cbIV);
rt> DES_set_key_unchecked(&cbKey, &kSchedule);
rt> DES_ncbc_encrypt(input, output, 19, &kSchedule, &cbIV, 1);
rt>
rt> cout << "In : " << input << endl;
rt>
You need to do this here:
DES_string_to_key(myIV, &cbIV);
rt> unsigned char output2[19];
rt> DES_ncbc_encrypt(output, output2, 24, &kSchedule, &cbIV, 0);
rt>
rt> char outHex[39];
rt> HubUtil::bytesToHex(output2, outHex, 19);
rt>
rt> cout << "Decrypted hex: " << outHex << endl;
rt>
rt> return 0;
You problem was that you didn't reinitialise the IV.
--
Richard Levitte \ Spannv�gen 38, II \ [EMAIL PROTECTED]
Redakteur@Stacken \ S-168 35 BROMMA \ T: +46-8-26 52 47
\ SWEDEN \ or +46-708-26 53 44
Procurator Odiosus Ex Infernis -- [EMAIL PROTECTED]
Member of the OpenSSL development team: http://www.openssl.org/
Unsolicited commercial email is subject to an archival fee of $400.
See <http://www.stacken.kth.se/~levitte/mail/> for more info.
______________________________________________________________________
OpenSSL Project http://www.openssl.org
Development Mailing List [EMAIL PROTECTED]
Automated List Manager [EMAIL PROTECTED]