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

C 没有填充的OpenSSL RSA加密无法正确加密,c,openssl,symbian,rsa,C,Openssl,Symbian,Rsa,我们正在尝试使用“RSA\u public\u encrypt()”方法(Symbian上的openSSL)执行RSA加密,但我们没有完全成功。 加密本身成功了,但加密文本(我们尝试将其与散列匹配)并不是它应该的样子 (在其他平台上,我们检查了这一点,现在它工作正常,我们得到了不同的值)。 我们认为这可能是由于没有以正确的格式向“RSA\u public\u encrypt()”方法提供输入 守则: #define RSA_E 0x10001L void Authenticator::Test(

我们正在尝试使用“RSA\u public\u encrypt()”方法(Symbian上的openSSL)执行RSA加密,但我们没有完全成功。 加密本身成功了,但加密文本(我们尝试将其与散列匹配)并不是它应该的样子 (在其他平台上,我们检查了这一点,现在它工作正常,我们得到了不同的值)。 我们认为这可能是由于没有以正确的格式向“RSA\u public\u 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个字符被加密(不确定这是否正确?),这可能就是原因 我们的加密文本不是它应该是的。还是有什么我们做得不对的? 那么,转换纯文本的正确方法是什么,以便对所有“明文密钥”数据进行加密

我们还尝试了一个十六进制字符串“9603cab…”,长度为256个字符,但没有成功。 因此,如果正确地转换为字节(128),是否可以使用它?如果是,转换将如何工作


非常感谢您的帮助,谢谢

看看这个文件,也许会有帮助:

谢谢,我不再从事那个项目,所以我无法测试它。你的代码是针对Symbian还是普通C++?