我正在尝试使用 openssl 提出一种单行解决方案,它将接收消息的填充 SHA256 摘要(在本例中为 256 字节,对于 RSA2048),并将 RSA“解密”应用于 256 字节摘要,即
m^d mod n
其中 n 是模数,d 是私有(private)指数,m 是摘要消息(已填充)。这是普通 RSA2048 身份验证中通常会发生的事情,但我需要逐步完成。
我尝试了这个,但没有成功。
openssl rsautl -decrypt -in msg.sha256 -inkey secret.pem -out msg.sha256.sig
RSA operation error
2675740:error:04065084:rsa routines:RSA_EAY_PRIVATE_DECRYPT:data too large for
modulus:rsa_eay.c:532:
希望我混淆了加密和解密(因为我想在计算中使用私有(private)指数)我也尝试过这个
openssl rsautl -encrypt -in msg.sha256 -inkey secret.pem -out msg.sha256.sig
RSA operation error
2675740:error:0406D06E:rsa routines:RSA_padding_add_PKCS1_type_2:data too large
for key size:rsa_pk1.c:151:
阅读 RSA“加密/解密”,在这种情况下,我认为我将文件加密/解密与单消息加密/解密混淆了。文件加密显然是一个更高级别的协议(protocol),涉及 AES128 等。我不想那样,我只想用私有(private)指数进行模幂运算。
我还尝试了pkeyutil
(据我所知,这是具有相同结果的首选方法)。我很确定我走错了路,也许从命令行这是不可能的(我知道使用“bignum”从 C API 是可行的。)
--- 更新 ----
我尝试从 SHA256 哈希中删除 PKCS1 v1.5 填充(因此我只剩下 32 字节的哈希),然后将其传递过去,但这也不起作用。
$ head -32c msg.sha256 | openssl.exe rsautl -decrypt -inkey secret.pem -out test.sig
RSA operation error
2675740:error:0407106B:rsa routines:RSA_padding_check_PKCS1_type_2:block type is
not 02:rsa_pk1.c:190:
2675740:error:04065072:rsa routines:RSA_EAY_PRIVATE_DECRYPT:padding check failed
:rsa_eay.c:616:
我对 -raw
进行了相同的尝试,并且没有错误地完成,但结果不是我所期望的(这是我所期望的,因为它没有 PKCS1v1.5 填充)。然后我尝试反转输入文件的字节顺序,因为 openssl 似乎在提示输入文件(作为 256 字节 BIGNUM)比模数(作为 256 字节 BIGNUM)稍大这一事实,并且如果您反转字节顺序,则尾随 0 变为前导 0,并且它更小。完成了,但结果仍然不正确。
$ perl -e '$a = `cat msg.sha256`; print "".reverse($a);' > msg.sha256r
$ head -256c msg.sha256r | openssl.exe rsautl -encrypt -raw -inkey secret.pem -out test3e.sig
$ head -256c msg.sha256r | openssl.exe rsautl -decrypt -raw -inkey secret.pem -out test3d.sig
是的,我意识到我只是在黑暗中跌跌撞撞,但你会惊讶地发现我有多少次这样幸运......
test3e.sig 和 test3d.sig 都不符合我通过第三方工具进行的计算。
最佳答案
您收到的错误,data too large for modulus
,是因为你输入了第二个if
以下代码:
/* make data into a big number */
if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
if (BN_ucmp(f, rsa->n) >= 0)
{
RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
goto err;
}
BN_bin2bn
转换您的数据 from
(无符号字符*)到bignum f
(大数)。
BN_ucmp
然后比较 bignum f
和模数n
。 >=0
意味着f>=n
.
由于您的输入是 SHA256 摘要(256 字节,2048 位),因此转换后的摘要的值可能大于 2048 位模 n
,因此出现错误。
如果您可以将消息修改为更短的消息,这似乎是可行的。此外,您调用的函数在计算 f^d mod n
后会进行填充如以下行
if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
rsa->_method_mod_n))
您可能想切换到RSA_NO_PADDING
模式以获得预期的答案。
关于openssl - 使用命令行 openssl 执行填充 SHA256 哈希的 RSA "encryption"的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15886012/