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

发布于 2024-11-03 16:35:11 字数 1688 浏览 3 评论 0原文

我们尝试使用“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”的所有数据都被加密的正确方法是什么?

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

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

We're trying to perform RSA encryption using the "RSA_public_encrypt()" method (openSSL on Symbian), but we're not quite succeeding.
The encryption itself succeeds, but the encrypted text (which we try to match to a hash) isn't what it should be
(on other platforms, we checked this were we now it is working correctly and we get different values here).
We think this is probably due to the input which isn't provided in the correct format to the "RSA_public_encrypt()" method.

The code:

#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);

}

We have a plain key which is 309 characters, but the maxSize is 128 bytes (RSA_NO_PADDING, so input has to be equal to modulus size),
so only the first 128 characters are encrypted (not really sure if this is correct?), which is probably why
our encrypted text isn't what it should be. Or is there something else we don't do correctly ?
What is then the correct way of transforming our plain text so all data of 'plainkey' is encrypted ?

We also tried this with a hexadecimal string '9603cab...' which is 256 characters long, but didn't succeed.
So if correctly converted into bytes (128) could this be used and if yes, how would that conversion work?

Any help is greatly appreciated, thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

中二柚 2024-11-10 16:35:11

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

Take a look at this file, maybe it helps: https://gist.github.com/850935

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文