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

标签 java php encryption x509 phpseclib

我从客户那里获得了以下格式的证书公共(public)文件:

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 代码转换了此 key :

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

}

此代码后的最终 key 格式:

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4fo71hZu5fwhB5zkbKlHfJMFpKVeJRIb1ZUgJyLRdpq1PWUwzR8yIak2yxLWwbHIIQgpcYtXnHuJi1xpXChDY2I5mVTuY7GrVbH6LZMIagsrJKOgbMRRJfJ7dnOC4IaY0sjG3BiyVRt/lb9fC/5aj5UiqUl+xtt/rext21gxb8wIDAQAB

现在,我想知道这个 key 的格式,因为我需要在我的 PHP 代码 RSA 加密中使用它。

有人可以帮我吗?

已编辑:

在 PHP 中,Crypt_RSA 对象中的 loadKey 之后将是:

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] => ) )

我们如何使用这个 key 来加密数据? 我的 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)

最佳答案

这符合你的要求吗?:

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

关于java - 使用java代码生成的公钥的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22988114/

相关文章:

java - Java 中持久的客户端-服务器套接字连接

java - 如何获取Jsoup中特定html元素的内容?

Spring Config Server "="符号加密问题

encryption - 为什么更改三重 DES key 或初始值中的一位不会给出不同的加密数据?

java - 解密 AES/CBC/PKCS5Padding 错误 : Given final block not properly padded

java - 如何使Spring Web Flow中的 session 失效?

java - 如何获取动态报表的页码?

php - 作为 0000-00-00 插入日期列

php - 获取异常的 'friendly' 错误消息

php - pdo GROUP BY 查询不返回所有值