0

I have a RSA Public Key in this format:

OpenSSLRSAPublicKey{modulus=9ee9f82dd8429d9fa7f091c1d375b9c289bcf2c39ec57e175a2998b4bdd083465ef0fe6c7955c821b7e883929d017a9164a60290f1622f664a72096f5d2ffda7c7825c3d657c2d13d177445fa6cdd5d68b96346006a96040f5b09baae56d0c3efeaa77d57602f69018f5cefd60cb5c71b6b6f8a4b0472e8740367266917d8c13,publicExponent=10001}

So, I have the modulus and the exponent. How can I convert this in a format that is accepted by openssl_encrypt() in PHP? I have searched on Google and didn't find anything good. Isn't there an easy way to format it?

neubert
  • 15,947
  • 24
  • 120
  • 212
Introser
  • 161
  • 1
  • 12

2 Answers2

0

Assuming you mean openssl_public_encrypt() instead of openssl_encrypt(), then try this (using phpseclib 1.0):

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

$rsa = new Crypt_RSA();
$rsa->loadKey(array(
    'n' => new Math_BigInteger('9ee9f82dd8429d9fa7f091c1d375b9c289bcf2c39ec57e175a2998b4bdd083465ef0fe6c7955c821b7e883929d017a9164a60290f1622f664a72096f5d2ffda7c7825c3d657c2d13d177445fa6cdd5d68b96346006a96040f5b09baae56d0c3efeaa77d57602f69018f5cefd60cb5c71b6b6f8a4b0472e8740367266917d8c13', 16),
    'e' => new Math_BigInteger('10001', 16)
));

echo $rsa;

Altho that said, if you're gonna use phpseclib to convert the key, why not use it to encrypt whatever it is that you're trying to encrypt as well?:

//$rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
$rsa->encrypt('plaintext');
neubert
  • 15,947
  • 24
  • 120
  • 212
  • You seem to have one too many zeros in your exponent value (1048577 vs 65537), unless I'm missing something. – bartonjs Mar 05 '18 at 19:40
  • Im trying it at the moment with phpseclib 1.0 because 2.0 dont really want to work on my machine. – Introser Mar 08 '18 at 20:12
  • The problem is that I get a different encrypted String everytime I encrypt the same string with the same mod and exponent – Introser Mar 08 '18 at 20:19
  • @Introser - the ciphertext's are _supposed_ to be different each time. Quoting https://tools.ietf.org/html/rfc3447#section-7.2.1 , one of the steps is to "_generate an octet string PS of length k - mLen - 3 consisting of pseudo-randomly generated nonzero octets_". It's done to prevent known plaintext attacks. – neubert Mar 08 '18 at 20:46
  • @Introser - I've updated my post to use phpseclib 1.0 code. – neubert Mar 08 '18 at 20:48
  • Thats great, had the same Code. The Problem is, at the other end, an android app, im using rsa too. But Java always generate the same string and the result is always a 199 long string, while php generates 256 long different strings – Introser Mar 08 '18 at 21:02
  • @Introser - that's a fundamentally different question than the one you asked in this topic. I'd recommend creating a new question to ask about that. And post the code that you're using. – neubert Mar 08 '18 at 21:10
-2

No, there isn't any way to do this. Public/private keys can only be used in asymmetric cryptography. openssl_encrypt() requires symmetric cryptography.

openssl_public_encrypt() which you need for asymmetric cryptography wants a pubic key resource. This can be obtained using openssl_pkey_get_public().

Now, openssl_pkey_get_public() needs a PEM file. The docs describe it as:

  1. an X.509 certificate resource
  2. a string having the format file://path/to/file.pem. The named file must contain a PEM encoded certificate/public key (it may contain both).
  3. A PEM formatted public key.

So you need to create a PEM file from the public key configuration you have in your question. The modulus and exponent you have are enough to do this.

There are several examples on the internet on how to archive this. Have a look at:

Basically you need to create a "ASN1-formatted text file" and employ some openssl commands.

rollstuhlfahrer
  • 3,988
  • 9
  • 25
  • 38