试图从java x509键转换为PHP和解码字符串

时间:2013-08-09 19:59:18

标签: java php openssl x509 phpseclib

任何人都可以帮助将代码从java转换为php?阅读更多信息..

所以..我得到了这段代码..我正在尝试转换为php(5. {3,4,5} ..无所谓..我只是不想运行tomcat和东西)

  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);
      }
  }

当我读到它时,它从x509获取public_key并根据x509规范使用public_key对其获取的字符串进行解码

我一直在尝试这样的PHP,没有运气......

$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);

大喊

  

PHP警告:openssl_public_decrypt():key参数无效   公钥

也尝试使用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);

哪个叫喊

  

openssl_public_decrypt():此PHP版本不支持密钥类型!

就像这样:

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);

哪个没什么叫喊......还有:

$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():提供的参数无法强制转换为X509证书!

我没有想法......

任何人都可以帮助将代码从java转换为php吗?

任何想法..非常感谢。如果已经回答了请不要拍摄(我确实谷歌并找到信息,但没有运气试图申请。提前致谢。

0 个答案:

没有答案