DER格式的密钥转PEM
PEM格式的密钥就是用DER格式的数据用base64进行编码后,然后在前后加上 “-----BEGIN PUBLIC KEY-----” 和 “-----END PUBLIC KEY-----”
PHP
十进制DER数据转换
$derData = 'XXXXXXXXXXXXXXXXXXXX'; $pemData = "-----BEGIN PUBLIC KEY----- " . wordwrap(base64_encode($derData), 64, " ", true) . " -----END PUBLIC KEY-----";
十六进制DER格式数据转换
$derData = 'XXXXXXXXXXXXXXXXXXXX'; $pemData = "-----BEGIN PUBLIC KEY----- " . wordwrap(base64_encode(Hex2String($derData)), 64, " ", true) . " -----END PUBLIC KEY-----"; function Hex2String($hex) { $string = ''; for ($i = 0; $i < strlen($hex) - 1; $i += 2) { $string .= chr(hexdec($hex[$i] . $hex[$i + 1])); } return $string; }