|
Hi,
I'm using the latest (0.9.7) Crypto lib to encrypt
a string of clear text, by using CBC mode with IV preset to 8 bytes of
0x00.
When the encrypted text was decrypted back to plain
text, the first 8 bytes are bad text. I believe there are something to do
with the IV parameter, or I call the functions incorrectly. Please
help!!
Here are the source code and the output:
=============================================
#include <iostream.h>
#include <stdio.h> #include <openssl/des.h> #include "HubUtil.h"
int main() {
char mKey[8];
mKey[0] = 0xb1; mKey[1] = 0xa5; mKey[2] = 0x38; mKey[3] = 0x58; mKey[4] = 0xb2; mKey[5] = 0x60; mKey[6] = 0xd7; mKey[7] = 0x38; char myIV[8];
myIV[0] = 0x00; myIV[1] = 0x00; myIV[2] = 0x00; myIV[3] = 0x00; myIV[4] = 0x00; myIV[5] = 0x00; myIV[6] = 0x00; myIV[7] = 0x00; DES_cblock cbKey;
DES_cblock cbIV; DES_key_schedule kSchedule; char inS[] = "Hello c++ World!!!!";
unsigned char input[19]; int i;
for(i=0; i<strlen(inS); i++) { input[i] = (unsigned) inS[i]; } char inHex[39];
HubUtil::bytesToHex(input, inHex, 19); cout << "Input hex: " << inHex
<< endl;
unsigned char output[24];
DES_string_to_key(mKey, &cbKey);
DES_string_to_key(myIV, &cbIV); DES_set_key_unchecked(&cbKey, &kSchedule); DES_ncbc_encrypt(input, output, 19, &kSchedule, &cbIV, 1); cout << "In : " << input <<
endl;
unsigned char output2[19];
DES_ncbc_encrypt(output, output2, 24, &kSchedule, &cbIV, 0); char outHex[39];
HubUtil::bytesToHex(output2, outHex, 19); cout << "Decrypted hex: " << outHex
<< endl;
return 0;
} =============================================
byteToHex - input char in decimal value: H
byteToHex - input char in decimal value: e byteToHex - input char in decimal value: l byteToHex - input char in decimal value: l byteToHex - input char in decimal value: o byteToHex - input char in decimal value: byteToHex - input char in decimal value: c byteToHex - input char in decimal value: + byteToHex - input char in decimal value: + byteToHex - input char in decimal value: byteToHex - input char in decimal value: W byteToHex - input char in decimal value: o byteToHex - input char in decimal value: r byteToHex - input char in decimal value: l byteToHex - input char in decimal value: d byteToHex - input char in decimal value: ! byteToHex - input char in decimal value: ! byteToHex - input char in decimal value: ! byteToHex - input char in decimal value: ! Input hex: 48656C6C6F20632B2B20576F726C6421212121 In : Hello c++ World!!!! byteToHex - input char in decimal value: � byteToHex - input char in decimal value: � byteToHex - input char in decimal value: ( byteToHex - input char in decimal value: e byteToHex - input char in decimal value: ) byteToHex - input char in decimal value: ^ byteToHex - input char in decimal value: E byteToHex - input char in decimal value: � byteToHex - input char in decimal value: + byteToHex - input char in decimal value: byteToHex - input char in decimal value: W byteToHex - input char in decimal value: o byteToHex - input char in decimal value: r byteToHex - input char in decimal value: l byteToHex - input char in decimal value: d byteToHex - input char in decimal value: ! byteToHex - input char in decimal value: ! byteToHex - input char in decimal value: ! byteToHex - input char in decimal value: ! Decrypted hex: ACA62865295E45F32B20576F726C6421212121 =============================================
Ken Ho
|
