dongyupen6269 2014-04-10 12:29
浏览 100
已采纳

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

I have got cert public file from client which is in this format :

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

According to their guide I converted this key using this java code:

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

}

Final key format after this code :

MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC4fo71hZu5fwhB5zkbKlHfJMFpKVeJRIb1ZUgJyLRdpq1PWUwzR8yIak2yxLWwbHIIQgpcYtXnHuJi1xpXChDY2I5mVTuY7GrVbH6LZMIagsrJKOgbMRRJfJ7dnOC4IaY0sjG3BiyVRt/lb9fC/5aj5UiqUl+xtt/rext21gxb8wIDAQAB

Now, I want to know format of this key as I need to use this in my PHP code RSA encryption.

Can anyone help me in this ?

Edited:

In PHP, after loadKey in Crypt_RSA Object will be:

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

How can we use this key to encrypt data? My PHP code :

$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)
  • 写回答

2条回答 默认 最新

  • dqfsbvd43312 2014-04-10 19:51
    关注

    This do what you want?:

    <?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(' ', "", "
    "), '', $a);
    $a = pack('H*', $a);
    $a = "-----BEGIN CERTIFICATE-----
    " .
         base64_encode($a) .
         "
    -----END CERTIFICATE-----";
    
    include('File/X509.php');
    
    $x509 = new File_x509();
    $x509->loadX509($a);
    $rsa = $x509->getPublicKey();
    
    echo bin2hex($rsa->encrypt('test'));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB怎么通过柱坐标变换画开口是圆形的旋转抛物面?
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿