使用java代码生成的公钥的格式

使用java代码生成的公钥的格式,java,php,encryption,x509,phpseclib,Java,Php,Encryption,X509,Phpseclib,我已从客户端获得证书公共文件,其格式如下: 3082 024d 3082 01b6 a003 0201 0202 0453 3d78 5830 0d06 092a 8648 86f7 0d01 0104 0500 306b 310b 3009 0603 5504 0613 0255 5331 0b30 0906 0355 0408 1302 4e4a 3111 300f 0603 5504 0713 0853 6f6d 6572 7365 7431 1030 0e06 0355 040a 130

我已从客户端获得证书公共文件,其格式如下:

3082 024d 3082 01b6 a003 0201 0202 0453
3d78 5830 0d06 092a 8648 86f7 0d01 0104
0500 306b 310b 3009 0603 5504 0613 0255
5331 0b30 0906 0355 0408 1302 4e4a 3111
300f 0603 5504 0713 0853 6f6d 6572 7365
7431 1030 0e06 0355 040a 1307 4d65 744c
6966 6531 1630 1406 0355 040b 130d 496e
7374 6974 7574 696f 6e61 6c31 1230 1006
0355 0403 1309 6542 7573 696e 6573 7330
1e17 0d31 3430 3430 3331 3530 3335 325a
170d 3234 3033 3331 3135 3033 3532 5a30
6b31 0b30 0906 0355 0406 1302 5553 310b
3009 0603 5504 0813 024e 4a31 1130 0f06
0355 0407 1308 536f 6d65 7273 6574 3110
300e 0603 5504 0a13 074d 6574 4c69 6665
3116 3014 0603 5504 0b13 0d49 6e73 7469
7475 7469 6f6e 616c 3112 3010 0603 5504
0313 0965 4275 7369 6e65 7373 3081 9f30
0d06 092a 8648 86f7 0d01 0101 0500 0381
8d00 3081 8902 8181 00b8 7e8e f585 9bb9
7f08 41e7 391b 2a51 df24 c169 2957 8944
86f5 6548 09c8 b45d a6ad 4f59 4c33 47cc
886a 4db2 c4b5 b06c 7208 420a 5c62 d5e7
1ee2 62d7 1a57 0a10 d8d8 8e66 553b 98ec
6ad5 6c7e 8b64 c21a 82ca c928 e81b 3114
497c 9edd 9ce0 b821 a634 b231 b706 2c95
46df e56f d7c2 ff96 a3e5 48aa 525f b1b6
dfeb 7b1b 76d6 0c5b f302 0301 0001 300d
0609 2a86 4886 f70d 0101 0405 0003 8181
0020 6cc4 ca93 1898 1833 650e 9029 e57c
9357 193f e4c8 37fb 97a2 6140 586a b4f6
71c4 115b 7c64 da1d 344b 0028 4c63 d321
4c60 6519 d7a7 4ac4 79a3 438b 3290 22a8
a8d1 747b fa8e b5bd 606b 0e2d 2aad 61e1
4cba 7ffe 06dc a85b 326c 0cd0 7b42 2d7b
a7c6 3cc4 da51 1eb0 dcd8 35e0 9fd9 d6ea
78dc 1eda 5055 9844 96bd 840b 804f c106
51
根据他们的指南,我使用以下java代码转换了此密钥:

public static void encodeCertFile (String fileInput, String fileOutput) throws Exception {

    KeyFactory keyFactory = KeyFactory.getInstance("RSA");

    // create a client public key object from the client Certificate
    FileInputStream fis = new FileInputStream(fileInput);

    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    java.security.cert.Certificate cert = cf.generateCertificate(fis);

    RSAPublicKey publicKey =(RSAPublicKey) keyFactory.translateKey(cert.getPublicKey());

    byte[] binaryKey = publicKey.getEncoded();

    FileOutputStream fos = new FileOutputStream(fileOutput);

    fos.write(encoder.encode(binaryKey).getBytes());

    fos.flush();
    fos.close();

}
此代码后的最终键格式:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4fo71hZu5fwhB5zkbKlHfJMFpKVeJRIb1ZUgJyLRdpq1PWUwzR8yIak2yxLWwbHIIQgpcYtXnHuJi1xpXChDY2I5mVTuY7GrVbH6LZMIagsrJKOgbMRRJfJ7dnOC4IaY0sjG3BiyVRt/lb9fC/5aj5UiqUl+xtt/rext21gxb8wIDAQAB
现在,我想知道这个密钥的格式,因为我需要在PHP代码RSA加密中使用它

有人能帮我吗

编辑:

在PHP中,Crypt_RSA对象中的加载后密钥将为:

Array ( [modulus] => Math_BigInteger Object ( [value] => 129556351703302235097294578633499498214321346771419165226567447673642587371674457167887773329525082335024048008203150388684301609247024871301530851141306484720411967514496781667235409323971108893240957281328168302000493894326395451998673830109545777451356722603427467636518443157123562165966550902323389684723 [is_negative] => [generator] => mt_rand [precision] => -1 [bitmask] => [hex] => ) [privateExponent] => Math_BigInteger Object ( [value] => 65537 [is_negative] => [generator] => mt_rand [precision] => -1 [bitmask] => [hex] => ) )
我们如何使用此密钥加密数据? 我的PHP代码:

$rsa = new Crypt_RSA();
try{
// $rsa->loadKey(file_get_contents('/var/www/git/ta_client/Keys_4Apr/Public.txt'));
$rsa->loadKey(base64_decode(file_get_contents($configValues['metPubKey']))); // private key
// $rsa->setSignatureMode(CRYPT_RSA_ENCRYPTION_PKCS1);
// $rsa->setHash('md5');
// $rsa->setPublicKeyFormat(CRYPT_RSA_ENCRYPTION_PKCS1);
//$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
//$rsa->setPublicKeyFormat(CRYPT_RSA_PUBLIC_FORMAT_PKCS1);
$encrypted = $rsa->encrypt($token);
$encryptedToken = base64_encode($encrypted)
RSAPublicKey.getEncoded返回ASN.1 DER编码公钥。
但是,您可能希望使用获取公钥模数,并将其直接插入PHP Crypt_RSA对象。您可以将私有指数设为常数65537。几乎没有什么不同。

这就是你想要的吗

<?php
$a = '3082 024d 3082 01b6 a003 0201 0202 0453
3d78 5830 0d06 092a 8648 86f7 0d01 0104
0500 306b 310b 3009 0603 5504 0613 0255
5331 0b30 0906 0355 0408 1302 4e4a 3111
300f 0603 5504 0713 0853 6f6d 6572 7365
7431 1030 0e06 0355 040a 1307 4d65 744c
6966 6531 1630 1406 0355 040b 130d 496e
7374 6974 7574 696f 6e61 6c31 1230 1006
0355 0403 1309 6542 7573 696e 6573 7330
1e17 0d31 3430 3430 3331 3530 3335 325a
170d 3234 3033 3331 3135 3033 3532 5a30
6b31 0b30 0906 0355 0406 1302 5553 310b
3009 0603 5504 0813 024e 4a31 1130 0f06
0355 0407 1308 536f 6d65 7273 6574 3110
300e 0603 5504 0a13 074d 6574 4c69 6665
3116 3014 0603 5504 0b13 0d49 6e73 7469
7475 7469 6f6e 616c 3112 3010 0603 5504
0313 0965 4275 7369 6e65 7373 3081 9f30
0d06 092a 8648 86f7 0d01 0101 0500 0381
8d00 3081 8902 8181 00b8 7e8e f585 9bb9
7f08 41e7 391b 2a51 df24 c169 2957 8944
86f5 6548 09c8 b45d a6ad 4f59 4c33 47cc
886a 4db2 c4b5 b06c 7208 420a 5c62 d5e7
1ee2 62d7 1a57 0a10 d8d8 8e66 553b 98ec
6ad5 6c7e 8b64 c21a 82ca c928 e81b 3114
497c 9edd 9ce0 b821 a634 b231 b706 2c95
46df e56f d7c2 ff96 a3e5 48aa 525f b1b6
dfeb 7b1b 76d6 0c5b f302 0301 0001 300d
0609 2a86 4886 f70d 0101 0405 0003 8181
0020 6cc4 ca93 1898 1833 650e 9029 e57c
9357 193f e4c8 37fb 97a2 6140 586a b4f6
71c4 115b 7c64 da1d 344b 0028 4c63 d321
4c60 6519 d7a7 4ac4 79a3 438b 3290 22a8
a8d1 747b fa8e b5bd 606b 0e2d 2aad 61e1
4cba 7ffe 06dc a85b 326c 0cd0 7b42 2d7b
a7c6 3cc4 da51 1eb0 dcd8 35e0 9fd9 d6ea
78dc 1eda 5055 9844 96bd 840b 804f c106
51';

$a = str_replace(array(' ', "\r", "\n"), '', $a);
$a = pack('H*', $a);
$a = "-----BEGIN CERTIFICATE-----\r\n" .
     base64_encode($a) .
     "\r\n-----END CERTIFICATE-----";

include('File/X509.php');

$x509 = new File_x509();
$x509->loadX509($a);
$rsa = $x509->getPublicKey();

echo bin2hex($rsa->encrypt('test'));

米格玛。。。字符串显然是Base64编码的值,所以您需要对其进行Base64解码。解码后,我们可以确定密钥是否为ASN.1、der/ber、pkcs等?3082 024d。。。告诉您它的ASN.1 DER编码,但它的十六进制编码。30是ASN.1序列,82是ASN.1长度,0x24d是序列的长度,等等。如果这是您所期望的,那么公钥就在那里的某个地方。将其从十六进制转换为二进制,然后使用Gutmann's dumpasn1之类的工具打印。