没有填充的OpenSSL RSA加密无法正确加密

我们尝试使用“RSA_public_encrypt()”方法(Symbian上的openSSL)执行RSA加密,但我们并没有取得成功。 加密本身成功,但加密文本(我们尝试匹配哈希)不是它应该是的(在其他平台上,我们检查了这是我们现在它正常工作,我们在这里得到不同的值)。 我们认为这可能是由于输入没有以正确的格式提供给“RSA_public_encrypt()”方法。

代码:

#define RSA_E 0x10001L void Authenticator::Test(TDesC8& aSignature, TDesC8& aMessage) { RSA *rsa; char *plainkey; char *cipherkey; int buffSize; // create the public key BIGNUM * bn_mod = BN_new(); BIGNUM * bn_exp = BN_new(); const char* modulus2 = "137...859"; // 309 digits // Convert to BIGNUM int len = BN_dec2bn(&bn_mod, modulus2); // with 309 digits -> gives maxSize 128 (OK!) BN_set_word(bn_exp, RSA_E); rsa = RSA_new(); // Create a new RSA key rsa->n = bn_mod; // Assign in the values rsa->e = bn_exp; rsa->d = NULL; rsa->p = NULL; rsa->q = NULL; int maxSize = RSA_size(rsa); // Find the length of the cipher text // maxSize is 128 bytes (1024 bits) // session key received from server side, what format should this be in ??? plainkey = "105...205"; // 309 digits cipherkey = (char *)malloc(maxSize); memset(cipherkey, 0, maxSize); if (rsa) { buffSize = RSA_public_encrypt(maxSize, (unsigned char *) plainkey, (unsigned char *) cipherkey, rsa, RSA_NO_PADDING); unsigned long err = ERR_get_error(); free(cipherkey); free(plainkey); } /* Free */ if (rsa) RSA_free(rsa); 

}

我们有一个普通的密钥,它是309个字符,但是maxSize是128个字节(RSA_NO_PADDING,所以输入必须等于模数大小),所以只有前128个字符被加密(不确定这是否正确?),可能是我们的加密文本不应该是它的原因。 或者还有其他我们没有正确做的事情? 那么转换我们的纯文本的正确方法是什么,所以’plainkey’的所有数据都被加密了?

我们还尝试使用hex字符串’9603cab …’,这个字符串长256个字符,但没有成功。 因此,如果正确转换为字节(128)可以使用它,如果是,那么转换将如何工作?

非常感谢任何帮助,谢谢!

看看这个文件,也许有帮助: https : //gist.github.com/850935