开发者

Various PEM to DER in perl

i have application 开发者_Python百科with client-server architecture.

client (C program):

  1. generate various DER encoded data
  2. convert DER to PEM (using openssl's PEM_write_bio) with various PEM header
  3. send PEM to server

server (Perl script):

  1. receive PEM data
  2. convert PEM to DER
  3. ....

My question is how to convert various PEM data to DER/BER (binary data) in perl?


You can strip off the PEM tags yourself, and do a decode of the Base64 block inside using MIME::Base64.

Should be as simple as

$derBlob = decode_base64($base64Blob);


An example based on the accepted answer:

#!/usr/bin/perl

use strict;
use warnings;
use MIME::Base64;

my $certPath = 'cert.pem';

open my $fh, '<', $certPath or die(sprintf('Could not open %s file: %s', $certPath, $!));
my $derBlob = do { local $/; decode_base64(<$fh> =~ s/^-.*?\n//gmr); };
close($fh);
print $derBlob;

1;
__END__
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜