I have an exponend in base64 encoding I use to encrypt a 128 bit AES key.

First, set the modulus 'n':

char *n64 = ".base64 data.......";

char *e64= "AQAB"; // aka 0x010001

byte nbin[256];

// decode n64 into nbin, length nlen

mpbsetbin(&n, nbin, nlen);

// decode e64 into ebin, length elen

mpnsetbin(&e, ebin, elen);

here, I encrypt "plain" (16 bytes buffer) into crypted (256 byte buffer)

mpnumber plaintext, ciphertext;

mpnzero(&ciphertext);

mpnsetbin(&plaintext, plain, len);

rsapub(&n, &e, &plaintext, &ciphertext);

// Do I need to use i2osp to get the ciphertext as a byte* array?

i2osp(crypted, 256, ciphertext.data, ciphertext.size);

mpnfree(&plaintext);

mpnfree(&ciphertext);

// base64 encode and send the ciphertext over the network to the remote end

On the remote decrypting end, the decrypted plaintext is completely wrong. I am running this on an x86, so the machine is little endian.

I must be doing something wrong, since the following code using OpenSSL produces a ciphertext that the remote end accepts as valid.

RSA * rsa=RSA_new();

// base64 decode the modulus into a buffer

// use BN_bin2bn to convert the modulus from a buffer to a BIGNUM

rsa->n=BN_bin2bn(modules,outlen,NULL);

rsa->e=BN_bin2bn(exponent,outlen,NULL);

size=RSA_public_encrypt(len, text, res, rsa, RSA_PKCS1_OAEP_PADDING);

Any suggestions?

Eric Bellotti