Skip to content

Instantly share code, notes, and snippets.

@noname007
Created December 7, 2018 11:30
Show Gist options
  • Save noname007/7da4a85ce512dc348ecf1542e5f8202f to your computer and use it in GitHub Desktop.
Save noname007/7da4a85ce512dc348ecf1542e5f8202f to your computer and use it in GitHub Desktop.
二进制流,字符串化后处理,反解回二进制流对应的 base_64_encode
<?php
$hex_str = 'e123476343f37e7ecfd8401b60897de9bcc1621e5ede9755e9c442fc9a16d41ee82e344f18135866ba1360f587346711255d8c40e5a5fb45958833f67c7487ac1ff5833716fcc6b7b0781d6fe38c1cb95f4ceeb99b17f62a927777a07103af379dc24d3e2ce0540838ee8f3549b3fb58083fbf7058d1cf6d7c15e9da03bf3709c61a79c2b2c858f327ed6755df7883c2130e9ba4b36fc366b7b72d0459c2ea326af189887ef213432e820beadc321791aa9fd708bbb3ab9c556e54beb8e15f88f07542030de04e0bbbf0537825f6d6761c919a93500099c83f8d00f7ba8b2b2e408e9f820c9390b8d1dba667c047b7adb3e3d2c3fdfd721f5998f80c5509299b';
$len = strlen($hex);
$a = [];
for($i = 0; $i < $len; $i += 2)
{
$a[] = chr((int)base_convert($hex[$i].$hex[$i + 1], 16, 10));
}
echo count($a);
$bin_str = implode('', $a);
$pkey = "-----BEGIN PUBLIC KEY-----\n".
chunk_split(base64_encode($bin_str), 64, "\n")
. "-----END PUBLIC KEY-----";
;
var_dump($pkey);
@noname007
Copy link
Author

string(401) "-----BEGIN PUBLIC KEY-----
4SNHY0Pzfn7P2EAbYIl96bzBYh5e3pdV6cRC/JoW1B7oLjRPGBNYZroTYPWHNGcR
JV2MQOWl+0WViDP2fHSHrB/1gzcW/Ma3sHgdb+OMHLlfTO65mxf2KpJ3d6BxA683
ncJNPizgVAg47o81SbP7WAg/v3BY0c9tfBXp2gO/NwnGGnnCsshY8yftZ1XfeIPC
Ew6bpLNvw2a3ty0EWcLqMmrxiYh+8hNDLoIL6twyF5Gqn9cIu7OrnFVuVL644V+I
8HVCAw3gTgu78FN4JfbWdhyRmpNQAJnIP40A97qLKy5Ajp+CDJOQuNHbpmfAR7et
s+PSw/39ch9ZmPgMVQkpmw==
-----END PUBLIC KEY-----"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment