I have tried to use crypto++ and openssl 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++ and openssl is:
0xa7, 0x44, 0x24, 0x6e, ...

As you can see the first byte is the same but the following ones are 
not. I am by no means an expert on neither mcrypt nor cryptography but 
AFAIK using cfb should result in the same result so either I am doing 
something wrong with mcrypt or something is wrong.

// Michael Medin

The code I use is the following:

#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>
#include <openssl/ssl.h>
#include <openssl/err.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() {
    print_it("plain", plain, sizeof(plain));
    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("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));
}
void testOpenSSL() {
    print_it("plain", plain, sizeof(plain));
    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,ctout);
    EVP_CIPHER_CTX_cleanup(&ctx);

}


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

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Mcrypt-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mcrypt-users

Reply via email to