Problem with mcryp/crypto++

49 views
Skip to first unread message

mic...@gmail.com

unread,
Feb 10, 2008, 9:18:09 AM2/10/08
to Crypto++ Users
Hello,

I have tried to use crypto++ to get the same result as mcrypt for a
simple CFB crypto. The reason is I am trying to write a new client for
an existing server that use mcrypt, and mcrypt is not "easily"
available for windows.

The result from mcrypt is:
0xa7, 0x8f, 0x88, 0x4e, ...

Where-as the result from crypto++ is:
0xa7, 0x44, 0x24, 0x6e, ...

As you can see the first byte is the same but the following ones are
not. I have seen another post on the list regarding a similar issue
but there was no solution. I am thinking it might be related to some
issues with block-size or some such but I am not an expert on
cryptography so I hope that someone here might be able to help me with
this. I have also tried this with DES/3DES with the exact same result
(ie. first char correct, the rest are not).

// Michael Medin

The code I use is:

#include <stdio.h>
#include <mcrypt.h>
#include <iostream>
#include <iomanip>
#include <sstream>

#include <crypto++/cryptlib.h>
#include <crypto++/modes.h>
#include <crypto++/des.h>
#include <crypto++/aes.h>
#include <crypto++/filters.h>
#include <mcrypt.h>

unsigned char key[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30,
0x31, 0x32}; // 12345678901234567890123456789012
unsigned char iv[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38,
0x39, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36}; // 1234567890123456
unsigned char plain[] = {0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77,
0x6f, 0x72, 0x6c, 0x64}; // hello world

void print_it(char *title, unsigned char* buff, int len) {
int i;
printf("unsigned char %s[] = {", title);
for (i=0;i<len-1;i++) {
printf("0x%x, ", (unsigned char)buff[i]);
}
printf("0x%x", (unsigned char)buff[len-1]);
printf("} // %s\n", buff);
}

void testCryptoPP() {
CryptoPP::CFB_Mode< CryptoPP::AES >::Encryption encryptor(key,
sizeof(key), iv, sizeof(iv));

unsigned char ciphertext[100];
for(int x=0;x<sizeof(plain);x++) {
encryptor.ProcessData(&ciphertext[x], (unsigned char*)&plain[x], 1);
ciphertext[x+1] = 0;
}
print_it("plain", plain, sizeof(plain));
print_it("crypto", ciphertext, sizeof(plain));
}
void testMCrypt() {
MCRYPT td;

td = mcrypt_module_open(MCRYPT_RIJNDAEL_128,NULL,"cfb",NULL);
int iv_size = mcrypt_enc_get_iv_size(td);
printf("iv size: %d (%d)\n", iv_size, sizeof(iv));

int key_size = mcrypt_enc_get_key_size(td);
printf("key size: %d (%d)\n", key_size, sizeof(key));

mcrypt_generic_init(td,key,key_size,iv);

print_it("plain", plain, sizeof(plain));
for(int x=0;x<sizeof(plain);x++)
mcrypt_generic(td,&plain[x],1);
print_it("mcrypt", plain, sizeof(plain));
}


int main(int argv, char *argc[]) {
testCryptoPP();
testMCrypt();
}

Geoff Beier

unread,
Feb 10, 2008, 12:11:20 PM2/10/08
to mic...@gmail.com, Crypto++ Users
> I have tried to use crypto++ to get the same result as mcrypt for a
> simple CFB crypto. The reason is I am trying to write a new client for
> an existing server that use mcrypt, and mcrypt is not "easily"
> available for windows.
>
> The result from mcrypt is:
> 0xa7, 0x8f, 0x88, 0x4e, ...
>
> Where-as the result from crypto++ is:
> 0xa7, 0x44, 0x24, 0x6e, ...
>
> As you can see the first byte is the same but the following ones are
> not. I have seen another post on the list regarding a similar issue
> but there was no solution. I am thinking it might be related to some
> issues with block-size or some such but I am not an expert on
> cryptography so I hope that someone here might be able to help me with
> this. I have also tried this with DES/3DES with the exact same result
> (ie. first char correct, the rest are not).
>
> // Michael Medin


FWIW, when I replace your test mcrypt function with a similar function
for OpenSSL:

void testOpenSSL() {
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx,EVP_aes_256_cfb(),0,key,iv,1);
int ctout = 1024;
unsigned char ciphertext[ctout];
EVP_CipherUpdate(&ctx,ciphertext,&ctout,plain,sizeof(plain));
print_it("openssl ciphertext",ciphertext,ctout);
EVP_CIPHER_CTX_cleanup(&ctx);
}

OpenSSL gives the same results as crypto++. So I suspect your problem
is either misuse of libmcrypt or a bug in libmcrypt. I haven't really
used that library, so I couldn't say. Is there an mcrypt mailing list?

HTH,

Geoff

StefSchultz

unread,
Feb 11, 2008, 12:18:12 AM2/11/08
to Crypto++ Users


On Feb 10, 3:18 pm, "mich...@medin.name" <mic...@gmail.com> wrote:
> Hello,
>
> I have tried to use crypto++ to get the same result as mcrypt for a
> simple CFB crypto. The reason is I am trying to write a new client for
> an existing server that use mcrypt, and mcrypt is not "easily"
> available for windows.
>
> The result from mcrypt is:
> 0xa7, 0x8f, 0x88, 0x4e, ...
>
> Where-as the result from crypto++ is:
> 0xa7, 0x44, 0x24, 0x6e, ...

Hi,

this is the same problem I had. I was able to solve it with Jeffrey's
help. Take a look into http://groups.google.com/group/cryptopp-users/t/c9c2d68f70e6763b

Best regards,
Stephen
Reply all
Reply to author
Forward
0 new messages