PHP:RSA仅使用模数和指数加密

PHP:RSA仅使用模数和指数加密

问题描述:

now , i ONLY have the modulus and exponent.

How should i do to encrypt using modulus and exponent in PHP?

It costed me much time that serching the answer in internet, but all was useless.

Thanks for your time

现在,我只有模数和指数。 p>

我该怎么办? 在PHP中使用模数和指数进行加密? p>

我花了很多时间在网上搜索答案,但都没用。 p>

谢谢 你的时间 p> div>

Using phpseclib, a pure PHP RSA implementation,

<?php
include('Crypt/RSA.php');

$rsa = new Crypt_RSA(); 
$rsa->loadKey(
    array(
        'e' => new Math_BigInteger('...'),
        'n' => new Math_BigInteger('...')
    )
);

$rsa->encrypt('...');

A discussion of the various number formats supported by Math_BigInteger can be found here:

http://phpseclib.sourceforge.net/math/examples.html#constructor

Note that phpseclib follows the PKCS1 standards and pads plaintext's. Since it sounds like your RSA key isn't formated in an industry standard way I'm going to guess you're not wanting to use industry standard padding either. So what you'll want to do is this:

$plaintext = new Math_BigInteger('aaaaaa');
echo $rsa->_exponentiate($plaintext)->toBytes();

Do note that doing RSA encryption like that is quite insecure but I'll leave that consideration up to you.