Hi, Cornell. I think I see what's happening here, but if I'm right
I don't know why the decrypt works in either case.... at any rate,
my comments are embedded below. I've snipped the original
that my comments don't apply to.
> int main(int argc, char* argv[])
> {
> UCHAR plain[] = "test message";
> UCHAR out_plain[sizeof(plain)];
> UCHAR chiper[sizeof(plain)];
> UCHAR chiper2[sizeof(plain)];
> UCHAR key[DES::DEFAULT_KEYLENGTH];
> UCHAR iv[DES::BLOCKSIZE];
> unsigned bSize = strlen((char*)plain);
>
> srand(time(0));
> for(int i=0;i<DES::DEFAULT_KEYLENGTH;i++)
> key[i] = rand()%256;
> for(i=0;i<DES::BLOCKSIZE;i++)
> iv[i] = rand()%256;
Above is the first place I see a potential problem. You aren't
saving a copy of the IV for decryption use. Note that the IV is
updated with every block encrypted, changing it from its
original value. I suggest you do this:
UCHAR ivDec[DES::BLOCKSIZE];
for(i=0;i<DES::BLOCKSIZE;i++)
ivDec[i] = iv[i];
Then use ivDec to construct the decryption object. You'll need the
original IV anyway to send to the recipient or they won't be able
to decrypt the ciphertext.
> { //THIS PART RUN'S FINE!!!
> DES::Encryption desEncryption(key, DES::DEFAULT_KEYLENGTH);
> CFB_Mode_ExternalCipher::Encryption cfbEncryption(desEncryption,
> iv);
>
> DES::Encryption desDecryption(key, DES::DEFAULT_KEYLENGTH);
> CFB_Mode_ExternalCipher::Decryption cfbDecryption(desDecryption,
> iv);
I think I see it. You're declaring desDecryption as a DES::Encryption object here,
which is correct for CFB, OFB, and CTR modes.
> cfbEncryption.ProcessData(chiper, plain,bSize);
> cfbDecryption.ProcessData(out_plain, chiper,bSize);
> }
> { //THIS PART DOES NOT WORK!!!
> //even if it should be the same as previous...
> //encrypt work's ok the chiper and chiper2 is equal, but the
> out_plain is different!
>
> DES::Encryption *chpEnc = new DES::Encryption(key,
> DES::DEFAULT_KEYLENGTH);
> CFB_Mode_ExternalCipher::Encryption *cfbEncrypt
> = new CFB_Mode_ExternalCipher::Encryption(*chpEnc, iv);
/******************************************************************************/
> DES::Decryption *chpDec = new DES::Decryption(key,
> DES::DEFAULT_KEYLENGTH);
> CFB_Mode_ExternalCipher::Decryption *cfbDecrypt
> = new CFB_Mode_ExternalCipher::Decryption(*chpDec, iv);
/*******************************************************************************/
Looks like the block above is your problem. *chpDec MUST be an
encryption object for CFB, OFB, and CTR modes. In the code that works
above, you use "DES::Encryption desDecryption" to construct the
decrypter. Here you are using "DES::Decryption *chpDec" to construct it.
Change that to "DES::Encryption *chpDec" and it should work.
> cfbEncrypt->ProcessData(chiper2, plain, bSize);
> cfbDecrypt->ProcessData(out_plain, chiper2, bSize);
> delete cfbEncrypt;
> delete cfbDecrypt;
> delete chpEnc;
> delete chpDec;
> }
I hope this turns out to be the solution, but if not, at least I tried.
Rickey