2
votes

Anyone can help convert that code from java to php ? Read more below..

So .. I got this piece of code .. I'm trying to convert to php ( 5.{3,4,5} .. doesn't matter .. I just don't want to run tomcat and stuff )

  private static byte[] cAGLT(String lC){
      try {
          PublicKey PUBLIC_KEY;
          String key = "MIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9E.............long 593 char stuff here";
          KeyFactory keyFactory = KeyFactory.getInstance("DSA");
          PUBLIC_KEY = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decodeBase64(key.getBytes())));
          byte[] decodedBytes = Base64.decodeBase64(lC.getBytes());
          ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);
          DataInputStream dIn = new DataInputStream(in);
          int textLength = dIn.readInt();
          byte[] lT = new byte[textLength];
          dIn.read(lT);
          byte[] hash = new byte[dIn.available()];
          dIn.read(hash);
          try{
              Signature signature = Signature.getInstance("SHA1withDSA");
              signature.initVerify(PUBLIC_KEY);
              signature.update(lT);
              try{
                  signature.verify(hash);
              } catch (Exception e) {
                    throw e;
              }
          }catch (Exception e){
              throw e;
          }
          return lT;
      }catch (Exception e){
          throw new Error(e);
      }
  }

As I read it, it gets the public_key from the x509 and decodes the string it gets using a public_key as per x509 spec

I've been trying like this in php with no luck ...

$key = "MIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9E.............long 593 char stuff here";
$key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($key, 64, "\n", true) . "-----END PUBLIC KEY-----"; // no \n before the ----END because it already has one
$key = openssl_pkey_get_public($key);
$key = openssl_pkey_get_details($key);
$key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap(base64_encode($key['dsa']['pub_key']), 64, "\n", true)."\n-----END PUBLIC KEY-----";
        $data = null;
        openssl_public_decrypt($todecode, $data, $key);

which yells

PHP Warning: openssl_public_decrypt(): key parameter is not a valid public key

Also tried like this with phpseclib ( http://phpseclib.sourceforge.net/pear.htm )

$key = "MIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9E.............long 593 char stuff here";
$key = "-----BEGIN PUBLIC KEY-----\n" . wordwrap($key, 64, "\n", true) . "-----END PUBLIC KEY-----";
$data = null;
openssl_public_decrypt($licenseContent, $data, $key);

Which yells

openssl_public_decrypt(): key type not supported in this PHP build!

And like this:

include('BigInteger.php');
include('ASN1.php');
include('X509.php');
$key = "MIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9E.............long 593 char stuff here";
$key = "-----BEGIN CERTIFICATE-----\n" . wordwrap($key, 64, "\n", true) . "-----END CERTIFICATE-----";
$x509 = new File_X509();
$cert = $x509->loadX509($key);
print_r($cert);

Which yells NOTHING... also:

$key = "MIIBuDCCASwGByqGSM44BAEwggEfAoGBAP1/U4EddRIpUt9KnC7s5Of2EbdSPO9E.............long 593 char stuff here";
$key = "-----BEGIN CERTIFICATE-----\n" . wordwrap($key, 64, "\n", true) . "-----END CERTIFICATE-----";
echo openssl_x509_read($key);

openssl_x509_read(): supplied parameter cannot be coerced into an X509 certificate!

I'm out of ideas...

Anyone can help convert that code from java to php ?

Any ideas .. greatly appreciated. Please don't shoot if this has already been answered ( I did google and found info but no luck trying to apply. Thanks in advance.

Anyone can help convert that code from java to php ?Adrian Sandu
I thought this question was perfectly clear. Long story short, from the snippet of the alleged X.509 cert that you've posted I can tell you that it isn't actually an X.509 cert. More info: pastebin.com/65kauL6Sneubert
Ok, so it's a dsa public key. Great. Now, I wish I'd know how to use it to simulate that function in php :| So far no luck .. I don't understand exactly what that function does now since I just understood dsa is used for signing ...Adrian Sandu
Using PHP's OpenSSL bindings what you'd probably need to do is to create an X.509 cert from the public key by creating a CSR out of the public key. Once that's done you could use the signature verification functions. See phpseclib.sourceforge.net/interop.html# (click on private_encrypt / public_decrypt on left and then go to the decrypt with openssl tab)neubert
By the way, forgot to thanks a ton for the very good answers ! Now .. Wouldn't one need a private key too to generate the CSR ?Adrian Sandu