我不是加密方面的专家,我花了好几个小时搜索互联网和stackoverflow.com寻求答案,但我仍然陷入困境。 我的问题是,我有公钥模数和公钥指数,两者都给出了。有了这些,我需要加密消息。
示例:
MOD:
CA498337FDD3D7C5487DBE5101899309B51B9B4708E647F85CC599A6C96ADFF62D7CD6A184DF346A3F707E7A34C5853ABA9030C65773AF604C59B8ED2E78D869F26B57E03CA9D0D45C67B8791AB010482224D108FCE20B515D8B1904B4DB41D0003950245E2382CA62477727E543850BC4FD4235041A44F213A99514EF5F64BE3D7F9DEE9E383062078D4E64ED92A42B94A0466B5BC36DAC55499DEA719A38C3A0C287724F57C64507AB424E9DBCC7F93112CF38D1B5458BFCD454F4907C5A617EBCD0F79DE40BBF8971D7CF225D9425010CF5CF638EF00B2582CDE7EA41DF7D65419B4129BBD37A872372D270B537B95C2DEE078107515B8CE719D5020CF337
的 EXP:
010001
我仍然无法弄清楚如何在PHP中实现这一点。这是我到目前为止的代码(我使用的是phpseclib):
$message = "Hello World";
$rsa = new Crypt_RSA();
$rsa->loadKey(
array(
'e' => new Math_BigInteger($pubkey_exp),
'n' => new Math_BigInteger($pubkey_mod)
)
);
$rsa->setPublicKey();
$ciphertext = $rsa->encrypt($message);
但是,我没有得到任何输出。
答案 0 :(得分:4)
$pubkey_n = 'CA498337FDD3D7C5487DBE5101899309B51B9B4708E647F85CC599A6C96ADFF62D7CD6A184DF346A3F707E7A34C5853ABA9030C65773AF604C59B8ED2E78D869F26B57E03CA9D0D45C67B8791AB010482224D108FCE20B515D8B1904B4DB41D0003950245E2382CA62477727E543850BC4FD4235041A44F213A99514EF5F64BE3D7F9DEE9E383062078D4E64ED92A42B94A0466B5BC36DAC55499DEA719A38C3A0C287724F57C64507AB424E9DBCC7F93112CF38D1B5458BFCD454F4907C5A617EBCD0F79DE40BBF8971D7CF225D9425010CF5CF638EF00B2582CDE7EA41DF7D65419B4129BBD37A872372D270B537B95C2DEE078107515B8CE719D5020CF337';
$pubkey_e = '010001';
$message = "Hello World";
$rsa = new Crypt_RSA();
$r = $rsa->loadkey(
array(
'e' => new Math_BigInteger($pubkey_e, 16),
'n' => new Math_BigInteger($pubkey_n, 16)
),
CRYPT_RSA_PUBLIC_FORMAT_RAW
);
//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
var_export( $ciphertext = $rsa->encrypt($message) );
似乎正在发挥作用。